Calculate Color Values from Green to Red

Calculate color values from green to red

You could indeed go for the solution provided by @KamilT. Disadvantage of this method (imo) is that the colors in the middle (around 50) will get brownish and not very nice compared to your full red and green.

I think it would be much nicer to follow the color spectrum, and passing over orange and yellow, in stead of that ugly brownish.

This can easily by achieved by working with HSL values rather then RGB values. If you set the Hue value based on your number between 0 and 100 to a value between 0°(red) and 120°(green), and keep your Saturation at 100% and your Lightness at 50%, you should get nice bright colors.

I found a way to convert between rgb and hsl here: HSL to RGB color conversion

And I wrote a simple function to calculate your rgb value using the conversion function from the answer above:

// convert a number to a color using hsl
function numberToColorHsl(i) {
// as the function expects a value between 0 and 1, and red = 0° and green = 120°
// we convert the input to the appropriate hue value
var hue = i * 1.2 / 360;
// we convert hsl to rgb (saturation 100%, lightness 50%)
var rgb = hslToRgb(hue, 1, .5);
// we format to css value and return
return 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';
}

And I set up a fiddle to demonstrate the diffrences between the HSL method and the RGB method: http://jsfiddle.net/rE6Rk/1/

update a more versatile version:

If you do not want to work with a range from red to green, you can slightly adapt the above method. The value that determines the actual color in a hsl representation is the hue, so that's the one we'll need to calculate.

Sample Image

If you define the range of your hue, by providing the 0 and 1 value as parameters, the calculation of the hue value becomes basic math. Have a look at the updated method:

function percentageToHsl(percentage, hue0, hue1) {
var hue = (percentage * (hue1 - hue0)) + hue0;
return 'hsl(' + hue + ', 100%, 50%)';
}

As you can see I changed the API a bit. The parameters are as follows:

  • percentage: a value between 0 and 1
  • hue0: the hue value of the color you want to get when the percentage is 0
  • hue1: the hue value of the color you want to get when the percentage is 1

Also, there is no longer a need to calculate the rgb value, modern browsers support hsl values as is.

So now you can use the method as follows:

// green(120) to red(0)
color = percentageToHsl(perc, 120, 0);

// blue(225) to pink(315)
color = percentageToHsl(perc, 225, 315);

// blue (225) to yellow(45 + 360)
color = percentageToHsl(perc, 225, 405);

So if you want to go clockwise you have to make hue0 < hue1. If you want to go counter clockwise you have to make hue0 > hue1. And since these are degrees, you can just add or subtract 360 to force the direction. You can even use this technique to wrap around the hue circle multiple times.

I've created a new fiddle to demonstrate: https://jsfiddle.net/r438s65s/

from green to red color depend on percentage

This may be more than you need, but this lets you set up any arbitrary color map:

var percentColors = [
{ pct: 0.0, color: { r: 0xff, g: 0x00, b: 0 } },
{ pct: 0.5, color: { r: 0xff, g: 0xff, b: 0 } },
{ pct: 1.0, color: { r: 0x00, g: 0xff, b: 0 } } ];

var getColorForPercentage = function(pct) {
for (var i = 1; i < percentColors.length - 1; i++) {
if (pct < percentColors[i].pct) {
break;
}
}
var lower = percentColors[i - 1];
var upper = percentColors[i];
var range = upper.pct - lower.pct;
var rangePct = (pct - lower.pct) / range;
var pctLower = 1 - rangePct;
var pctUpper = rangePct;
var color = {
r: Math.floor(lower.color.r * pctLower + upper.color.r * pctUpper),
g: Math.floor(lower.color.g * pctLower + upper.color.g * pctUpper),
b: Math.floor(lower.color.b * pctLower + upper.color.b * pctUpper)
};
return 'rgb(' + [color.r, color.g, color.b].join(',') + ')';
// or output as hex if preferred
};

Generate colors between red and green for a power meter?

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:

http://i38.tinypic.com/29o0q4k.jpg

Algorithm: How do I fade from Red to Green via Yellow using RGB values?

The RGB values for the colors:

  • Red 255, 0, 0
  • Yellow 255, 255, 0
  • Green 0, 255, 0

Between Red and Yellow, equally space your additions to the green channel until it reaches 255. Between Yellow and Green, equally space your subtractions from the red channel.

Calculate value based on amount of green against blue and red

I worked out a way to do this correctly.

please review the code below:

