@@ -328,31 +328,34 @@ function sample(wv)
328
328
end
329
329
function rand_argmin (a, tor= 10 )
330
330
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]
332
332
i = sample (last .(ind_weight))
333
333
ind_weight[i] |> first
334
334
end
335
335
function array_max! (a, b)
336
336
a .= max .(a, b)
337
337
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 )
342
345
end
343
346
function randommaskcolor (colors)
344
347
colors = ascolor .(unique (colors))
345
348
colors = HSL .(colors)
346
349
colors = [(c. h, c. s, c. l) for c in colors]
347
350
rl = 77 # 256*0.3
348
351
rl2 = 102 # 256*0.4
349
- l_slots = zeros (256 + 2 rl2) .+ 0.00625
352
+ l_slots = zeros (256 + 2 rl2) .+ 1e-3
350
353
rh = 30
351
- h_slots = zeros (360 ) .+ 0.00625
354
+ h_slots = zeros (360 ) .+ 1e-3
352
355
s2max = 0.6
353
- win_l = window (2 rl + 1 )
354
- win_l2 = window (2 rl2 + 1 )
355
- win_h = window (2 rh + 1 )
356
+ win_l = expwin (2 rl + 1 , 1 , 3 )
357
+ win_l2 = expwin (2 rl2 + 1 , 1 , 3 )
358
+ win_h = expwin (2 rh + 1 , 1 , 3 )
356
359
for c in colors
357
360
li = clamp (round (Int, c[3 ] * 255 ), 0 , 255 ) + 1
358
361
array_max! (@view (l_slots[li- rl+ rl2: li+ rl+ rl2]), win_l) # 明度避让以保证对比度
@@ -388,13 +391,10 @@ function randommaskcolor(colors)
388
391
end
389
392
l_slots = l_slots[1 + rl2: 256 + rl2]
390
393
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 ) # 高亮偏好
393
396
l_slots .*= w
394
397
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
398
398
s2 = rand () * s2max
399
399
" #" * hex (HSL (h2, s2, l2))
400
400
end
0 commit comments