线性增加颜色暗度算法
我想用 ruby 编写一个函数,给定 1 到 500 之间的数字,将输出一个 6 位十六进制颜色代码,数字越大,颜色代码线性变暗。这看起来并不难,但我不知道从哪里开始。我怎样才能实现这个?
编辑
色调似乎是一种更可靠的方法。我想给出一个参考颜色,比如绿色的阴影,然后根据输入的数字将其变暗或变亮。
输入:10
输出:颜色代码(rgb 或 HSV),它是参考颜色的浅色输入
:400
输出:颜色代码(rgb 或 HSV)是参考颜色的相当暗的阴影
edit 2
我需要使用 1 到 500 之间的唯一原因是因为这是我必须使用的输入。如果一些接近的数字映射到相同的颜色也没关系。
I want to write a function in ruby that given a number between 1 and 500 will output a 6 digit hex color code that gets linearly darker for higher numbers. This doesn't seem that hard but I'm not sure where to begin. How can I implement this?
edit
Hue seems like a more reliable way to go. I'd like to give a reference color, say a shade of green, and then darken or lighten it based on the input number.
input: 10
output: color code (in rgb or HSV) that is a light shade of the reference color
input: 400
output: color code (in rgb or HSV) that is a fairly dark shade of the reference color
edit 2
The only reason I need to use between 1 and 500 is because that's the input I have to work with. It's alright if some numbers that are close together map to the same color.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
6 位十六进制颜色代码为 RGB。
您想要的是在 HSV 中工作:选择色相和饱和度,并逐渐降低值。
从 HSV 转换为 RGB 以输出颜色。
有关示例,请参阅此处。
The 6 digit hex color code is in RGB.
What you want is to work in HSV: pick a Hue and Saturation, and gradually decrease the Value.
Convert from HSV to RGB to output the color.
See here for an example.
基本线性插值?
Basic linear interpolation?
为什么不直接返回灰度级,#ffffff 到 #000000?无论如何,500 个级别的黑暗并不能真正区分,而灰色则为您提供 256 个级别。
Why not just return a gray level then, #ffffff to #000000? 500 levels of darkness aren't really distinguishable anyway, and grays give you 256 levels.
如果您只想使参考颜色变暗,这很简单。给定您想要的最亮的 R、G、B 颜色,将 3 个值中的每一个乘以(500 输入),然后除以 499。将每个值转换为 2 个十六进制数字,并在它们后面附加一个 #前面。
If you only want to darken your reference color, it's easy. Given an R,G,B color that is the brightest you want to go, multiply each of the 3 values by (500-input) and divide by 499. Convert each of the values to 2 hex digits and append them with a # at the front.