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:
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
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!
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.
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
If you want an green-yellow-red representation like the accepted answer suggests then take a look at this.
http://jsfiddle.net/0awncw5u/2/