Similar to this (how to increase brightness) I want to change the Hue of a RGB (Hex) Color.
Say changeHue(\"#FF0000\", 40) returns \"#FFAA00\"
If you're not afraid of libraries and a few kb won't ruin your project, you could try sc-color rather than reimplementing the wheel...
Here's a jsfiddle using sc-color. The crux of the code is here:
var c = sc_color("#FF0000").hue(40).hex6();
$("#test").css("background-color", c);
Disclosure: I'm the author of sc-color