Skip to content

Commit 9bb9778

Browse files
committed
support vector of colors
1 parent 6f898ca commit 9bb9778

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/rendering.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ Render `str` into `img` using the font `face` of size `pixelsize` at coordinates
3737
3838
# Arguments
3939
* `y0,x0`: origin is in upper left with positive `y` going down
40-
* `fcolor`: foreground color; typemax(T) for T<:Integer, otherwise one(T)
40+
* `fcolor`: foreground color; AbstractVector{T}, typemax(T) for T<:Integer, otherwise one(T)
4141
* `bcolor`: background color; set to `nothing` for transparent
4242
* `halign`: :hleft, :hcenter, or :hright
4343
* `valign`: :vtop, :vcenter, :vbaseline, or :vbottom
4444
"""
4545
function renderstring!(
4646
img::AbstractMatrix{T}, str::String, face::FTFont, pixelsize::Union{Int, Tuple{Int, Int}}, y0, x0;
47-
fcolor::T = one_or_typemax(T), bcolor::Union{T,Nothing} = zero(T),
47+
fcolor::Union{AbstractVector{T},T} = one_or_typemax(T), bcolor::Union{T,Nothing} = zero(T),
4848
halign::Symbol = :hleft, valign::Symbol = :vbaseline
4949
) where T<:Union{Real,Colorant}
5050

@@ -103,21 +103,27 @@ function renderstring!(
103103
px += kx
104104
end
105105

106+
fcol = if fcolor isa AbstractVector
107+
fcolor[istr]
108+
else
109+
fcolor
110+
end
111+
106112
# trim parts of glyph images that are outside the destination
107113
cliprowlo, cliprowhi = max(0, -(py-by)), max(0, py - by + h - imgh)
108114
clipcollo, clipcolhi = max(0, -bx-px), max(0, px + bx + w - imgw)
109115

110116
if bcolor === nothing
111117
for row = 1+cliprowlo : h-cliprowhi, col = 1+clipcollo : w-clipcolhi
112118
bitmaps[istr][col,row]==0 && continue
113-
c1 = bitmaps[istr][col,row] / bitmapmax * fcolor
119+
c1 = bitmaps[istr][col,row] / bitmapmax * fcol
114120
img[row+py-by, col+px+bx] = T <: Integer ? round(T, c1) : T(c1)
115121
end
116122
else
117123
for row = 1+cliprowlo : h-cliprowhi, col = 1+clipcollo : w-clipcolhi
118124
bitmaps[istr][col, row] == 0 && continue
119125
w1 = bitmaps[istr][col, row] / bitmapmax
120-
c1 = w1 * fcolor
126+
c1 = w1 * fcol
121127
c0 = (1.0 - w1) * bcolor
122128
img[row + py - by, col + px + bx] = T <: Integer ? round(T, c1 + c0) : T(c1 + c0)
123129
end

0 commit comments

Comments
 (0)