调色板原码(RGB <=> HSL)!

 

    public class HSLColorSetting
    {
        private double _hueValue = 0;
        private double _satValue = 0;
        private double _lumValue = 0;

        public HSLColorSetting Clone()
        {
            HSLColorSetting hsl = new HSLColorSetting();
            hsl.HueValue = _hueValue;
            hsl.SatValue = _satValue;
            hsl.LumValue = _lumValue;
            return hsl;
        }

        private double CheckValue(double value)
        {
            double result = 0;
            if (value > 1)
            {
                result = 1;
            }
            else if (value > 0)
            {
                result = value;
            }
            return result;
        }

        public double HueValue { get { return _hueValue; } set { _hueValue = CheckValue(value); } }
        public double SatValue { get { return _satValue; } set { _satValue = CheckValue(value); } }
        public double LumValue { get { return _lumValue; } set { _lumValue = CheckValue(value); } }

        public static HSLColorSetting CovertFromRGB(Color color)
        {
            HSLColorSetting hsl = new HSLColorSetting();
            hsl.ConverFromRGB(color.R, color.G, color.B);
            return hsl;
        }

        public Color ConvertToRGB()
        {
            double r, g, b;
            double s = SatValue;
            double h = HueValue;
            double l = LumValue;

            if (s == 0)
                r = g = b = l;
            else
            {
                double q = 0, p = 0, tempR = 0, tempG = 0, tempB = 0;
                if (l < 0.5)
                {
                    q = l * (1 + s);
                }
                else
                {
                    q = l + s - (l * s);
                }

                p = 2.0 * l - q;
                tempR = h + 1.0 / 3;
                tempG = h;
                tempB = h - 1.0 / 3;

                r = ToRGB(tempR, q, p);
                g = ToRGB(tempG, q, p);
                b = ToRGB(tempB, q, p);
            }
            r = Convert.ToInt32(r * 255);
            g = Convert.ToInt32(g * 255);
            b = Convert.ToInt32(b * 255);


            return Color.FromArgb(0xff, (byte)r, (byte)g, (byte)b);
        }

        private double ToRGB(double tc, double q, double p)
        {
            if (tc < 0)
            {
                tc += 1;
            }
            if (tc > 1)
            {
                tc -= 1;
            }
            if (tc < 1.0 / 6)
            {
                return p + (q - p) * 6 * tc;
            }
            else if (tc < 1.0 / 2)
            {
                return q;
            }
            else if (tc < 2.0 / 3)
            {
                return p + (q - p) * 6.0 * (2.0 / 3 - tc);
            }
            else
            {
                return p;
            }
        }

        private void ConverFromRGB(byte red, byte green, byte blue)
        {
            double r = 1.0 * red / 255;
            double g = 1.0 * green / 255;
            double b = 1.0 * blue / 255;

            double min, max;
            max = Math.Max(Math.Max(r, g), b);
            min = Math.Min(Math.Min(r, g), b);

            //计算 H 值。
            if (max == min)
            {
                HueValue = 0;
            }
            else if (max == r && g >= b)
            {
                HueValue = (g - b) / (max - min) / 6;
            }
            else if (max == r && g < b)
            {
                HueValue = ((g - b) / (max - min)) / 6 + 1;
            }
            else if (max == g)
            {
                HueValue = ((b - r) / (max - min)) / 6 + 1.0 / 3;
            }
            else if (max == b)
            {
                HueValue = ((r - g) / (max - min)) / 6 + 2.0 / 3;
            }

            //计算 L 值。
            LumValue = (max + min) / 2;
            //计算 S 值。
            if (LumValue == 0 || max == min)
            {
                SatValue = 0;
            }
            else if (LumValue > 0 && LumValue <= 0.5)
            {
                SatValue = (max - min) / (max + min);
            }
            else if (LumValue > 0.5)
            {
                SatValue = (max - min) / (2 - (max + min));
            }

        }
    }

posted @ 2011-11-17 09:28  zhjxiao  阅读(369)  评论(0编辑  收藏  举报