zoukankan      html  css  js  c++  java
  • (zt)RGB、HSV色彩空间模式的互相转换

    转自:http://www.cnitblog.com/edaiqingwa/archive/2007/12/11/35643.html
    在开发有关bitmap方面的程序时,经常需要将位图的颜色在RGB和HSV色彩空间之间转换,该颜色转换由C++实现:

    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;
        }
    }

  • 相关阅读:
    integer和double的比较.
    查看mysql版本的四种方法
    codefoces 1397D Stoned Game
    最小生成树集合合并
    codefoces 1400B RPG Protagonist
    codefoces 1400B RPG Protagonist
    牛客挑战赛42 小睿睿的伤害
    病毒扩散 排列组合
    Dus on tree 月出皎兮,佼人僚兮。
    msc的宠物 二分+树形DP
  • 原文地址:https://www.cnblogs.com/gamesacer/p/1088344.html
Copyright © 2011-2022 走看看