I'm looking for tips to pick a color from a variable value.

I've got a color scale like this:

As you can see, I've got 3 main colors.

The goal is to convert a variable value to a hexadecimal color, using php.

So in this example (lets name the variable x) :

- if x < -2, return "#FF0000"
- if x > 2, return "#00FF00"
- else estimate the color according to the above scale

-------------Problems Reply------------

This can be done using some simple PHP code.

First we want to find the hex value between FF and 22. If we work in decimals the range is 255 - 31. So when x=0 we want a value of 34 and when x=2 we want a value of 255. To do this we simply take (((255-34)/2)*x)+34 = (110.5*x)+34.

If the variable x is below 0 then the hex number has 4 zeros added to the end, if it is above 0 then it has 2 zeros added either side.

The final PHP code for the above logic is:

`<?php`

```
```//Calculate the dec value from 22 to FF

$colourDec = (110.5 * abs($x)) + 34;

$colourDec = min($colourDec, 255);

//Convert to hex

$colourHex = dechex($colourDec);

if ($x<0)

$colourCode = "#".$colourHex."0000";

else if ($x>0)

$colourCode = "#00".$colourHex."00";

else

$colourCode = "#222222";

`?>`

There is probably a much more elegant solution, but this one should work.

I used some functions found here.

`<?`

function rgb2hex($rgb) {

$hex = "#";

$hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);

$hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);

$hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);

```
```return $hex; // returns the hex value including the number sign (#)

}

function hex2rgb($hex) {

$hex = str_replace("#", "", $hex);

if(strlen($hex) == 3) {

$r = hexdec(substr($hex,0,1).substr($hex,0,1));

$g = hexdec(substr($hex,1,1).substr($hex,1,1));

$b = hexdec(substr($hex,2,1).substr($hex,2,1));

} else {

$r = hexdec(substr($hex,0,2));

$g = hexdec(substr($hex,2,2));

$b = hexdec(substr($hex,4,2));

}

$rgb = array($r, $g, $b);

//return implode(",", $rgb); // returns the rgb values separated by commas

return $rgb; // returns an array with the rgb values

}

//Your limit values and HEX colors

$min = -2;

$max = 2;

$min_hex = "#FF0000";

$max_hex = "#00FF00";

//Your selection between the limits

$sel = -0.7;

//Calculations

$interval = $max - $min;

$min_rgb = hex2rgb($min_hex);

$max_rgb = hex2rgb($max_hex);

$min_rgb_R = $min_rgb[0];

$min_rgb_G = $min_rgb[1];

$min_rgb_B = $min_rgb[2];

$max_rgb_R = $max_rgb[0];

$max_rgb_G = $max_rgb[1];

$max_rgb_B = $max_rgb[2];

$new_rgb_R = $max_rgb_R - ((($max_rgb_R - $min_rgb_R)/$interval)*($max - $sel));

$new_rgb_G = $max_rgb_G - ((($max_rgb_G - $min_rgb_G)/$interval)*($max - $sel));

$new_rgb_B = $max_rgb_B - ((($max_rgb_B - $min_rgb_B)/$interval)*($max - $sel));

$new_rgb = array($new_rgb_R,$new_rgb_G,$new_rgb_B);

$new_hex = rgb2hex($new_rgb);

//Show results

echo "<div style='background-color:$min_hex'>($min) $min_hex</div>";

echo "<div style='background-color:$new_hex'>($sel) $new_hex</div>";

echo "<div style='background-color:$max_hex'>($max) $max_hex</div>";

`?>`