Javascript: Convert grayscale to color given hue

Viewed 2289

I have a colour in greyscale, rgb(128, 128, 128), and I want to find the colour in a certain hue, 0 (red) that has the same lightness as the grey colour, in Javascript.

I know how to find the lightness of the grey, using ((0.3 * r + 0.59 * g + 0.11 * b)), but I want to know how I could then add a hue to that colour. I want white to become white, light grey to become light red, medium grey to become bright red, dark grey to become dark red, and black to become black etc.. The hue may change.

Currently I've tried this but it gives bright red rather than white when white is given.

// lightness = 0 - 255
// color = {r: 1, g: 0, b: 0}

newcolor = {};
newcolor.r = lightness * color.r;
newcolor.g = lightness * color.g;
newcolor.b = lightness * color.b;
return newcolor;
1 Answers
Related