Exemplo n.º 1
0
        public static HSL FromHSL(int h, byte s, byte l) // establezco el color que representa la clase
        {
            if (h < 0 || h > 360)
            {
                h = 0;
            }
            if (s > 100)
            {
                s = 0;
            }
            if (l > 100)
            {
                l = 0;
            }

            HSL hsl = new HSL(h, s, l);

            return(hsl);
        }
Exemplo n.º 2
0
        public static HSL ToHsl(this Color color) // paso de color rgb a hsl
        {
            double double_l, double_s, double_h;
            byte   l, s;
            int    h;
            double r = color.R / 255.0;
            double g = color.G / 255.0;
            double b = color.B / 255.0;

            double max = r;

            if (max < g)
            {
                max = g;
            }
            if (max < b)
            {
                max = b;
            }

            double min = r;

            if (min > g)
            {
                min = g;
            }
            if (min > b)
            {
                min = b;
            }

            double diff = max - min;

            double_l = (max + min) / 2;
            if (Math.Abs(diff) < 0)
            {
                double_h = 0;
                double_s = 0;
            }
            else
            {
                if (double_l <= 0.5)
                {
                    double_s = diff / (max + min);
                }
                else
                {
                    double_s = diff / (2 - max - min);
                }

                double r_dist = (max - r) / diff;
                double g_dist = (max - g) / diff;
                double b_dist = (max - b) / diff;
                if (r == max)
                {
                    double_h = b_dist - g_dist;
                }
                else if (g == max)
                {
                    double_h = 2 + r_dist - b_dist;
                }
                else
                {
                    double_h = 4 + g_dist - r_dist;
                }

                double_h = double_h * 60;
                if (double_h < 0)
                {
                    double_h += 360;
                }
            }
            h = Convert.ToInt32(double_h);
            s = Convert.ToByte(double_s * 100);
            l = Convert.ToByte(double_l * 100);
            HSL hsl = HSL.FromHSL(h, s, l);

            return(hsl);
        }