Generate colors between red and green for a power meter?
Posted
by Simucal
on Stack Overflow
See other posts from Stack Overflow
or by Simucal
Published on 2008-12-04T10:56:25Z
Indexed on
2010/04/10
19:33 UTC
Read the original article
Hit count: 379
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 (in java):
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:
Thanks everyone for helping me with this!
© Stack Overflow or respective owner