Exemple #1
0
        public Rgb ToRgb()
        {
            // Using formula from https://www.easyrgb.com/en/math.php

            var x1 = X / 100.0;
            var y1 = Y / 100.0;
            var z1 = Z / 100.0;

            var r1 = x1 * 3.2406 + y1 * -1.5372 + z1 * -0.4986;
            var g1 = x1 * -0.9689 + y1 * 1.8758 + z1 * 0.0415;
            var b1 = x1 * 0.0557 + y1 * -0.2040 + z1 * 1.0570;

            if (r1 > 0.0031308)
            {
                r1 = 1.055 * Math.Pow(r1, 1.0 / 2.4) - 0.055;
            }
            else
            {
                r1 = 12.92 * r1;
            }

            if (g1 > 0.0031308)
            {
                g1 = 1.055 * Math.Pow(g1, 1.0 / 2.4) - 0.055;
            }
            else
            {
                g1 = 12.92 * g1;
            }

            if (b1 > 0.0031308)
            {
                b1 = 1.055 * Math.Pow(b1, 1.0 / 2.4) - 0.055;
            }
            else
            {
                b1 = 12.92 * b1;
            }

            return(new Rgb(Rgb.ClampedComponent(r1), Rgb.ClampedComponent(g1), Rgb.ClampedComponent(b1)));
        }
Exemple #2
0
        public Rgb ToRgb()
        {
            // Formula from https://www.rapidtables.com/convert/color/hsl-to-rgb.html

            var hue = ScaledHue;
            var sat = Saturation;
            var lum = Luminance;

            // 360deg is actually 0deg (Red)
            if (hue >= 360)
            {
                hue = 0;
            }

            var C = (1 - Math.Abs(2 * lum - 1)) * sat;
            var X = C * (1 - Math.Abs(hue / 60 % 2 - 1));
            var m = lum - C / 2;

            var r1 = 0.0;
            var g1 = 0.0;
            var b1 = 0.0;

            if (hue >= 0 && hue < 60)
            {
                r1 = C;
                g1 = X;
                b1 = 0;
            }
            else if (hue >= 60 && hue < 120)
            {
                r1 = X;
                g1 = C;
                b1 = 0;
            }
            else if (hue >= 120 && hue < 180)
            {
                r1 = 0;
                g1 = C;
                b1 = X;
            }
            else if (hue >= 180 && hue < 240)
            {
                r1 = 0;
                g1 = X;
                b1 = C;
            }
            else if (hue >= 240 && hue < 300)
            {
                r1 = X;
                g1 = 0;
                b1 = C;
            }
            else if (hue >= 300 && hue < 360)
            {
                r1 = C;
                g1 = 0;
                b1 = X;
            }

            return(new Rgb(Rgb.ClampedComponent(r1 + m), Rgb.ClampedComponent(g1 + m), Rgb.ClampedComponent(b1 + m)));
        }