为功率计生成红色和绿色之间的颜色?
我正在编写一个 Java 游戏,我想实现一个功率计来测量您射击某物的力度。
我需要编写一个函数,它接受 0 - 100 之间的整数,并根据该数字的高低,它将返回绿色(功率刻度上的 0)和红色(功率刻度上的 100)之间的颜色。
与音量控制的工作原理类似:
我需要对颜色的红、绿、蓝分量进行什么操作才能生成之间的颜色绿色和红色?
因此,我可以运行 getColor(80)
,它将返回橙色(其值在 R、G、B 中)或 getColor(10)
它将返回更多绿色/黄色 RGB 值。
我知道我需要增加新颜色的 R、G、B 值的分量,但我不知道当颜色从绿红转变时具体会上升或下降。
进展:
我最终使用了 HSV/HSB 颜色空间,因为我更喜欢渐变(中间没有深棕色)。
我使用的函数是:
public Color getColor(double power)
{
double H = power * 0.4; // Hue (note 0.4 = Green, see huge chart below)
double S = 0.9; // Saturation
double B = 0.9; // Brightness
return Color.getHSBColor((float)H, (float)S, (float)B);
}
其中“power”是 0.0 到 1.0 之间的数字。 0.0 将返回亮红色,1.0 将返回亮绿色。
Java 色调图表:
I'm writing a Java game and I want to implement a power meter for how hard you are going to shoot something.
I need to write a function that takes a int between 0 - 100, and based on how high that number is, it will return a color between Green (0 on the power scale) and Red (100 on the power scale).
Similar to how volume controls work:
What operation do I need to do on the Red, Green, and Blue components of a color to generate the colors between Green and Red?
So, I could run say, getColor(80)
and it will return an orangish color (its values in R, G, B) or getColor(10)
which will return a more Green/Yellow RGB value.
I know I need to increase components of the R, G, B values for a new color, but I don't know specifically what goes up or down as the colors shift from Green-Red.
Progress:
I ended up using HSV/HSB color space because I liked the gradiant better (no dark browns in the middle).
The function I used was:
public Color getColor(double power)
{
double H = power * 0.4; // Hue (note 0.4 = Green, see huge chart below)
double S = 0.9; // Saturation
double B = 0.9; // Brightness
return Color.getHSBColor((float)H, (float)S, (float)B);
}
Where "power" is a number between 0.0 and 1.0. 0.0 will return a bright red, 1.0 will return a bright green.
Java Hue Chart:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
这应该可行 - 只需线性缩放红色和绿色值。 假设您的最大红/绿/蓝值为
255
,并且n
的范围为0 .. 100
(针对整数数学进行了修改,向 Ferrucio 致敬)
另一种方法是使用 HSV 颜色模型,并使用适合您的饱和度和值将色调从
0 度
(红色)到120 度
(绿色)循环。 这应该会产生更令人满意的渐变。以下是每种技术的演示 - 顶部渐变使用 RGB,底部使用 HSV:
This should work - just linearly scale the red and green values. Assuming your max red/green/blue value is
255
, andn
is in range0 .. 100
(Amended for integer maths, tip of the hat to Ferrucio)
Another way to do would be to use a HSV colour model, and cycle the hue from
0 degrees
(red) to120 degrees
(green) with whatever saturation and value suited you. This should give a more pleasing gradient.Here's a demonstration of each technique - top gradient uses RGB, bottom uses HSV:
在我的脑海中,这是 HSV 空间中的绿-红色调过渡,转换为 RGB:
上例中的红、绿、蓝值是百分比,您可能需要将它们乘以 255 以获得最常用的0-255范围。
Off the top of my head, here is the green-red hue transition in HSV space, translated to RGB:
The red, green, blue values in the above example are percentages, you'd probably want to multiply them by 255 to get the most used 0-255 range.
简短的复制粘贴答案...
在 Java Std 上:
在 Android 上:
注意:值是 0 到 1 之间的数字,表示红到绿的状态。
Short Copy'n'Paste answer...
On Java Std:
On Android:
note: value is a number between 0 and 1 indicating the red-to-green condition.
如果您想要像接受的答案所建议的那样的绿-黄-红表示,那么请看一下这个。
http://jsfiddle.net/0awncw5u/2/
If you want an green-yellow-red representation like the accepted answer suggests then take a look at this.
http://jsfiddle.net/0awncw5u/2/
在绿色和红色之间线性插值几乎应该可以工作,除了中间会有浑浊的棕色。
最灵活、最好看的解决方案是在某个位置拥有一个具有您想要的确切色带的图像文件。 然后查找其中的像素值。 这具有灵活性,您可以将渐变调整到恰到好处。
如果您仍然想从代码中执行此操作,那么最好在左侧的绿色和黄色之间以及右侧的黄色和红色之间进行插值。 在RGB空间中,绿色为(R=0,G=255,B=0),黄色为(R=255,G=255,B=0),红色为(R=255,G=0,B=0) ) - 假设每个颜色分量的范围为 0 到 255。
Linearly interpolating between green and red almost should work, except that in the middle there will be muddy brown color.
The most flexible and best looking solution is to have an image file somewhere that has the exact color ramp you want. And then lookup the pixel value in there. This has the flexibility that you can tweak the gradient to be just right.
If you still want to do it from code, then it's probably best to interpolate between green and yellow colors in the left side, and between yellow and red on the right side. In RGB space, green is (R=0, G=255, B=0), yellow is (R=255, G=255, B=0), red is (R=255, G=0, B=0) - this is assuming each color component goes from 0 to 255.
我制作了一个小函数,它为您提供百分比值的 rgb 整数值:
因此,如果您的 currentValue 是 50 并且 maxValue 是 100,则此函数将返回您需要的颜色,因此如果您使用百分比值循环此函数,你的颜色值将从绿色变为红色。 抱歉解释不好
I made a small function wich gives you the rgb integer value for a percentage value:
So if your currentValue is 50 and your maxValue is 100, this function will return the color that you need, so if you loop this function with a percentage value, your color value will go from green to red. Sorry for the bad explanation
我想说你想要 HSV 空间中的线段(中心有一个稍微太亮的黄色)和 RGB 空间中的线段(中心有一个难看的棕色)之间的东西。 我会使用这个,其中
power = 0
将给出绿色,power = 50
将给出略暗的黄色,而power = 100
将给出红色的。I'd say you want something in between a line segment in the HSV space (which has a slightly-too-bright yellow in the centre) and a line segment in the RGB space (which has an ugly brown in the centre). I would use this, where
power = 0
will give green,power = 50
will give a slightly dull yellow, andpower = 100
will give red.接受的答案甚至没有真正回答问题...
C++
这是我的引擎中的一个简单的 C++ 代码段,它在三种任意颜色之间线性有效地插值:
假设
interpolation_factor
在范围内0.0 ... 1.0
。颜色假定在
0.0 ... 1.0
范围内(例如对于OpenGL)。C#
以下是用 C# 编写的相同函数:
假定
interpolationFactor
的范围为0.0 ... 1.0
。颜色假定在
0 ... 255
范围内。The accepted answer didn't even really answer the question...
C++
Here's a simple C++ code segment from my engine that linearly and efficiently interpolates between three arbitrary colors:
The
interpolation_factor
is assumed to be in the range of0.0 ... 1.0
.The colors are assumed to be in the range of
0.0 ... 1.0
(e.g. for OpenGL).C#
Here's the same function written in C#:
The
interpolationFactor
is assumed to be in the range of0.0 ... 1.0
.The colors are assumed to be in the range of
0 ... 255
.您需要线性插值 (LERP) 颜色分量。 一般情况下是这样完成的,给定起始值 v0、结束值 v1 和所需的比率(0.0 到 1.0 之间的标准化浮点值) ):
当比率为 0.0 时,给出 v0;当比率为 1.0 时,给出 v1;在其他情况下,给出介于两者之间的所有值。
您可以在 RGB 分量上执行此操作,也可以使用其他一些配色方案(例如 HSV 或 HLS)。 后两者在视觉上会更令人愉悦,因为它们处理的色调和亮度成分可以更好地映射我们的颜色感知。
You need to linearly interpolate (LERP) the color components. Here's how it's done in general, given a start value v0, an end value v1 and the required ratio (a normalized float between 0.0 and 1.0):
This gives v0 when ratio is 0.0, v1 when ratio is 1.0, and everything between in the other cases.
You can do this either on the RGB components, or using some other color scheme, like HSV or HLS. The latter two will be more visually pleasing, since they work on hue and brightness compoments that map better to our color perception.
以下是 Swift 和 HSV 比例的复制粘贴解决方案:
UIColor 初始值设定项接受 [0, 1] 中的色调、饱和度和亮度,因此对于来自 [0, 1] 的给定值,我们有:
Here is copy-and-paste solution for Swift and HSV scale:
UIColor initializer accepts hue, saturation and brightness in [0, 1] so for given value from [0, 1] we have:
如果您在 CSS(最低版本 2.1)中需要这种渐变(实际上是反向的),您也可以使用 HSL。
假设您在 AngularJs 模板中,值是从 0 到 1 的数字,并且您想要设置元素的样式(背景或文本颜色)...
第一个值:度数(0 红色,120 绿色)
第二个值:饱和度(50%为中性)
第三个值:亮度(50% 中性)
此处 一篇不错的
文章维基百科此处
If you need this kind of gradient (actually reversed) in CSS (min version 2.1) you can use HSL, too.
Supposing you are in an AngularJs template and value is a number from 0 to 1 and you want to style an element (background or text color)...
First value: degrees ( 0 red, 120 green)
Second value: saturation (50% is neutral)
Third value: lightness (50% neutral)
A nice article here
Theory on Wikipedia here
以下是 Simucal 解决方案的
Objective-C
版本:其中 level 是
0.0
和1.0
之间的值。希望这对某人有帮助!
Here is the
Objective-C
version of Simucal's solution:Where level would be a value between
0.0
and1.0
.Hope this helps someone!
在 Python 2.7 中:
百分比当然是
value / max_value
。 或者,如果您的比例不是从 0 开始,则(value - min_value) / (max_value - min_value)
。In Python 2.7:
Percent is of course
value / max_value
. Or if your scale doesn't begin at 0 then(value - min_value) / (max_value - min_value)
.JavaScript
我正在将 Google Visualization 与条形图结合使用,并希望条形根据百分比从绿色变为红色。 事实证明,这是我发现的最干净的解决方案并且工作完美。
只需确保您的百分比为 50 或 50%,而不是 0.50。
JavaScript
I am using Google Visualization with bar chart and wanted the bars to be green to red based on a percentage. This turned out to be the cleanest solution I found and works perfectly.
Just make sure your percentage is 50 for 50% and not 0.50.
我已经更新了接受的答案,将其分为 (0,100) 范围的前半部分和后半部分,并用最大级别替换 100,以便范围可以变得动态。 结果与 HSV 模型完全相同,但仍使用 RGB。 关键是中间的 (255,255,0) 代表黄色。 我已将这个想法结合在接受的答案和另一个 VBA 代码中,因此在 VBA 中实现它并在 Excel 中使用。 我希望该逻辑有所帮助并且可以在其他语言/应用程序中使用。
干杯,
帕夫林
I have updated the accepted answer by spliting it into first and second half of the (0,100) range and substituting 100 by a max level, so that the range can become dynamic. The result is exactly as with the HSV model, but still using RGB. The key is to have the (255,255,0) in the middle to represent the yellow colour. I have combined this idea in the accepted answer and another VBA code, so achieve it in VBA and use in Excel. I hope the logic helps and can be used in other languages/ applications.
Cheers,
Pavlin
VB
VB
自包含示例
Self contained example
颜色会从红色变为黄色,然后变为绿色
值范围为 0 - 100
This will vary from Red to Yellow and then to Green
value varies from 0 - 100