var color = parseInt(colorString);
var fitness:Number = 0;
var red:int = (color>> 16) & 0xFF;
var green:int = (color>> 8) & 0xFF;
var blue:int = color & 0xFF;
var greenPercentage = (green/255)*100;
var redAndBluePercentage = ((red+blue)/510)*100;
fitness = greenPercentage-redAndBluePercentage;
fitness = (fitness+100)/2;
trace(fitness);

Maths works like this:
take your color separate it into Red, Green and Blue.

Take the percentage of green in the colour. (x/255)

take the percentage of red and blue in the colour (x/510)

subtract your red and blue percentage from your green.

add some maths to it to give you a possible score from 0-100.

colorFitness("0xFF00FF"); //Outputs 0
colorFitness("0x00FF00"); //Outputs 100

Determine which color (red, blue, green or other) would be visible for a given RGB value combination?

If you define "being color-ish" as:

  • the color value is above 100
  • the color value is at least twice as the 2 other values

    • your current code states "at least half of the 2 other values" but this can't work because if you have "R:199 G:101 B:102", you could say that it is green-ish because G is above 100 and more than half 199 and half 102 (when it's obviously red-ish)

then your code looks almost good (just replace / with *).

I would just use an enum for the result:

enum Ish
{
Other, // 0 so this would be false if you convert it to bool
Red, // 1
Green, // 2
Blue // 3
}
if (R>100 && R>G*2 && R>B*2)
return Ish.Red;
if (G>100 && G>R*2 && G>B*2) // you had G>G/2 here /!\
return Ish.Green;
if (B>100 && B>G*2 && B>R*2)
return Ish.Blue;
return Ish.Other;

I used 2 for twice, but I think you can use other values as long as it is >1 (you can't say blue is dominant if B <= R for example)

This would be the redish values possible for R=100 (left image with factor 2, right image with factor 1):

Red-ish 100 fact 2 Red-ish 100 fact 1

And this for R=200 (left image with factor 2, right image with factor 1):

Red-ish 200 fact 2 Red-ish 200 fact 1

Therefore you could probably use a factor between 1 and 2

For R=200 you can see the red-ish colors depending on the factor below:

Red-ish 200 multifactor

Shifting the color value based on percentage from green to red using PyGame

0.5 correspond to (255, 255, 0), and a value of 1.0 correspond to (0, 255, 0) (255, 255, 0), and a value of 1.0 correspond to (0, 255, 0)

Therefore the green color component is min(255, pct_diff*2 * 255) and the red color component is min(255, pct*2 * 255):

pct_diff = 1.0 - pct
red_color = min(255, pct_diff*2 * 255)
green_color = min(255, pct*2 * 255)
col = (red_color, green_color, 0)

Sample Image

Calculate the color at a given point on a gradient between two colors?

You simply linearly interpolate the red, the green, and the blue channels like this:

double resultRed = color1.red + percent * (color2.red - color1.red);
double resultGreen = color1.green + percent * (color2.green - color1.green);
double resultBlue = color1.blue + percent * (color2.blue - color1.blue);

where percent is a value between 0 and 1 (location in your first method prototype).

Calculating color value (r,g,b) using javascript

Another option would be to simple calculate the hsl value, since you already know the exact colors you are dealing with. Converting from hsl to rgb should not be to hard, there are plenty of libraries out there that do that very well.

Here is an example.

var speeds = [0.5, 0.5, 0.75, 1.25, 0.50, 0.75, 1.00, 4.00, 4.50, 8.00, 7.50, 8.50, 6.50, 6.00, 5.00, 5.25, 4.75, 4.00, 3.25, 2.50, 1.25, 0.00];var fragment = document.createDocumentFragment();var list = document.querySelector('ul');var speedsMin = Math.min(...speeds);var speedsMax = Math.max(...speeds);var hslMin = 0;var hslMax = 240;
var hslValues = speeds.map(function(value) { return { h: Math.ceil( ( (value - speedsMin) / (speedsMax - speedsMin) ) * (hslMax - hslMin) + hslMin ), s: 100, l: 50 }})
hslValues.forEach(function(value) { var item = document.createElement('li'); var color = 'hsl(' + value.h + ',' + value.s + '%,' + value.l + '%)'; item.style.backgroundColor = color; fragment.appendChild(item)})
list.appendChild(fragment)
ul {  list-style-type: none  margin: 0;  padding: 0;}
ul li { width: 20px; height: 20px; border-radius: 10px; display: inline-block; margin: 0 4px}
<ul></ul>


Related Topics



Leave a reply



Submit