非常见颜色模式转RGB模式

 RGB模式是计算机图像学最常用的颜色模式,除此之外,还有其他非常用的颜色模式,以下列举常用颜色模式转RGB的算法

1. CMYK是印刷业用的颜色模式,它是一种减色模式,与RGB加色模式完全不同,有时我们需要把CMYK转成RGB模式

  转换算法:

  r = (1-k)-(1-k)*c;

  g = (1-k)-(1-k)*m;

  b = (1-k)-(1-k)*y;

2. HSB也是一种常见的颜色模式(又称HSV),这个模型中颜色的参数分别是:色调(H),饱和度(S),亮度(B)。色调决定了颜色的名称,如红色色调,S表示色彩的纯度,B表示色彩的明亮度。(以下算法取自Cocos2d-x引擎算法)

RGBA ColorUtil::RGBfromHSV(HSV value)
{
    double      hh, p, q, t, ff;
    long        i;
    RGBA        out;
    out.a        = 1;
    
    if (value.s <= 0.0) // < is bogus, just shuts up warnings
    {       
        if (isnan(value.h)) // value.h == NAN
        {   
            out.r = value.v;
            out.g = value.v;
            out.b = value.v;
            return out;
        }
        
        // error - should never happen
        out.r = 0.0;
        out.g = 0.0;
        out.b = 0.0;
        return out;
    }
    
    hh = value.h;
    if(hh >= 360.0) hh = 0.0;
    hh /= 60.0;
    i = (long)hh;
    ff = hh - i;
    p = value.v * (1.0 - value.s);
    q = value.v * (1.0 - (value.s * ff));
    t = value.v * (1.0 - (value.s * (1.0 - ff)));
    
    switch(i)
    {
        case 0:
            out.r = value.v;
            out.g = t;
            out.b = p;
            break;
        case 1:
            out.r = q;
            out.g = value.v;
            out.b = p;
            break;
        case 2:
            out.r = p;
            out.g = value.v;
            out.b = t;
            break;
            
        case 3:
            out.r = p;
            out.g = q;
            out.b = value.v;
            break;
        case 4:
            out.r = t;
            out.g = p;
            out.b = value.v;
            break;
        case 5:
        default:
            out.r = value.v;
            out.g = p;
            out.b = q;
            break;
    }
    return out;     
}

3。LAB颜色模式是模拟人眼对颜色的感觉,理论上,它比上面两种的颜色范围更广更细致,转换算法如下

double L, a, b;
double X, Y, Z;
double R, G, B;

// Lab -> normalized XYZ (X,Y,Z are all in 0...1)

Y = L * (1.0/116.0) + 16.0/116.0;
X = a * (1.0/500.0) + Y;
Z = b * (-1.0/200.0) + Y;

X = X > 6.0/29.0 ? X * X * X : X * (108.0/841.0) - 432.0/24389.0;
Y = L > 8.0 ? Y * Y * Y : L * (27.0/24389.0);
Z = Z > 6.0/29.0 ? Z * Z * Z : Z * (108.0/841.0) - 432.0/24389.0;

// normalized XYZ -> linear sRGB (in 0...1)

R = X * (1219569.0/395920.0)     + Y * (-608687.0/395920.0)    + Z * (-107481.0/197960.0);
G = X * (-80960619.0/87888100.0) + Y * (82435961.0/43944050.0) + Z * (3976797.0/87888100.0);
B = X * (93813.0/1774030.0)      + Y * (-180961.0/887015.0)    + Z * (107481.0/93370.0);

// linear sRGB -> gamma-compressed sRGB (in 0...1)

R = R > 0.0031308 ? pow(R, 1.0 / 2.4) * 1.055 - 0.055 : R * 12.92;
G = G > 0.0031308 ? pow(G, 1.0 / 2.4) * 1.055 - 0.055 : G * 12.92;
B = B > 0.0031308 ? pow(B, 1.0 / 2.4) * 1.055 - 0.055 : B * 12.92;

 

 

posted @ 2013-05-08 19:54  haroel  阅读(515)  评论(0编辑  收藏  举报