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);
        }
Example #2
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)));
        }