Skip to content

Commit 9405ad4

Browse files
committed
ascolor
1 parent 8750733 commit 9405ad4

File tree

11 files changed

+44
-44
lines changed

11 files changed

+44
-44
lines changed

examples/logo.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ function cloudshape(height, args...; backgroundcolor=(0, 0, 0, 0))
77
r = height / 2
88
d = Luxor.Drawing((2height, height)..., :svg)
99
Luxor.origin()
10-
Luxor.background(parsecolor(backgroundcolor))
11-
Luxor.setcolor(parsecolor((0.22, 0.596, 0.149)))
10+
Luxor.background(ascolor(backgroundcolor))
11+
Luxor.setcolor(ascolor((0.22, 0.596, 0.149)))
1212
Luxor.pie(0, 0, r, 0, 2π, :fill)
13-
Luxor.setcolor(parsecolor((0.584, 0.345, 0.698)))
13+
Luxor.setcolor(ascolor((0.584, 0.345, 0.698)))
1414
Luxor.pie(r, r, r, -π, 0, :fill)
15-
Luxor.setcolor(parsecolor((0.796, 0.235, 0.20)))
15+
Luxor.setcolor(ascolor((0.796, 0.235, 0.20)))
1616
Luxor.Luxor.pie(-r, r, r, -π, 0, :fill)
1717
Luxor.finish()
1818
WordCloud.SVG(Luxor.svgstring(), d.height, d.width)

examples/recolor.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ mixstyleimg = paint(wc, background=background2)
4444
#md#
4545
h, w = size(avgimg)
4646
lw = 21
47-
lc = eltype(avgimg)(parsecolor(0.1))
47+
lc = eltype(avgimg)(ascolor(0.1))
4848
vbar = zeros(eltype(avgimg), (h, lw))
4949
hbar = zeros(eltype(avgimg), (lw, 2w + lw))
5050
vbar[:, lw ÷ 2 + 1] .= lc

examples/semantic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ tree = hclust(D, linkage=:ward)
6666
lb = cutree(tree, h=3, k=8)
6767
println("$(length(lb)) words are divided into $(length(unique(lb))) groups")
6868
#md#
69-
colors = parsecolor(:seaborn_dark)
69+
colors = ascolor(:seaborn_dark)
7070
setcolors!(wc, words, colors[lb .% length(colors) .+ 1])
7171
recolor!(wc, style=:reset)
7272
paint(wc, "semantic_clustering.png")

