If you want to achieve this task, there are a few key things you should keep in mind. The RGB colorspace might not be the best choice for what you're trying to do - it's recommended to use either HSV or HSL instead. Since most browsers support HSL, let's go with that option.
Take a look at the H channel of the HSL colorspace and you'll find the exact range of colors you need. Blue typically has a hue of around 240° while red is closer to 0°.
To achieve our goal, we need to map the range of [min..max] to the hues of [240..0] (it may seem backwards but it works). Let's create a function that will handle this mapping and provide us with a valid color string.
function calcColor(min, max, val)
{
var minHue = 240, maxHue=0;
var curPercent = (val - min) / (max-min);
var colString = "hsl(" + ((curPercent * (maxHue-minHue) ) + minHue) + ",100%,50%)";
return colString;
}
We establish the two Hues end-points we want to work with first. Then, determine where the current value falls within the range. Finally, translate that position to the corresponding Hue range rather than keeping it in the user-input range.
An example demonstrating its usage is provided below.
<!DOCTYPE html>
<html>
<head>
<script>
"use strict";
function newEl(tag){return document.createElement(tag);}
window.addEventListener('load', onDocLoaded, false);
function onDocLoaded()
{
for (var i=0; i<10; i++)
{
var elem = newEl('div');
elem.style.backgroundColor = calcColor(0, 9, i);
elem.className = "rgb";
document.body.appendChild(elem);
}
}
function calcColor(min, max, val)
{
var minHue = 240, maxHue=0;
var curPercent = (val - min) / (max-min);
var colString = "hsl(" + ((curPercent * (maxHue-minHue) ) + minHue) + ",100%,50%)";
return colString;
}
</script>
<style>
.rgb
{
width: 16px;
height: 16px;
display: inline-block;
}
</style>
</head>
<body>
</body>
</html>