首页 > 代码库 > RGB空间与HSV空间的相互转换(C++实现,修正网上大多数的代码错误)

RGB空间与HSV空间的相互转换(C++实现,修正网上大多数的代码错误)

void Rgb2Hsv(float R, float G, float B, float& H, float& S, float&V){     // r,g,b values are from 0 to 1    // h = [0,360], s = [0,1], v = [0,1]    // if s == 0, then h = -1 (undefined)    float min, max, delta,tmp;    tmp = R>G?G:R;    min = tmp>B?B:tmp;    tmp = R>G?R:G;    max = tmp>B?tmp:B;    V = max; // v    delta = max - min;    if( max != 0 )        S = delta / max; // s    else    {        // r = g = b = 0 // s = 0, v is undefined        S = 0;        H = 0;        return;    }    if (delta == 0){        H = 0;        return;    }    else if(R == max){        if (G >= B)            H = (G - B) / delta; // between yellow & magenta        else            H = (G - B) / delta + 6.0;    }    else if( G == max )        H = 2.0 + ( B - R ) / delta; // between cyan & yellow    else if (B == max)        H = 4.0 + ( R - G ) / delta; // between magenta & cyan    H *= 60.0; // degrees}

源地址:http://blog.csdn.net/shijun_zhang/article/details/6525388

RGB空间与HSV空间的相互转换(C++实现,修正网上大多数的代码错误)