Convert from HSV to HSL color space - CSharp System.Drawing

CSharp examples for System.Drawing:Color Convert

Description

Convert from HSV to HSL color space

Demo Code


using System.Windows.Media;
using System.Globalization;
using System;/*from  ww w.  j  a va  2 s. c o m*/

public class Main{
        /// <summary>
        /// Convert from HSV to <see cref="Color"/>
        /// http://en.wikipedia.org/wiki/HSL_color_space
        /// </summary>
        /// <param name="hue">
        /// Hue [0,1]
        /// </param>
        /// <param name="sat">
        /// Saturation [0,1]
        /// </param>
        /// <param name="val">
        /// Value [0,1]
        /// </param>
        /// <returns>
        /// </returns>
        public static Color HsvToColor(double hue, double sat, double val)
        {
            int i;
            double aa, bb, cc, f;
            double r, g, b;
            r = g = b = 0;

            if (sat == 0)
            {
                // Gray scale
                r = g = b = val;
            }
            else
            {
                if (hue == 1.0)
                {
                    hue = 0;
                }

                hue *= 6.0;
                i = (int)Math.Floor(hue);
                f = hue - i;
                aa = val * (1 - sat);
                bb = val * (1 - (sat * f));
                cc = val * (1 - (sat * (1 - f)));
                switch (i)
                {
                    case 0:
                        r = val;
                        g = cc;
                        b = aa;
                        break;

                    case 1:
                        r = bb;
                        g = val;
                        b = aa;
                        break;

                    case 2:
                        r = aa;
                        g = val;
                        b = cc;
                        break;

                    case 3:
                        r = aa;
                        g = bb;
                        b = val;
                        break;

                    case 4:
                        r = cc;
                        g = aa;
                        b = val;
                        break;

                    case 5:
                        r = val;
                        g = aa;
                        b = bb;
                        break;
                }
            }

            return Color.FromRgb((byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
        }
        /// <summary>
        /// Create a color from the specified HSV.
        /// </summary>
        /// <param name="hsv">
        /// The HSV.
        /// </param>
        /// <returns>
        /// A color.
        /// </returns>
        public static Color HsvToColor(double[] hsv)
        {
            if (hsv.Length != 3)
            {
                throw new InvalidOperationException("Wrong length of hsv array.");
            }

            return HsvToColor(hsv[0], hsv[1], hsv[2]);
        }
        /// <summary>
        /// Converts from HSV to a RGB <see cref="Color"/>
        /// </summary>
        /// <param name="hue">
        /// The hue.
        /// </param>
        /// <param name="saturation">
        /// The saturation.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// </returns>
        public static Color HsvToColor(byte hue, byte saturation, byte value)
        {
            double r, g, b;
            double h = hue * 360.0 / 255;
            double s = saturation / 255.0;
            double v = value / 255.0;

            if (s == 0)
            {
                r = v;
                g = v;
                b = v;
            }
            else
            {
                int i;
                double f, p, q, t;

                if (h == 360)
                {
                    h = 0;
                }
                else
                {
                    h = h / 60;
                }

                i = (int)Math.Truncate(h);
                f = h - i;

                p = v * (1.0 - s);
                q = v * (1.0 - (s * f));
                t = v * (1.0 - (s * (1.0 - f)));

                switch (i)
                {
                    case 0:
                        r = v;
                        g = t;
                        b = p;
                        break;

                    case 1:
                        r = q;
                        g = v;
                        b = p;
                        break;

                    case 2:
                        r = p;
                        g = v;
                        b = t;
                        break;

                    case 3:
                        r = p;
                        g = q;
                        b = v;
                        break;

                    case 4:
                        r = t;
                        g = p;
                        b = v;
                        break;

                    default:
                        r = v;
                        g = p;
                        b = q;
                        break;
                }
            }

            return Color.FromArgb(255, (byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
        }
}

Related Tutorials