Esempio n. 1
0
        public void HSLtoRGBTest()
        {
            var hsl = new HSL { H = 0.61082024432809767, L = 1.0, S = 0.74901960784313726 };

            var rgb = ColorUtilities.HSLtoRGB(hsl);

            Assert.AreEqual(255, rgb.A);
            Assert.AreEqual(64, rgb.R);
            Assert.AreEqual(128, rgb.G);
            Assert.AreEqual(255, rgb.B);
        }
Esempio n. 2
0
        public void TestProperties()
        {
            var hsl = new HSL { H = 0.5 };
            Assert.AreEqual(0.5, hsl.H);

            hsl.L = 0.5;
            Assert.AreEqual(0.5, hsl.L);

            hsl.S = 0.5;
            Assert.AreEqual(0.5, hsl.S);
        }
Esempio n. 3
0
        public void ValuesShouldNotBeLowerThan0()
        {
            var hsl = new HSL { H = -2.0 };

            Assert.AreEqual(0.0, hsl.H);

            hsl.L = -2.0;
            Assert.AreEqual(0.0, hsl.L);

            hsl.S = -2.0;
            Assert.AreEqual(0.0, hsl.S);
        }
Esempio n. 4
0
        public void ValuesShouldNotBeHigherThan1()
        {
            var hsl = new HSL { H = 2.0 };

            Assert.AreEqual(1.0, hsl.H);

            hsl.L = 2.0;
            Assert.AreEqual(1.0, hsl.L);

            hsl.S = 2.0;
            Assert.AreEqual(1.0, hsl.S);
        }
Esempio n. 5
0
        /// <summary>
        /// Rgs the bto HSL.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <returns>HSL.</returns>
        public static HSL RGBtoHSL(Color c)
        {
            var hsl = new HSL();

            int max, min;

            //Of our RGB values, assign the highest value to Max, and the Smallest to Min
            if (c.R > c.G)
            {
                max = c.R;
                min = c.G;
            }
            else
            {
                max = c.G;
                min = c.R;
            }

            if (c.B > max) max = c.B;
            else if (c.B < min) min = c.B;

            int diff = max - min;

            //Luminance - a.k.a. Brightness - Adobe photoshop uses the logic that the
            //site VBspeed regards (regarded) as too primitive = superior decides the 
            //level of brightness.
            hsl.L = (double)max / 255;

            //Saturation
            if (max == 0) hsl.S = 0; //Protecting from the impossible operation of division by zero.
            else hsl.S = (double)diff / max; //The logic of Adobe Photoshops is this simple.

            //Hue R is situated at the angel of 360 eller noll degrees; 
            //    G vid 120 degrees
            //    B vid 240 degrees
            double q;
            if (diff == 0) q = 0; // Protecting from the impossible operation of division by zero.
            else q = (double)60 / diff;

            if (max == c.R)
            {
                hsl.H = c.G < c.B ? (360 + (q * (c.G - c.B))) / 360 : (q * (c.G - c.B)) / 360;
            }
            else if (max == c.G) hsl.H = (120 + (q * (c.B - c.R))) / 360;
            else if (max == c.B) hsl.H = (240 + (q * (c.R - c.G))) / 360;
            else hsl.H = 0.0;

            return hsl;
        }
Esempio n. 6
0
 /// <summary>
 /// Hses the lto RGB.
 /// </summary>
 /// <param name="hsl">The HSL.</param>
 /// <returns>Color.</returns>
 public static Color HSLtoRGB(HSL hsl)
 {
     return HSLtoRGB(hsl.H, hsl.S, hsl.L);
 }