为功率计生成红色和绿色之间的颜色? [英] Generate colors between red and green for a power meter?

查看:203
本文介绍了为功率计生成红色和绿色之间的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个Java游戏,我想实现一个功率计,你要多么努力拍摄的东西。



我需要写一个函数,它的int介于0到100之间,根据该数字的高低,它将返回一个绿色



类似于音量控制的工作原理:



我需要对红,绿和蓝进行什么操作组件的颜色生成绿色和红色之间的颜色?



所以,我可以运行说, getColor(80),它将返回一个橘黄色的颜色(其值在R,G,B)或 getColor(10),它将返回更多的绿色/黄色RGB值。 p>

我知道我需要增加一个新颜色的R,G,B值的组件,但是我不知道当颜色从绿色 - 红色。






进度:



使用HSV / HSB颜色空间,因为我喜欢gradiant更好(中间没有深褐色)。



我使用的功能是:

  public Color getColor(double power)
{
double H = power * 0.4; // Hue(note 0.4 = Green,see large chart below)
double S = 0.9; //饱和度
double B = 0.9; //亮度

返回Color.getHSBColor((float)H,(float)S,(float)B);
}

其中power是介于0.0和1.0之间的数字。 0.0将返回亮红色,1.0将返回亮绿色。



Java Hue图表:

解决方案

缩放红色和绿色值。假设您的最大红色/绿色/蓝色值为 255 ,并且 n 在范围 0 .. 100

  R =(255 * n)/ 100 
G = 255 *(100-n))/ 100
B = 0

修改为整数数学,帽子的尖端到Ferrucio)



另一种方法是使用 HSV颜色模型,并将 0度的色相循环(红色)到 120度(绿色),具有适合你的任何饱和度和值。



这里是每种技术的演示 - 顶部渐变使用RGB,底部使用HSV:




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:

解决方案

This should work - just linearly scale the red and green values. Assuming your max red/green/blue value is 255, and n is in range 0 .. 100

R = (255 * n) / 100
G = (255 * (100 - n)) / 100 
B = 0

(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) to 120 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:

这篇关于为功率计生成红色和绿色之间的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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