Include raphaeljs on your page and paste the functions estimateColorForValue()
and toHex()
somewhere in your code. estimateColorForValue(hue, value, darkestValue, brightestValue)
computes a color for some value, interpolating the color by seeing where in the range [darkestValue-brightestValue]
value
is. hue
is in the range [0-1]
: 0.1
for orange-browny, 0.4
for green, 0.8
for pinkish, and many more colors in between. small changes in hue drastically change the visual color.
For example: estimateColorForValue(.1, 15, 1, 20)
, can be explained as, for data ranging 1 through 20, compute color for value 15, in the orangy space.
toHex(estimateColorForValue(.1, 15, 1, 20)) ==> "#cf8415"
Code:
<script src="https://raw.github.com/DmitryBaranovskiy/raphael/master/raphael.js"></script>
<script>
function toHex(hsb) {
return Raphael.hsb2rgb(hsb.h, hsb.s, hsb.b).hex;
}
function estimateColorForValue(hue, value, darkestValue, brightestValue) {
// Constants to determine saturation and brightness
var darkBrightness = 0.6;
var brightBrightness = 1;
var darkSaturation = 0.3;
var brightSaturation = 1;
// Compute saturation and brightness:
var gradient = (value - darkestValue) / (brightestValue - darkestValue);
var saturation = darkSaturation + gradient * (brightSaturation - darkSaturation);
var brightness = darkBrightness + gradient * (brightBrightness - darkBrightness);
return {h: hue, s:saturation, b:brightness};
}
</script>