博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
调色板原码(RGB <=> HSL)!
阅读量:6981 次
发布时间:2019-06-27

本文共 2616 字,大约阅读时间需要 8 分钟。

 

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

        }

    }

转载于:https://www.cnblogs.com/zhjxiao/archive/2011/11/17/2252135.html

你可能感兴趣的文章
MySQL编码引发的两个问题
查看>>
Mysql——外键
查看>>
高级特性-多线程,GUI
查看>>
高可用集群之分布式文件系统
查看>>
LINUX内核升级
查看>>
将视觉深度学习模型应用于非视觉领域
查看>>
连接centos7桌面方法
查看>>
eclipse设置
查看>>
第二课 , 启动 ./start-all.sh
查看>>
linux下jboss的安装配置
查看>>
运维日志管理系统
查看>>
fragment 横竖屏 不重建
查看>>
Oracle数据库文件坏块损坏的恢复方法
查看>>
Watir-webdriver处理table
查看>>
我的友情链接
查看>>
docker 数据卷与容器卷
查看>>
2011-11-27
查看>>
linux下挂载硬盘
查看>>
C 语言 和 C++语言的对比学习 二 数据类型
查看>>
1.0 封装后的布局成本
查看>>