HSV,HSL颜色表示与RGB的互转

来源:互联网 发布:云计算架构师培训 编辑:程序博客网 时间:2024/05/07 05:30

RGB之外几种常用颜色表达

  • HSL 表示 hue(色相)、saturation(饱和度)、lightness(亮度)
  • HSV 表示 hue(色相)、saturation(饱和度)、value(色调)
  • HSB 表示 hue(色相)、saturation(饱和度)、brightness(明度)

HSV

这里写图片描述

C#实现

//HSV->RGBpublic static Color ColorFromHSV(float h, float s, float v, float a = 1){        if (s == 0)            return new Color(v, v, v, a);        float sector = h / 60;        int i = (int)sector;        float f = sector - i;        float p = v * (1 - s);        float q = v * (1 - s * f);        float t = v * (1 - s * (1 - f));        Color color = new Color(0, 0, 0, a);        switch(i)        {            case 0:                color.r = v;                color.g = t;                color.b = p;                break;            case 1:                color.r = q;                color.g = v;                color.b = p;                break;            case 2:                color.r  = p;                color.g  = v;                color.b  = t;                break;            case 3:                color.r  = p;                color.g  = q;                color.b  = v;                break;            case 4:                color.r  = t;                color.g  = p;                color.b  = v;                break;            default:                color.r  = v;                color.g  = p;                color.b  = q;                break;        }        return color;}//RGB->HSVpublic static void ColorToHSV(Color color, out float h, out float s, out float v){        float min = Mathf.Min(Mathf.Min(color.r, color.g), color.b);        float max = Mathf.Max(Mathf.Max(color.r, color.g), color.b);        float delta = max - min;        v = max;        if (!Mathf.Approximately(max, 0))            s = delta / max;        else{            s = 0;            h = -1;            return;        }        if (Mathf.Approximately(min, max))        {            v = max;            s = 0;            h = -1;            return;        }        if (color.r == max)            h = (color.g - color.b) / delta;        else if (color.g == max)            h = 2 + (color.b - color.r) / delta;                        else            h = 4 + (color.r - color.g) / delta;                        h *= 60;        if (h < 0 )            h += 360;}

CG Shader实现

待补

HSL

这里写图片描述

C#实现

public static Color HSL2RGB(float h, float s, float l, float a = 1){    float r,g,b;    r = l;    g = l;    b = l;    float v = (l <= 0.5f) ? (l * (1 + s)) : (l + s - l * s);    if (v > 0)    {        float m = l + l - v;        float sv = (v - m )/ v;        h *= 6.0;        int sextant = (int)h;        float fract = h - sextant;        float vsf = v * sv * fract;        float mid1 = m + vsf;        float mid2 = v - vsf;        switch (sextant)        {            case 0:                r = v;                g = mid1;                b = m;                break;            case 1:                r = mid2;                g = v;                b = m;                break;            case 2:                r = m;                g = v;                b = mid1;                break;            case 3:                r = m;                g = mid2;                b = v;                break;            case 4:                r = mid1;                g = m;                b = v;                break;            case 5:                r = v;                g = m;                b = mid2;                break;        }    }    Color rgb = new Color(r * 255,g*255,b*255,a);    return rgb;}    // Return H,S,L in range of 0-1public static void RGB2HSL (Color rgb, out float h, out float s, out float l){        float r = rgb.r/255;        float g = rgb.g/255;        float b = rgb.b/255;        h = 0;         s = 0;        l = 0;        float v = Mathf.Max(r,g,b);        float m = Mathf.Min(r,g,b);        l = (m + v) / 2.0;        if (l <= 0)            return;        float vm = v - m;        s = vm;        if (s > 0.0)            s /= (l <= 0.5) ? (v + m ) : (2.0 - v - m);        else            return;        float r2 = (v - r) / vm;        float g2 = (v - g) / vm;        float b2 = (v - b) / vm;        if (r == v)            h = (g == m ? 5.0 + b2 : 1.0 - g2);        else if (g == v)            h = (b == m ? 1.0 + r2 : 3.0 - b2);        else            h = (r == m ? 3.0 + g2 : 5.0 - r2);        h /= 6f;}

CG Shader实现

待补