使用RGB你将难以避免灰色,以及"难以看到"的颜色(我在白色背景上猜测)
如果您需要它们是随机的,您可以使用HSV值远离白色,灰色和黑色光谱.这意味着您可以在值和饱和度参数中设置范围(例如,~175到255),而可以随意自由选择色调.
所以,这可能有效:
def random_bright_color(threshold = 175)
hue = rand(256 - threshold) + threshold
saturation = rand(256 - threshold) + threshold
value = rand(256)
hsv_to_rgb(hue, saturation, value)
end
Run Code Online (Sandbox Code Playgroud)
哪里
def hsv_to_rgb(h, s, v)
if s == 0
r = g = b = (v * 2.55).round
else
h /= 60.0
s /= 100.0
v /= 100.0
i = h.floor
f = h - i
p = v * (1 - s)
q = v * (1 - s * f)
t = v * (1 - s * (1 - f))
rgb = case i
when 0 then [v, t, p]
when 1 then [q, v, p]
when 2 then [q, v, t]
when 3 then [p, q, v]
when 4 then [t, p, v]
else [v, p, q]
end
end
rgb.map{|color| (color * 255).round}
end
Run Code Online (Sandbox Code Playgroud)
从这里移植,解释可以在同一篇维基百科文章中找到
但是,如果你还需要彼此不同的随机颜色,也许真正的解决方案是从一组基色中选择它们,并从该组中随机选择.
| 归档时间: |
|
| 查看次数: |
1461 次 |
| 最近记录: |