Ruby,生成一个随机的十六进制颜色(只有浅色) [英] Ruby, Generate a random hex color (only light colors)

查看:466
本文介绍了Ruby,生成一个随机的十六进制颜色(只有浅色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是重复的问题。
Ruby,生成随机十六进制颜色

I know this is possible duplicated question. Ruby, Generate a random hex color

我的问题略有不同。我需要知道,如何生成随机十六进制的光色,而不是黑暗。

My question is slightly different. I need to know, how to generate the random hex light colors only, not the dark.

推荐答案

在这个线程中,颜色亮度用公式

In this thread colour lumincance is described with a formula of

(0.2126*r) + (0.7152*g) + (0.0722*b)

亮度的相同公式为在维基百科中找到(它取自本出版物)。

The same formula for luminance is given in wikipedia (and it is taken from this publication). It reflects the human perception, with green being the most "intensive" and blue the least.

因此,您可以选择r,g,b直到亮度值超过上限为止 dark (255到0)之间的分隔。例如:

Therefore, you can select r, g, b until the luminance value goes above the division between light and dark (255 to 0). For example:

lum, ary = 0, []
while lum < 128
 ary = (1..3).collect {rand(256)}
 lum = ary[0]*0.2126 + ary[1]*0.7152 + ary[2]*0.0722
end

另一篇文章指的是亮度,是r,g和b的算术平均值。注意,亮度甚至更主观,因为给定的目标亮度可以在不同的上下文中引起不同的亮度感知(特别地,周围的颜色可以影响你的感知)。

Another article refers to brightness, being the arithmetic mean of r, g and b. Note that brightness is even more subjective, as a given target luminance can elicit different perceptions of brightness in different contexts (in particular, the surrounding colours can affect your perception).

总而言之,这取决于你认为是轻的颜色。

All in all, it depends on which colours you consider "light".

这篇关于Ruby,生成一个随机的十六进制颜色(只有浅色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