//RGBToHSL is working fine, as intended.
        public static HSL RGBToHSL(Color RGB)
        {
            HSL localHSL = new HSL();

            double varR = Convert.ToDouble(RGB.R) / 255.0d;
            double varG = Convert.ToDouble(RGB.G) / 255.0d;
            double varB = Convert.ToDouble(RGB.B) / 255.0d;

            double minVal = Math.Min(Math.Min(varR, varG), varB);//Min. value of RGB
            double maxVal = Math.Max(Math.Max(varR, varG), varB);//Max. value of RGB
            double maxDel = maxVal - minVal;//Delta RGB value

            localHSL.L = (maxVal + minVal) / 2.0d;

            if (maxDel == 0.0d)                     //This is a gray, no chroma...
            {
                localHSL.H = 0.0d;                  //HSL results from 0 to 1
                localHSL.S = 0.0d;
            }
            else                                    //Chromatic data...
            {
                if (localHSL.L < 0.5d)
                    localHSL.S = maxDel / (maxVal + minVal);
                else
                    localHSL.S = maxDel / (2.0d - maxVal - minVal);

                double delR = (((maxVal - varR) / 6.0d) + (maxDel / 2.0d)) / maxDel;
                double delG = (((maxVal - varG) / 6.0d) + (maxDel / 2.0d)) / maxDel;
                double delB = (((maxVal - varB) / 6.0d) + (maxDel / 2.0d)) / maxDel;

                if (varR == maxVal)
                    localHSL.H = delB - delG;
                else if (varG == maxVal)
                    localHSL.H = (1.0d / 3.0d) + delR - delB;
                else if (varB == maxVal)
                    localHSL.H = (2.0d / 3.0d) + delG - delR;

                if (localHSL.H < 0.0d)  //Getting Hue between 0 and 1.
                    localHSL.H += 1.0d;
                if (localHSL.H > 1.0d)
                    localHSL.H -= 1.0d;

            }

                return localHSL;
        }
Пример #2
0
        public Color HSLToRGB(HSL hsl) //HSLToRGB working as intended.
        {
            double R;
            double B;
            double G;

            double var_1;
            double var_2;

            if (hsl.S == 0.0d)                       //HSL from 0 to 1
            {
                R = hsl.L * 255.0d;                  //RGB results from 0 to 255
                G = hsl.L * 255.0d;
                B = hsl.L * 255.0d;
            }
            else
            {
                if (hsl.L < 0.5d)
                {
                    var_2 = hsl.L * (1.0d + hsl.S);
                }
                else
                {
                    var_2 = (hsl.L + hsl.S) - (hsl.S * hsl.L);
                }

                var_1 = 2.0d * hsl.L - var_2;

                R = 255.0d * HueToRGB(var_1, var_2, hsl.H + (1.0d / 3.0d));
                G = 255.0d * HueToRGB(var_1, var_2, hsl.H);
                B = 255.0d * HueToRGB(var_1, var_2, hsl.H - (1.0d / 3.0d));
            }
            Color cl = Color.FromArgb(Convert.ToInt32(R), Convert.ToInt32(G), Convert.ToInt32(B));

            return(cl);
        }
Пример #3
0
        public static HSL RGBToHSL(Color RGB)  //RGBToHSL is working fine, as intended.
        {
            HSL localHSL = new HSL();

            double varR = Convert.ToDouble(RGB.R) / 255.0d;
            double varG = Convert.ToDouble(RGB.G) / 255.0d;
            double varB = Convert.ToDouble(RGB.B) / 255.0d;

            double minVal = Math.Min(Math.Min(varR, varG), varB); //Min. value of RGB
            double maxVal = Math.Max(Math.Max(varR, varG), varB); //Max. value of RGB
            double maxDel = maxVal - minVal;                      //Delta RGB value

            localHSL.L = (maxVal + minVal) / 2.0d;

            if (maxDel == 0.0d)                     //This is a gray, no chroma...
            {
                localHSL.H = 0.0d;                  //HSL results from 0 to 1
                localHSL.S = 0.0d;
            }
            else                                    //Chromatic data...
            {
                if (localHSL.L < 0.5d)
                {
                    localHSL.S = maxDel / (maxVal + minVal);
                }
                else
                {
                    localHSL.S = maxDel / (2.0d - maxVal - minVal);
                }


                double delR = (((maxVal - varR) / 6.0d) + (maxDel / 2.0d)) / maxDel;
                double delG = (((maxVal - varG) / 6.0d) + (maxDel / 2.0d)) / maxDel;
                double delB = (((maxVal - varB) / 6.0d) + (maxDel / 2.0d)) / maxDel;

                if (varR == maxVal)
                {
                    localHSL.H = delB - delG;
                }
                else if (varG == maxVal)
                {
                    localHSL.H = (1.0d / 3.0d) + delR - delB;
                }
                else if (varB == maxVal)
                {
                    localHSL.H = (2.0d / 3.0d) + delG - delR;
                }


                if (localHSL.H < 0.0d)  //Getting Hue between 0 and 1.
                {
                    localHSL.H += 1.0d;
                }
                if (localHSL.H > 1.0d)
                {
                    localHSL.H -= 1.0d;
                }
            }



            return(localHSL);
        }
        //HSLToRGB working as intended.
        public Color HSLToRGB(HSL hsl)
        {
            double R;
            double B;
            double G;

            double var_1;
            double var_2;

            if (hsl.S == 0.0d)                       //HSL from 0 to 1
            {
                R = hsl.L * 255.0d;                      //RGB results from 0 to 255
                G = hsl.L * 255.0d;
                B = hsl.L * 255.0d;
            }
            else
            {
                if (hsl.L < 0.5d)
                    var_2 = hsl.L * (1.0d + hsl.S);
                else
                    var_2 = (hsl.L + hsl.S) - (hsl.S * hsl.L);

                var_1 = 2.0d * hsl.L - var_2;

                R = 255.0d * HueToRGB(var_1, var_2, hsl.H + (1.0d / 3.0d));
                G = 255.0d * HueToRGB(var_1, var_2, hsl.H);
                B = 255.0d * HueToRGB(var_1, var_2, hsl.H - (1.0d / 3.0d));
            }
            Color cl = Color.FromArgb(Convert.ToInt32(R), Convert.ToInt32(G), Convert.ToInt32(B));

            return cl;
        }
        //This piece of code is working fine, as intended.
        public HSL calculateTheOppositeHue(HSL normalHSL)
        {
            HSL newHue = normalHSL;

            newHue.H = (newHue.H + 0.5d) % 1.0d;

            return newHue;
        }