HSV与RGB之间的转换

给定在 HSV 中 (h, s, v) 值定义的一个颜色,带有如上的 h,和分别表示饱和度和明度的 s 和 v 变化于 0 到 1 之间,在 RGB 空间中相应的 (r, g, b) 三原色能够计算为:


HSV与RGB之间的转换_第1张图片

对于每一个颜色向量 (r, g, b),


HSV与RGB之间的转换_第2张图片

对应的,Java实现这种转变的代码如下:

/*

 * 想构造一系列平滑过渡的颜色,用HSV颜色空间容易,用RGB较难。

 *

 * 将色彩由HSV空间转换到RGB空间

 *

 * h  颜色      用角度表示,范围:0到360度

 * s  色度      0.0到1.0  0为白色,越高颜色越“纯”

 * v  亮度      0.0到1.0  0为黑色,越高越亮

 */

Color HSVtoRGB(float h /* 0~360 degrees */, float s /* 0 ~ 1.0 */, float v /* 0 ~ 1.0 */ )

{

    float f, p, q, t;

    if( s == 0 ) { // achromatic (grey)

        return makeColor(v,v,v);

    }

    h /= 60;      // sector 0 to 5

    int i = (int) Math.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:

            return makeColor(v,t,p);

        case 1:

            return makeColor(q,v,p);

        case 2:

            return makeColor(p,v,t);

        case 3:

            return makeColor(p,q,v);

        case 4:

            return makeColor(t,p,v);

        default:

            return makeColor(v,p,q);

    }

}

你可能感兴趣的:(HSV与RGB之间的转换)