src/WordCloud.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Please visit the repository at: <https://github.com/guo-yong-zhi/WordCloud.jl>
1111
"""
1212
module WordCloud
1313
export wordcloud, processtext, html2text, countwords, casemerge!, rescaleweights
14-
export parsecolor, rendertext, shape, ellipse, box, squircle, star, ngon, bezistar, bezingon,
14+
export ascolor, rendertext, shape, ellipse, box, squircle, star, ngon, bezistar, bezingon,
1515
loadmask, outline, padding, paint, paintsvg, paintcloud, paintsvgcloud, svgstring
1616
export imageof, showmask, showmask!
1717
export record, @record, layout!, rescale!, recolor!, keep, ignore, pin, runexample, showexample, generate!, fit!

src/artist.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function randomcolorscheme(weights_or_num=100)
148148
end
149149
colors
150150
end
151-
# function randomfilteredcolorscheme(args...; filter=colors->Gray(parsecolor(randommaskcolor(colors)))>0.5, maxiter=100)
151+
# function randomfilteredcolorscheme(args...; filter=colors->Gray(ascolor(randommaskcolor(colors)))>0.5, maxiter=100)
152152
# for _ in 1:maxiter
153153
# colors = randomcolorscheme(args...)
154154
# filter(colors) && return colors
@@ -340,7 +340,7 @@ function window(k, h=1)
340340
w .= h .* (1 .- (abs.(w) ./ r) .^ 2)
341341
end
342342
function randommaskcolor(colors)
343-
colors = parsecolor.(unique(colors))
343+
colors = ascolor.(unique(colors))
344344
colors = HSL.(colors)
345345
colors = [(c.h, c.s, c.l) for c in colors]
346346
rl = 77 # 256*0.3
@@ -401,7 +401,7 @@ function randomlinecolor(colors)
401401
else
402402
linecolor = (rand(), rand(), rand(), min(1.0, 0.5 + rand() / 2))
403403
end
404-
"#" * hex(parsecolor(linecolor))
404+
"#" * hex(ascolor(linecolor))
405405
end
406406
randomoutline() = rand((0, 0, 0, rand(2:10)))
407407
function randomfonts(lang="")

src/rendering.jl

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Render
22
export rendertext, overlay!,
33
shape, ellipse, box, squircle, star, ngon, bezistar, bezingon, ellipse_area, box_area, squircle_area,
4-
star_area, ngon_area, GIF, generate, parsecolor, rendertextoutlines,
4+
star_area, ngon_area, GIF, generate, ascolor, rendertextoutlines,
55
colorschemes, torgba, imagemask, outline, pad, dilate!, imresize, recolor!, recolor
66
export issvg, save, load, tobitmap, tosvg, SVG, svg_wrap, svg_add, svg_stack
77
using Luxor
@@ -11,13 +11,13 @@ using FileIO
1111
using ImageTransformations
1212
include("svg.jl")
1313
# because of float error, (randommask(color=Gray(0.3))|>tobitmap)[300,300]|>torgba != Gray(0.3)|>torgba
14-
parsecolor(c) = parsecolor(parse(Colorant, c))
15-
parsecolor(c::Colorant) = ARGB{Colors.N0f8}(c)
16-
parsecolor(c::ARGB{Colors.N0f8}) = c
17-
parsecolor(tp::Tuple) = ARGB{Colors.N0f8}(tp...)
18-
parsecolor(gray::Real) = ARGB{Colors.N0f8}(Gray(gray))
19-
parsecolor(sc::Symbol) = parsecolor.(colorschemes[sc].colors)
20-
parsecolor(sc::AbstractArray) = parsecolor.(sc)
14+
ascolor(c) = ascolor(parse(Colorant, c))
15+
ascolor(c::Colorant) = ARGB{Colors.N0f8}(c)
16+
ascolor(c::ARGB{Colors.N0f8}) = c
17+
ascolor(tp::Tuple) = ARGB{Colors.N0f8}(tp...)
18+
ascolor(gray::Real) = ARGB{Colors.N0f8}(Gray(gray))
19+
ascolor(sc::Symbol) = ascolor.(colorschemes[sc].colors)
20+
ascolor(sc::AbstractArray) = ascolor.(sc)
2121

2222
Base.size(s::Drawing) = (s.height, s.width)
2323
issvg(d) = d isa SVG
@@ -130,7 +130,7 @@ function imresize(svg::SVG, sz...; ratio=1)
130130
end
131131

132132
function drawtext(t, size, pos, angle=0, color="black", font="")
133-
setcolor(parsecolor(color))
133+
setcolor(ascolor(color))
134134
setfont(font, size)
135135
settext(t, Point(pos...), halign="center", valign="center"; angle=angle)
136136
end
@@ -146,7 +146,7 @@ function rendertext(str::AbstractString, size::Real;
146146
drawing = Drawing(l, l, :svg) # svg is slow
147147
end
148148
origin()
149-
bgcolor = parsecolor(backgroundcolor)
149+
bgcolor = ascolor(backgroundcolor)
150150
bgcolor = background(bgcolor)
151151

152152
drawtext(str, size, pos, angle, color, font)
@@ -175,10 +175,10 @@ function rendertextoutlines(str::AbstractString, size::Real; color="black", bgco
175175
l = length(str)
176176
Drawing(ceil(Int, 2l * (size + 2linewidth) + 2), ceil(Int, 2 * (size + 2linewidth) + 2), :image)
177177
origin()
178-
bgcolor = parsecolor(bgcolor)
178+
bgcolor = ascolor(bgcolor)
179179
bgcolor = background(bgcolor)
180180
# bgcolor = Luxor.ARGB32(bgcolor...)
181-
setcolor(parsecolor(color))
181+
setcolor(ascolor(color))
182182
# setfont(font, size)
183183
fontface(font)
184184
fontsize(size)
@@ -193,7 +193,7 @@ function rendertextoutlines(str::AbstractString, size::Real; color="black", bgco
193193
end
194194

195195
function torgba(c)
196-
c = Colors.RGBA{Colors.N0f8}(parsecolor(c))
196+
c = Colors.RGBA{Colors.N0f8}(ascolor(c))
197197
rgba = (Colors.red(c), Colors.green(c), Colors.blue(c), Colors.alpha(c))
198198
reinterpret.(UInt8, rgba)
199199
end
@@ -219,7 +219,7 @@ function imagemask(img::AbstractMatrix, transparent=:auto)
219219
if transparent === nothing
220220
return trues(size(img))
221221
end
222-
img .!= convert(eltype(img), parsecolor(transparent))
222+
img .!= convert(eltype(img), ascolor(transparent))
223223
end
224224
imagemask(img::SVG, istransparent::Function) = imagemask(tobitmap(img), istransparent)
225225
imagemask(img::SVG, transparent::AbstractArray{Bool,2}) = .!transparent
@@ -281,22 +281,22 @@ function outline(img; transparent=:auto, color="black", linewidth=2, smoothness=
281281
r = 2 * linewidth * smoothness
282282
# @show r
283283
mask2 = dilate2(mask, max(linewidth, round(r)), smoothness=smoothness)
284-
c = ARGB(parsecolor(color)) # https://github.com/JuliaGraphics/Colors.jl/issues/500
284+
c = ARGB(ascolor(color)) # https://github.com/JuliaGraphics/Colors.jl/issues/500
285285
bg = convert.(eltype(img), coloralpha.(c, mask2))
286286
bg = overlay!(copy(img), bg)
287287
@views bg[mask] .= overlay.(bg[mask], img[mask])
288288
bg
289289
end
290290

291291
function pad(img::AbstractMatrix, r=maximum(size(img)) ÷ 10; backgroundcolor=:auto)
292-
color = convert(eltype(img), parsecolor(_backgroundcolor(img, backgroundcolor)))
292+
color = convert(eltype(img), ascolor(_backgroundcolor(img, backgroundcolor)))
293293
r = ceil.(Int, r)
294294
bg = fill(color, size(img) .+ 2 .* r)
295295
overlay!(bg, img, reverse((0, 0) .+ r)...)
296296
end
297297

298298
function pad(img::SVG, r=maximum(size(img)) ÷ 10; backgroundcolor=(0, 0, 0, 0))
299-
color = parsecolor(backgroundcolor)
299+
color = ascolor(backgroundcolor)
300300
sz = size(img) .+ 2 .* ceil.(Int, r)
301301
p = readsvg(string(img))
302302
m2 = Drawing(reverse(sz)..., :svg)
@@ -349,7 +349,7 @@ end
349349

350350
function overlay(imgs, poss; backgroundcolor=(1, 1, 1, 0), size=size(imgs[1]))
351351
bg = Drawing(size..., :svg)
352-
Luxor.background(parsecolor(backgroundcolor))
352+
Luxor.background(ascolor(backgroundcolor))
353353
finish()
354354
bg = SVG(svgstring(), bg.height, bg.width)
355355
# (x,y)=(1,1)时左上角重合,此时Point(0,0)
@@ -358,11 +358,11 @@ function overlay(imgs, poss; backgroundcolor=(1, 1, 1, 0), size=size(imgs[1]))
358358
bg
359359
end
360360
function recolor!(img::AbstractArray, color)
361-
c = parsecolor(color)
361+
c = ascolor(color)
362362
img .= convert.(eltype(img), Colors.alphacolor.(c, Colors.alpha.(img)))
363363
end
364364
function recolor(img::AbstractArray, color)
365-
c = parsecolor(color)
365+
c = ascolor(color)
366366
convert.(eltype(img), Colors.alphacolor.(c, Colors.alpha.(img)))
367367
end
368368

@@ -446,13 +446,13 @@ function shape(shape_, width, height, args...;
446446
kargs...)
447447
d = Drawing(ceil.(backgroundsize)..., :svg)
448448
origin()
449-
background(parsecolor(backgroundcolor))
449+
background(ascolor(backgroundcolor))
450450
if outline > 0
451451
setline(outline)
452-
setcolor(parsecolor(linecolor))
452+
setcolor(ascolor(linecolor))
453453
shape_(Point(0, 0), width, height, args...; action=:stroke, kargs...)
454454
end
455-
setcolor(parsecolor(color))
455+
setcolor(ascolor(color))
456456
shape_(Point(0, 0), width, height, args...; action=:fill, kargs...)
457457
finish()
458458
SVG(svgstring(), d.height, d.width)

src/strategy.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function prepareword(word, fontsize, color, angle; backgroundcolor=(0, 0, 0, 0),
105105
end
106106

107107
wordmask(img, bgcolor, border) = dilate!(alpha.(img) .!= 0, border)
108-
# use `alpha` instead of `convert(eltype(img), parsecolor(bgcolor))`
108+
# use `alpha` instead of `convert(eltype(img), ascolor(bgcolor))`
109109
# https://github.com/JuliaGraphics/Luxor.jl/issues/107
110110
function ternary_wordmask(img, bgcolor, border)
111111
tmask = fill(Stuffing.EMPTY, size(img))

src/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function imageof(layer::AbstractMatrix{UInt8})
55
end
66
function showmask!(img, mask; highlight=ARGB(1, 0, 0, 0.3))
77
mask = .!mask
8-
hl = convert(eltype(img), parsecolor(highlight))
8+
hl = convert(eltype(img), ascolor(highlight))
99
img[mask] .= Render.overlay.(img[mask], hl)
1010
img
1111
end

src/wc-class.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ const _SET_DOC = "This function accepts three positional arguments: a wordcloud
356356
@doc _GET_DOC getfonts(wc::WC, w=:) = wc.params[:fonts][index(wc, w)]
357357
@doc _GET_DOC getwords(wc::WC, w=:) = wc.words[index(wc, w)]
358358
@doc _GET_DOC getweights(wc::WC, w=:) = wc.weights[index(wc, w)]
359-
@doc _SET_DOC setcolors!(wc::WC, w, c) = @view(wc.params[:colors][index(wc, w)]) .= parsecolor(c)
359+
@doc _SET_DOC setcolors!(wc::WC, w, c) = @view(wc.params[:colors][index(wc, w)]) .= ascolor(c)
360360
@doc _SET_DOC setangles!(wc::WC, w, a::Union{Number,AbstractVector{<:Number}}) = @view(wc.params[:angles][index(wc, w)]) .= a
361361
@doc _SET_DOC
362362
function setfonts!(wc::WC, w, v::Union{AbstractString,AbstractVector{<:AbstractString}})

src/wc-helper.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function loadmask(img::AbstractMatrix{<:TransparentRGB}, args...;
3636
img = imresize(img, args...; ratio=ratio .* vf)
3737
copied = true
3838
end
39-
backgroundcolor DEFAULTSYMBOLS && (backgroundcolor = parsecolor(backgroundcolor))
39+
backgroundcolor DEFAULTSYMBOLS && (backgroundcolor = ascolor(backgroundcolor))
4040
if padding != 0 # padding before cal imagemask
4141
bc = backgroundcolor in DEFAULTSYMBOLS ? :auto : backgroundcolor
4242
img = pad(img, padding, backgroundcolor=bc)
@@ -47,7 +47,7 @@ function loadmask(img::AbstractMatrix{<:TransparentRGB}, args...;
4747
if color DEFAULTSYMBOLS || backgroundcolor DEFAULTSYMBOLS
4848
copied || (img = copy(img))
4949
if color DEFAULTSYMBOLS
50-
color = parsecolor(color)
50+
color = ascolor(color)
5151
alpha(color) == 1 || @warn "the alpha channel is ignored"
5252
m = @view img[mask]
5353
Render.recolor!(m, color) # 保持透明度
@@ -137,10 +137,10 @@ function paint(wc::WC, args...; background=true, kargs...)
137137
if background == true
138138
bgcolor = getbackgroundcolor(wc)
139139
bgcolor in DEFAULTSYMBOLS && (bgcolor = (1,1,1,0))
140-
background = fill(convert(eltype(wc.mask), parsecolor(bgcolor)), size(wc.mask))
140+
background = fill(convert(eltype(wc.mask), ascolor(bgcolor)), size(wc.mask))
141141
overlay!(background, wc.mask)
142142
elseif background == false || background === nothing
143-
background = fill(convert(eltype(wc.mask), parsecolor((1,1,1,0))), size(wc.mask))
143+
background = fill(convert(eltype(wc.mask), ascolor((1,1,1,0))), size(wc.mask))
144144
else
145145
background = copy(background)
146146
end

test/runtests.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ include("test_textprocessing.jl")
5050

5151
##############no mask file
5252
wc = wordcloud(["test"], [1], maskcolor="green", outline=5)
53-
@test WordCloud.alpha(parsecolor(getbackgroundcolor(wc))) == 0
53+
@test WordCloud.alpha(ascolor(getbackgroundcolor(wc))) == 0
5454
wc = wordcloud(["test"], [1], backgroundcolor="blue", outline=5)
55-
@test parsecolor(getmaskcolor(wc)) == parsecolor("blue")
55+
@test ascolor(getmaskcolor(wc)) == ascolor("blue")
5656
wc = wordcloud(["test"], [1], maskcolor="green")
5757
@test getparameter(wc, :outline) == 0
5858
wc = wordcloud(["test"], [1], backgroundcolor="blue")
@@ -84,8 +84,8 @@ include("test_textprocessing.jl")
8484
wc = wordcloud(["test"], [1], colors="#DE2910", mask=pngfile, backgroundcolor=0)
8585
@test getmaskcolor(wc) == getbackgroundcolor(wc)
8686
wc = wordcloud(["test"], [1], colors="#DE2910", mask=pngfile, maskcolor=1, backgroundcolor=0)
87-
@test parsecolor(getmaskcolor(wc)) == parsecolor(1)
88-
@test parsecolor(getbackgroundcolor(wc)) == parsecolor(0)
87+
@test ascolor(getmaskcolor(wc)) == ascolor(1)
88+
@test ascolor(getbackgroundcolor(wc)) == ascolor(0)
8989
wordcloud(["test"], [1], mask=pngfile, backgroundcolor=:maskcolor) # backgroundcolor=maskcolor=:default, didn't change anything
9090
wordcloud(["test"], [1], mask=pngfile, maskcolor="#faeef8", backgroundcolor=:maskcolor)
9191
wordcloud(["test"], [1], mask=pngfile, maskcolor="#faeef8", backgroundcolor=:maskcolor, outline=3)

0 commit comments

Comments
 (0)