Skip to content

Commit c241caf

Browse files
committed
use expwin
1 parent e5ae45b commit c241caf

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/artist.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -328,31 +328,34 @@ function sample(wv)
328328
end
329329
function rand_argmin(a, tor=10)
330330
th = minimum(a) * tor
331-
ind_weight = [(i, 1 / (ai + 1e-6)^2) for (i, ai) in enumerate(a) if ai <= th]
331+
ind_weight = [(i, 1 / (ai + 1e-6)) for (i, ai) in enumerate(a) if ai <= th]
332332
i = sample(last.(ind_weight))
333333
ind_weight[i] |> first
334334
end
335335
function array_max!(a, b)
336336
a .= max.(a, b)
337337
end
338-
function window(k, h=1)
339-
r = k ÷ 2
340-
w = collect(-(k - 1 - r):1.0:r)
341-
w .= h .* (1 .- (abs.(w) ./ r) .^ 2)
338+
function expwin(n, h=1, λ=3)
339+
f(x, λ) = 10.0^(-λ * abs(x))
340+
h * f.(range(-1.0, 1.0, n), λ)
341+
end
342+
function gausswin(n, h=1)
343+
g(x, σ) = exp(-0.5 * (x / σ)^2)
344+
h * g.(range(-0.5, 0.5, n), 0.5 / 2.5)
342345
end
343346
function randommaskcolor(colors)
344347
colors = ascolor.(unique(colors))
345348
colors = HSL.(colors)
346349
colors = [(c.h, c.s, c.l) for c in colors]
347350
rl = 77 # 256*0.3
348351
rl2 = 102 # 256*0.4
349-
l_slots = zeros(256 + 2rl2) .+ 0.00625
352+
l_slots = zeros(256 + 2rl2) .+ 1e-3
350353
rh = 30
351-
h_slots = zeros(360) .+ 0.00625
354+
h_slots = zeros(360) .+ 1e-3
352355
s2max = 0.6
353-
win_l = window(2rl + 1)
354-
win_l2 = window(2rl2 + 1)
355-
win_h = window(2rh + 1)
356+
win_l = expwin(2rl + 1, 1, 3)
357+
win_l2 = expwin(2rl2 + 1, 1, 3)
358+
win_h = expwin(2rh + 1, 1, 3)
356359
for c in colors
357360
li = clamp(round(Int, c[3] * 255), 0, 255) + 1
358361
array_max!(@view(l_slots[li-rl+rl2:li+rl+rl2]), win_l) # 明度避让以保证对比度
@@ -388,13 +391,10 @@ function randommaskcolor(colors)
388391
end
389392
l_slots = l_slots[1+rl2:256+rl2]
390393
w = ones(length(l_slots))
391-
w[51:end-51] .+= window(256 - 101, 7) # 中间调回避
392-
array_max!(@view(w[1:end÷2]), 3) # 高亮偏好
394+
w[51:end-51] .+= gausswin(256 - 101, 7) # 中间调回避
395+
array_max!(@view(w[1:end÷2]), 4) # 高亮偏好
393396
l_slots .*= w
394397
l2 = (rand_argmin(l_slots, 10) - 1) / 255
395-
# 10 > max(3, 1+7),不会截断概率,故高亮和中间调仅是偏好策略
396-
# 0.00625*8*10=0.5,截断最高能达到明度避让策略最高点(1.0)的一半,
397-
# 相对应地,保证明度差起码有sqrt(1-0.5/1.0)*(77/256)=0.21
398398
s2 = rand() * s2max
399399
"#" * hex(HSL(h2, s2, l2))
400400
end

0 commit comments

Comments
 (0)