HSV (0 .. 255) to RGB (0 .. 255) [duplicate]

2019-08-02 08:24发布

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++.

1条回答
相关推荐>>
2楼-- · 2019-08-02 09:09

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.

查看更多
登录 后发表回答