Skip to content

Commit

Permalink
better transparent judge
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-yong-zhi committed Dec 10, 2021
1 parent c523503 commit afe16fe
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "WordCloud"
uuid = "6385f0a0-cb03-45b6-9089-4e0acc74b26b"
authors = ["guoyongzhi <[email protected]>"]
version = "0.9.0"
version = "0.9.1"

[deps]
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ paint(wc, "alice.png", ratio=0.5)
*The variable `WordCloud.examples` holds all available examples.*
You can also [**see more examples**](https://github.com/guo-yong-zhi/WordCloud-Gallery) or [**try it online**](https://mybinder.org/v2/gh/guo-yong-zhi/WordCloud.jl/master?filepath=examples.ipynb).
# About Implementation
Unlike most other implementations, WordCloud.jl is programmed based on image local gradient optimization. It’s a non-greedy algorithm in which words can be further [moved](res/animation.gif) after they are positioned. This means shrinking words is unnecessary, thus the word size can be kept unchanged during the adjustment. In addition, it allows words to be assigned to any initial position whether or not there will be an overlap. This enables the program to achieve the maximum flexibility. See also [Stuffing.jl - Algorithm Description](https://github.com/guo-yong-zhi/Stuffing.jl#algorithm-description).
Unlike most other implementations, WordCloud.jl is programmed based on image local gradient optimization. It’s a non-greedy algorithm in which words can be further [moved](res/animation2.gif) after they are positioned. This means shrinking words is unnecessary, thus the word size can be kept unchanged during the adjustment. In addition, it allows words to be assigned to any initial position whether or not there will be an overlap. This enables the program to achieve the maximum flexibility. See also [Stuffing.jl - Algorithm Description](https://github.com/guo-yong-zhi/Stuffing.jl#algorithm-description).
* [x] 权重计算和单词位置初始化
* [x] 基于四叉树(层次包围盒)的碰撞检测
* [x] 根据局部灰度梯度平移单词(训练迭代)
Expand Down
1 change: 1 addition & 0 deletions examples/lettermask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ wc = wordcloud(
angles=0,
colors=("#006BB0", "#EFA90D", "#1D1815", "#059341", "#DC2F1F"),
density=0.55,
spacing=0,
) |> generate!
println("results are saved to lettermask.svg")
paint(wc, "lettermask.svg" , background=false)
Expand Down
20 changes: 9 additions & 11 deletions examples/outline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ words = (1:200) .% 10 .|> string
weights = (1:200) .% 11 .+ 1
#md# ### SVG
#md# You can directly set the `outline` and `maskcolor` in `wordcloud`
#md# ```julia
#md# wc1 = wordcloud(
#md# words, weights,
#md# mask = squircle, rt=0.5,
#md# masksize = (300, 200),
#md# maskcolor = "AliceBlue",
#md# outline = 6, linecolor = "navy"
#md# ) |> generate!
#md# ```
wc1 = wordcloud(
words, weights,
mask = squircle, rt=0.5,
masksize = (300, 200),
maskcolor = "AliceBlue",
outline = 6, linecolor = "navy"
) |> generate!
#md# Or if you already have a SVG mask with outline, you should set a proper transparent region in `wordcloud`
svgmask = shape(squircle, 300, 200, outline=6, linecolor="navy", color="AliceBlue")
wc1 = wordcloud(
words, weights,
mask=svgmask,
mask = svgmask,
transparent=c -> c != WordCloud.torgba("AliceBlue"), # the outline should be regarded as transparent too
) |> generate!

Expand All @@ -28,7 +26,7 @@ println("results are saved to outline.svg")
bitmapmask = WordCloud.svg2bitmap(shape(squircle, 300, 200, color="AliceBlue", backgroundsize=(312, 212)))
wc2 = wordcloud(
words, weights,
mask=bitmapmask,
mask = bitmapmask,
) |> generate!
paint(wc2, "outline.png", background=outline(bitmapmask, color="navy", linewidth=6, smoothness=0.8))
println("results are saved to outline.png")
Expand Down
5 changes: 2 additions & 3 deletions src/rendering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,8 @@ function _backgroundcolor(img, c=:auto)
end
end
imagemask(img::AbstractArray{Bool,2}) = img
function imagemask(img, istransparent::Function)
.! istransparent.(torgba.(img))
end
imagemask(img, istransparent::Function) = .!istransparent.(torgba.(img))
imagemask(img, transparent::AbstractArray{Bool,2}) = .!transparent
function imagemask(img, transparent=:auto)
if transparent == :auto
if img[1] == img[end] && any(c -> c != img[1], img)
Expand Down
7 changes: 5 additions & 2 deletions src/wc-class.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ function getstylescheme(words, weights; colors=:auto, angles=:auto, mask=:auto,
end
padding in DEFAULTSYMBOLS && (padding = round(Int, maximum(masksize) ÷ 10))
mask = randommask(masksize; maskshape=mask ,color=maskcolor, padding=padding, keeparea=keepmaskarea, kg..., kargs...)
transparent = c -> c != torgba(maskcolor)
else
ms = masksize in DEFAULTSYMBOLS ? () : masksize
if maskcolor == :auto && !issvg(loadmask(mask))
Expand Down Expand Up @@ -187,9 +188,11 @@ function getstylescheme(words, weights; colors=:auto, angles=:auto, mask=:auto,
linecolor = randomlinecolor(colors)
end
padding in DEFAULTSYMBOLS && (padding = 0)
mask = loadmask(mask, ms...; color=maskcolor, transparent=transparent, backgroundcolor=bc,
outline=outline, linecolor=linecolor,padding=padding, kargs...)
mask, binarymask = loadmask(mask, ms...; color=maskcolor, transparent=transparent, backgroundcolor=bc,
outline=outline, linecolor=linecolor,padding=padding, return_binarymask=true, kargs...)
binarymask === nothing || (transparent = .!binarymask)
end
# under this line: both mask == :auto or not
if transparent == :auto
if maskcolor DEFAULTSYMBOLS
transparent = c -> c[4] == 0 || c[1:3] != WordCloud.torgba(maskcolor)[1:3] #ignore the alpha channel when alpha!=0
Expand Down
13 changes: 8 additions & 5 deletions src/wc-helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ About orther keyword arguments like outline, linecolor, smoothness, see function
"""
function loadmask(img::AbstractMatrix{<:TransparentRGB}, args...;
color=:auto, backgroundcolor=:auto, transparent=:auto,
outline=0, linecolor="black", smoothness=0.5, padding=0, kargs...)
outline=0, linecolor="black", smoothness=0.5, padding=0, return_binarymask=false, kargs...)
copied = false
if !(isempty(args) && isempty(kargs))
img = imresize(img, args...; kargs...)
copied = true
end
if return_binarymask || color DEFAULTSYMBOLS || backgroundcolor DEFAULTSYMBOLS
mask = imagemask(img, transparent)
end
if color DEFAULTSYMBOLS || backgroundcolor DEFAULTSYMBOLS
copied || (img = copy(img))
mask = imagemask(img, transparent)
if color DEFAULTSYMBOLS
color = parsecolor(color)
alpha(color) == 1 || @warn "the alpha channel is ignored"
Expand All @@ -54,12 +56,13 @@ function loadmask(img::AbstractMatrix{<:TransparentRGB}, args...;
bc = backgroundcolor in DEFAULTSYMBOLS ? :auto : backgroundcolor
img = Render.padding(img, padding, backgroundcolor=bc)
end
img
return_binarymask ? (img, mask) : img
end
function loadmask(img::AbstractMatrix{<:Colorant}, args...; kargs...)
loadmask(ARGB.(img), args...; kargs...)
end
function loadmask(img::SVGImageType, args...; padding=0, transparent=:auto, outline=0, linecolor=:auto, kargs...)
function loadmask(img::SVGImageType, args...;
padding=0, transparent=:auto, outline=0, linecolor=:auto, return_binarymask=false, kargs...)
if !isempty(args) || !isempty(v for v in values(values(kargs)) if v DEFAULTSYMBOLS) || outline != 0
@warn "editing svg file is not supported: $args $kargs"
end
Expand All @@ -68,7 +71,7 @@ function loadmask(img::SVGImageType, args...; padding=0, transparent=:auto, outl
bc in DEFAULTSYMBOLS && (bc = (0,0,0,0))
img = Render.padding(img, padding, backgroundcolor=bc)
end
img
return_binarymask ? (img, nothing) : img
end
function loadmask(file, args...; kargs...)
mask = Render.load(file)
Expand Down

2 comments on commit afe16fe

@guo-yong-zhi
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/50285

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.1 -m "<description of version>" afe16fea7245dedfa60e93106ded275d0e6c571a
git push origin v0.9.1

Please sign in to comment.