HSL to RGB - CSharp System.Drawing

CSharp examples for System.Drawing:Color RGB

Description

HSL to RGB

Demo Code


using System.IO;/*  w w  w  .j av  a 2 s .c o m*/
using System.Drawing.Imaging;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        // Given H,S,L in range of 0-1
        // Returns a Color (RGB struct) in range of 0-255
        //code mostly taken from: http://www.geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm
        public static Pixel HSL2RGB(double h, double sl, double l)
        {
            double v;
            double r, g, b;

            r = l;   // default to gray
            g = l;
            b = l;
            v = (l <= 0.5) ? (l * (1.0 + sl)) : (l + sl - l * sl);

            if (v > 0)
            {
                double m;
                double sv;
                int sextant;
                double fract, vsf, mid1, mid2;

                m = l + l - v;
                sv = (v - m) / v;
                h *= 6.0;
                sextant = (int)h;
                fract = 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 new Pixel(Convert.ToInt32(r * 255.0f), Convert.ToInt32(g * 255.0f), Convert.ToInt32(b * 255.0f));
        }
}

Related Tutorials