Exemple #1
0
        /// <summary>
        /// Convert a color using the HSL to RGB.
        /// </summary>
        /// <param name="alpha">The alpha component (0.0-1.0).</param>
        /// <param name="hue">The Hue component (0.0 - 360.0).</param>
        /// <param name="saturation">The saturation component (0.0 - 1.0).</param>
        /// <param name="luminosity">The luminosity component (0.0 - 1.0).</param>
        public static HtmlColor FromHsl(double alpha, double hue, double saturation, double luminosity)
        {
            if (alpha < 0.0 || alpha > 1.0)
            {
                throw new ArgumentOutOfRangeException(nameof(alpha), alpha, "Alpha should be comprised between 0.0 and 1.0");
            }

            if (hue < 0 || hue > 360)
            {
                throw new ArgumentOutOfRangeException(nameof(hue), hue, "Hue should be comprised between 0° and 360°");
            }

            if (saturation < 0 || saturation > 1)
            {
                throw new ArgumentOutOfRangeException(nameof(saturation), saturation, "Saturation should be comprised between 0.0 and 1.0");
            }

            if (luminosity < 0 || luminosity > 1)
            {
                throw new ArgumentOutOfRangeException(nameof(luminosity), luminosity, "Brightness should be comprised between 0.0 and 1.0");
            }

            if (0 == saturation)
            {
                return(HtmlColor.FromArgb(alpha, Convert.ToByte(luminosity * 255),
                                          Convert.ToByte(luminosity * 255), Convert.ToByte(luminosity * 255)));
            }

            double fMax, fMid, fMin;
            int    iSextant;

            if (0.5 < luminosity)
            {
                fMax = luminosity - (luminosity * saturation) + saturation;
                fMin = luminosity + (luminosity * saturation) - saturation;
            }
            else
            {
                fMax = luminosity + (luminosity * saturation);
                fMin = luminosity - (luminosity * saturation);
            }

            iSextant = (int)Math.Floor(hue / 60f);
            if (300f <= hue)
            {
                hue -= 360f;
            }
            hue /= 60f;
            hue -= 2f * (float)Math.Floor(((iSextant + 1f) % 6f) / 2f);
            if (0 == iSextant % 2)
            {
                fMid = hue * (fMax - fMin) + fMin;
            }
            else
            {
                fMid = fMin - hue * (fMax - fMin);
            }

            byte iMax = Convert.ToByte(fMax * 255);
            byte iMid = Convert.ToByte(fMid * 255);
            byte iMin = Convert.ToByte(fMin * 255);

            switch (iSextant)
            {
            case 1:
                return(HtmlColor.FromArgb(alpha, iMid, iMax, iMin));

            case 2:
                return(HtmlColor.FromArgb(alpha, iMin, iMax, iMid));

            case 3:
                return(HtmlColor.FromArgb(alpha, iMin, iMid, iMax));

            case 4:
                return(HtmlColor.FromArgb(alpha, iMid, iMin, iMax));

            case 5:
                return(HtmlColor.FromArgb(alpha, iMax, iMin, iMid));

            default:
                return(HtmlColor.FromArgb(alpha, iMax, iMid, iMin));
            }
        }