I have a list of values which should be plotted to a map with a certain color.
The plotting to the map is already done, but I need to figure out a way to map the value n
to a color that represents its value.
An example and my solution so far is to normalize the values based on the min
and max
and then assign them to hex color 0
for the lowest and 255
for the highest. This of course limits my self to the grey scale. Here is the code:
$color = ($value / $max) * 255 // (min is zero)
But how to do this if the values should go from blue to red for instance? Is there any common libraries or tools that can solve this? So far I haven't been able to locate any.
This is one-function-only code to convert any number from any given range (let's say [0,20]) to particular color where 0 is red, 10 is yellow, 20 is green. You can use any colors and even use combination of 4 colors, so it's red - yellow - green - blue.
See the end of the gist file, where this function is used.
Gist with the code: Click here
There might be libs to do that. However let's get a short warm up into the general principles. In general you have following options:
$coloridx=array(0=>'#FFFFFF',1=>'#FFEE00',...);
If you include algorithms in your considerations you must understand that there is no
true
orfalse
. It all depends on what you would like to implement. There might be occasions where it makes sense to render variations of green inton=0..10
and then have red to black in everything beyondn>10
. Caps and multipliers help to set accents. Things like that.One way of implementing a linear gradient would be:
I UPDATED the code to add dechex, which feeds back on the comments.
There is I2UI for this.
There is a color range: from "Gray" - to "Green". The span element that has lowest rate value get the "Gray" color, the element with the biggest rate get "Green" color.
Thus, the span that are between edges get the color that has direct ratio to its rate.
Also, call JavaScript i2.emph() after the previous HTML have been loaded.
See demo
Colors values are representations. Numeric colors as well as hexadecimal colors. A "not grayscale" color contains at least 2 different informations: Red value, green value or blue values may be different. Performing operation on its representation gives wrong result. The 'Mapping" must then be performed on each pieces of information. You need to extract red, green and blue values, perform the mapping seperatly, then build the representation of the result color. Here is a quick helper that use a "min color" and "max color", performs mapping on red, green and blue values, according to the "n" value you need to work with, then return the result color in hexadecimal string. It works for any colors or gray scale color as well.
Just specify 2 colors as numeric value or hexadecimal string (without hash!) like this :
The other answer was very usefull. I decided to use JS as it reduces server load. The requirements also changed. The bar has to go from Red to white in the middle and then from white to yellow. And if the value is 0 it should be black. Here is my code for anyone who ever encounters a similar situation.