Skip to content

Commit

Permalink
fix keeparea
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-yong-zhi committed Oct 25, 2021
1 parent 39508ca commit d4aedd1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
19 changes: 13 additions & 6 deletions src/artist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ function randomscheme()
c = Render.colorschemes[scheme].colors
colors = randsubseq(c, rand())
colors = isempty(colors) ? c : colors
@show (scheme, length(colors))
println("color scheme: ", repr(scheme), ", length: ", length(colors))
else
colors = rand((0, 1, 0, 1, 0, 1, (0, 1), rand(), (rand(), rand())))
@show colors
end
(colors...,)
end
function randommask(sz::Number=800; kargs...)
s = sz * sz * (0.9 + 0.2rand())
s = sz * sz
ratio = (0.5 + rand() / 2)
ratio = ratio > 0.9 ? 1.0 : ratio
h = round(Int, sqrt(s * ratio))
Expand All @@ -79,8 +79,7 @@ end
function randommask(sz; kargs...)
randommask(sz...; kargs...)
end
function randommask(w, h, args...; maskshape=:rand, keeparea=:auto, kargs...)
keeparea = keeparea == :auto ? maskshape == :rand : keeparea
function randommask(w, h, args...; maskshape=:rand, kargs...)
ran = Dict(box => 0.2, squircle => 0.7, ellipse => 1, :rand => rand())[maskshape]
if ran <= 0.2
return randombox(w, h, args...; kargs...)
Expand Down Expand Up @@ -113,6 +112,7 @@ function randomsquircle(w, h; rt=:rand, keeparea=false, kargs...)
rt = 1 + 1.5rand()
end
end
rt = round(rt, digits=3)
end
sc = keeparea ? sqrt(w*h/squircle_area(w, h, rt=rt)) : 1
w = round(Int, w*sc); h = round(Int, h*sc)
Expand Down Expand Up @@ -159,7 +159,10 @@ function randommaskcolor(colors)
th1 = clamp(th2 - 0.15, 0, 1)
default = 0.0
end
maskcolor = rand((default, (rand(th1:0.001:th2), rand(th1:0.001:th2), rand(th1:0.001:th2))))
maskcolor = rand((default,
(round(rand(th1:0.001:th2), digits=3),
round(rand(th1:0.001:th2), digits=3),
round(rand(th1:0.001:th2), digits=3))))
# @show maskcolor
return maskcolor
catch e
Expand All @@ -172,7 +175,11 @@ function randomlinecolor(colors)
if rand() < 0.8
linecolor = rand((colors[1], colors[1], rand(colors)))
else
linecolor = (rand(), rand(), rand(), min(1., 0.5 + rand() / 2))
linecolor = (
round(rand(), digits=3),
round(rand(), digits=3),
round(rand(), digits=3),
min(1., round(0.5 + rand()/2, digits=3)))
end
linecolor
end
Expand Down
11 changes: 7 additions & 4 deletions src/wc-class.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Positional arguments are used to specify words and weights, and can be in differ
* mask = loadmask("res/heart.jpg", color="red", ratio=2) #see doc of `loadmask`
* mask = shape(ellipse, 800, 600, color="white", backgroundcolor=(0,0,0,0)) #see doc of `shape`
* maskshape: `box`, `ellipse`, or `squircle`. See `shape`. Take effect only when the `mask` argument is not given.
* masksize: Can be a tuple `(width, height)`, tuple `(width, height, cornerradius)` (for `box` only) or just a single number as hint.
* masksize: Can be a tuple `(width, height)`, tuple `(width, height, cornerradius)` (for `box` only) or just a single number as a side length hint.
* backgroundsize: See `shape`. Take effect only when the `mask` argument is not given.
* maskcolor: like "black", "#ff0000", (0.5,0.5,0.7), 0.2, or :default, :original (keep it unchanged), :auto (auto recolor the mask).
* backgroundcolor: like "black", "#ff0000", (0.5,0.5,0.7), 0.2, or :default, :original, :maskcolor, :auto (random choose between :original and :maskcolor)
Expand Down Expand Up @@ -78,9 +78,9 @@ function wordcloud(words::AbstractVector{<:AbstractString}, weights::AbstractVec
if maskoccupying == 0
error("Have you set the right `transparent`? e.g. `transparent=mask[1,1]`")
end
usablesize = round(Int, maskoccupying)
contentsize = round(Int, maskoccupying)
avgsize = round(Int, sqrt(maskoccupying / length(words)))
println("mask size: $(size(mask, 1))×$(size(mask, 2)), usable area: $(usablesize)² ($(avgsize)²/word)")
println("mask size: $(size(mask, 1))×$(size(mask, 2)), content area: $(contentsize)² ($(avgsize)²/word)")

@assert maskoccupying > 0
if minfontsize == :auto
Expand Down Expand Up @@ -132,8 +132,11 @@ function getstylescheme(words, weights; colors=:auto, angles=:auto, mask=:auto,
maskcolor = backgroundcolor
end
end
if keepmaskarea in DEFAULTSYMBOLS
keepmaskarea = masksize in DEFAULTSYMBOLS
end
weights = weights ./ (sum(weights) / length(weights)) #权重为平均值的单词为中等大小的单词。weights不平方,即按条目平均,而不是按面积平均
masksize = masksize in DEFAULTSYMBOLS ? 12 * sum(length.(words) .* weights .^ 2) : masksize #中等大小的单词其每个字母占据16 pixel*16 pixel
masksize = masksize in DEFAULTSYMBOLS ? 12 * sum(length.(words) .* weights .^ 2) : masksize #中等大小的单词其每个字母占据12 pixel*12 pixel
if backgroundcolor in DEFAULTSYMBOLS
backgroundcolor = maskcolor0 in DEFAULTSYMBOLS ? rand(((1, 1, 1, 0), :maskcolor)) : (1, 1, 1, 0)
end
Expand Down
2 changes: 1 addition & 1 deletion src/wc-method.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function placewords!(wc::WC; style=:uniform, rt=:auto, centerlargestword=:auto,
&& wc.weights[2] / wc.weights[1] < 0.5
&& prod(kernelsize(wc.qtrees[2])) / prod(kernelsize(wc.qtrees[1])) < 0.5))
if centerlargestword
println("Center the largest word $(repr(getwords(wc, 1)))")
println("center the largest word $(repr(getwords(wc, 1)))")
end
end
arg = ()
Expand Down

2 comments on commit d4aedd1

@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/47438

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.8.0 -m "<description of version>" d4aedd10f9d5746c91bc5e789b86b0a7dbb414a9
git push origin v0.8.0

Please sign in to comment.