Example #1
0
        public static HSB HSL2HSB(HSL hsl)
        {
            HSB hsb;

            hsb.H = (byte)(255.0 * hsl.H);
            hsb.S = (byte)(255.0 * hsl.S);
            if ((0.1793 <= hsl.L) && (hsl.L <= 0.9821))
            {
                hsb.B = (byte)(257.7 + 149.9 * Math.Log(hsl.L));
            }
            else
            {
                hsb.B = 0;
            }

            return(hsb);
        }
        public static HSB HSL2HSB(HSL hsl)
        {
            HSB hsb;

            hsb.H = (byte)(255.0 * hsl.H);
            hsb.S = (byte)(255.0 * hsl.S);
            if ((0.1793 <= hsl.L) && (hsl.L <= 0.9821))
            {
                hsb.B = (byte)(257.7 + 149.9 * Math.Log(hsl.L));
            }
            else
            {
                hsb.B = 0;
            }

            return hsb;
        }
Example #3
0
        public static HSB HSLToHSB(HSL hsl)
        {
            HSB hsb;

            hsb.H = (byte)Math.Round(255.0 * hsl.H);
            hsb.S = (byte)Math.Round(255.0 * hsl.S);
            if (hsl.L < 0.1793)
            {
                hsb.B = 0;
            }
            else if (hsl.L > 0.9821)
            {
                hsb.B = 0xff;
            }
            else
            {
                hsb.B = (byte)Math.Round(257.7 + 149.9 * Math.Log(hsl.L));
            }

            return(hsb);
        }
Example #4
0
        // based on http://www.geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm
        // Given H,S,L in range of 0-1
        // Returns a Color (RGB struct) in range of 0-255
        public static Color HSL2RGB(HSL hsl)
        {
            double v;
            double r, g, b;

            r = hsl.L;   // default to gray
            g = hsl.L;
            b = hsl.L;
            v = (hsl.L <= 0.5) ? (hsl.L * (1.0 + hsl.S)) : (hsl.L + hsl.S - hsl.L * hsl.S);
            if (v > 0)
            {
                double m;
                double sv;
                int    sextant;
                double fract, vsf, mid1, mid2;

                m       = hsl.L + hsl.L - v;
                sv      = (v - m) / v;
                hsl.H  *= 6.0;
                sextant = (int)hsl.H;
                fract   = hsl.H - sextant;
                vsf     = v * sv * fract;
                mid1    = m + vsf;
                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;
                }
            }

            return(Color.FromArgb(Convert.ToByte(r * 255.0f), Convert.ToByte(g * 255.0f), Convert.ToByte(b * 255.0f)));
        }
        //Conversion from W3C
        //H [0..360], S [0..1], L [0..1]
        //same conversion like Microsoft .NET Color.Hue, Color.Saturation, Color.Brightness
        static HSL RGBToHsl(byte r, byte g, byte b)
        {
            double min, max, l, s, maxcolor, h = 0;
            int    i;

            double[] rgb = new double[3];
            HSL      hsl = new HSL();

            rgb[0]   = r / 255.0;
            rgb[1]   = g / 255.0;
            rgb[2]   = b / 255.0;
            min      = rgb[0];
            max      = rgb[0];
            maxcolor = 0;
            for (i = 0; i < rgb.Length - 1; i++)
            {
                if (rgb[i + 1] <= min)
                {
                    min = rgb[i + 1];
                }
                if (rgb[i + 1] >= max)
                {
                    max = rgb[i + 1]; maxcolor = i + 1;
                }
            }
            if (r == g && g == b)
            {
                h = 0;
            }
            else
            {
                if (maxcolor == 0)
                {
                    h = (rgb[1] - rgb[2]) / (max - min);
                }
                if (maxcolor == 1)
                {
                    h = 2 + (rgb[2] - rgb[0]) / (max - min);
                }
                if (maxcolor == 2)
                {
                    h = 4 + (rgb[0] - rgb[1]) / (max - min);
                }

                if (double.NaN == h)
                {
                    h = 0;
                }
                h = h * 60;
                if (h < 0)
                {
                    h = h + 360;
                }
            }
            l = (min + max) / 2;
            if (min == max)
            {
                s = 0;
            }
            else
            {
                if (l < 0.5)
                {
                    s = (max - min) / (max + min);
                }
                else
                {
                    s = (max - min) / (2 - max - min);
                }
            }
            hsl.H = h; hsl.S = s; hsl.L = l;
            return(hsl);
        }
        // based on http://www.geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm
        // Given H,S,L in range of 0-1
        // Returns a Color (RGB struct) in range of 0-255
        public static Color HSL2RGB(HSL hsl)
        {
            double v;
            double r, g, b;

            r = hsl.L;   // default to gray
            g = hsl.L;
            b = hsl.L;
            v = (hsl.L <= 0.5) ? (hsl.L * (1.0 + hsl.S)) : (hsl.L + hsl.S - hsl.L * hsl.S);
            if (v > 0)
            {
                double m;
                double sv;
                int sextant;
                double fract, vsf, mid1, mid2;

                m = hsl.L + hsl.L - v;
                sv = (v - m) / v;
                hsl.H *= 6.0;
                sextant = (int)hsl.H;
                fract = hsl.H - sextant;
                vsf = v * sv * fract;
                mid1 = m + vsf;
                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;
                }
            }

            return Color.FromArgb(Convert.ToByte(r * 255.0f), Convert.ToByte(g * 255.0f), Convert.ToByte(b * 255.0f));
        }