1

Possible Duplicate:
Algorithm to convert RGB to HSV and HSV to RGB?

Can someone explain on how to convert an HSV value to an RGB when the value is 0 to 255? Also,

For reference, I am trying to do this in C++.

Community
  • 1
  • 1
judeclarke
  • 1,106
  • 5
  • 16
  • 31
  • 6
    Which part from http://en.wikipedia.org/wiki/HSL_and_HSV - Converting to RGB you do not understand? Simply reposting here an algorithm would be without sense, please tell us which part is not working / you do not understand it – Marcin Deptuła Nov 21 '11 at 08:16
  • @ Ravadre, I was hoping for something in more "lamens terms". Wikipedia is notorious for presenting formulas in more complicated manner than necessary. – judeclarke Nov 21 '11 at 08:24
  • 1
    @ Kerrek SB, was being rude really necessary? – judeclarke Nov 21 '11 at 08:24
  • @ detunized, Actually, its not. Those are typically (0 ... 1) to (0 .. 255), etc – judeclarke Nov 21 '11 at 08:32
  • @judeclarke: so, scale it: if you have (0 ... 255), simply divide your values by 255 and then they will be in (0 ... 1), then you're good to go to use those algorithms! – Mac Nov 21 '11 at 10:02

1 Answers1

3

Found it here http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript

function hsvToRgb(h, s, v){
    var r, g, b;

    var i = Math.floor(h * 6);
    var f = h * 6 - i;
    var p = v * (1 - s);
    var q = v * (1 - f * s);
    var t = v * (1 - (1 - f) * s);

    switch(i % 6){
        case 0: r = v, g = t, b = p; break;
        case 1: r = q, g = v, b = p; break;
        case 2: r = p, g = v, b = t; break;
        case 3: r = p, g = q, b = v; break;
        case 4: r = t, g = p, b = v; break;
        case 5: r = v, g = p, b = q; break;
    }

    return [r * 255, g * 255, b * 255];
}

Edit:

I once wrote a color picker and used to understand all these. Back then, I looked at photoshop color picker, tried moving around the cross hair, observe the changes in hsv/rgb numbers and figured out how they work. In this case i seems to locate at which point the main color, hue, is pointed it. The value of hue is actually a degree of fully saturated colors in cycle which starts with red and ends with red. There's a triangle with each point representing R,G and B where 0 degree is R. Between R and G, it's Yellow, between R and B, it's Magenta and between B and G, it's cyan. There we have total 6 colors. Those 6 colors are from case 0 through 5.

Moe Sweet
  • 3,683
  • 2
  • 34
  • 46
  • I am sorry, I should have posted that I was hoping for HSV (0 .. 255) to RGB (0 .. 255) and a good explanation of it – judeclarke Nov 21 '11 at 08:29
  • 1
    The output is already in the range 0..255. You can convert the algorithm to have an input range to 0..255 by dividing h,s,v each by 255. But frankly, if you don't see that, you have a bigger problem – Gunther Piez Nov 21 '11 at 08:44
  • @ drhirsch, Being rude isn't helpful. I do not see how the input is (0 .. 255) as there is no explanation in the code. For example, why are the cases like that? What does each case do? – judeclarke Nov 21 '11 at 08:48
  • I you would have had a look at the mentioned wikipedia article, you would have recognized the six cases. And don't tell me I should explain this article in layman terms to you because you don't want to read it - I would consider this rude – Gunther Piez Nov 21 '11 at 09:01
  • @ drhirsch the wiki does not explain each case – judeclarke Nov 21 '11 at 09:34
  • @ Moe Sweet I hope to understand code when I use it – judeclarke Nov 21 '11 at 09:34
  • @ Moe Sweet, are the inputs of type 0 .. 1 or 0 .. 255? – judeclarke Nov 22 '11 at 05:36
  • The reason I ask because with h = 2, sat = 68 and v = 128, the values will be all negative, such as -8576.0 for p (b) – judeclarke Nov 22 '11 at 05:39
  • "Assumes h, s, and v are contained in the set [0, 1] and" It's written in the comments. Have you checked out the link yet? – Moe Sweet Nov 24 '11 at 07:33