RGB转换为HSV的VC++代码

RGB颜色空间转换为HSV空间颜色值:

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 = min(R, G);
     min = min( tmp, B );
     tmp = max( R, G);
     max = max(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 = UNDEFINEDCOLOR;
       return;
     }
     if( R == max )
         H = ( G - B ) / delta; // between yellow & magenta
    else if( G == max )
         H = 2 + ( B - R ) / delta; // between cyan & yellow
    else
         H = 4 + ( R - G ) / delta; // between magenta & cyan

     H *= 60; // degrees
     if( H < 0 )
        H += 360;
}

HSV颜色空间转换为RGB空间颜色值:

void Hsv2Rgb(float H, float S, float V, float &R, float &G, float &B)
{
      int i;
     float f, p, q, t;

     if( S == 0 ) 
     {
     // achromatic (grey)
         R = G = B = V;
         return;
     }

     H /= 60; // sector 0 to 5
     i = floor( H );
     f = H - i; // factorial part of h
     p = V * ( 1 - S );
     q = V * ( 1 - S * f );
     t = V * ( 1 - S * ( 1 - f ) );

     switch( i ) 
     {
     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;
     default: // case 5:
        R = V;
        G = p;
        B = q;
        break;
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值