Пример #1
0
        /// <summary>
        /// RGB to HSL.
        /// </summary>
        /// <param name="rgb">The RGB.</param>
        /// <returns></returns>
        public static HSLColour RGBToHSL(RGBColour rgb)
        {
            double varR = (rgb.Red / 255.0); //Where RGB values = 0 ÷ 255
            double varG = (rgb.Green / 255.0);
            double varB = (rgb.Blue / 255.0);

            double varMin = GetMinimumValue(varR, varG, varB); //Min. value of RGB
            double varMax = GetMaximumValue(varR, varG, varB); //Max. value of RGB
            double delMax = varMax - varMin;                   //Delta RGB value

            double h;
            double s;
            double l = (varMax + varMin) / 2;

            if (delMax == 0) //This is a gray, no chroma...
            {
                h = 0;       //HSL results = 0 ÷ 1
                       //s = 0;
                       // UK:
                s = 1.0;
            }
            else //Chromatic data...
            {
                if (l < 0.5)
                {
                    s = delMax / (varMax + varMin);
                }
                else
                {
                    s = delMax / (2.0 - varMax - varMin);
                }

                double delR = (((varMax - varR) / 6.0) + (delMax / 2.0)) / delMax;
                double delG = (((varMax - varG) / 6.0) + (delMax / 2.0)) / delMax;
                double delB = (((varMax - varB) / 6.0) + (delMax / 2.0)) / delMax;

                if (varR == varMax)
                {
                    h = delB - delG;
                }
                else if (varG == varMax)
                {
                    h = (1.0 / 3.0) + delR - delB;
                }
                else if (varB == varMax)
                {
                    h = (2.0 / 3.0) + delG - delR;
                }
                else
                {
                    // Uwe Keim.
                    h = 0.0;
                }

                if (h < 0.0)
                {
                    h += 1.0;
                }
                if (h > 1.0)
                {
                    h -= 1.0;
                }
            }

            // --

            return(new HSLColour(h * 360.0, s * 100.0, l * 100.0));
        }
        /// <summary>
        /// Converts <see cref="RgbColor"/> to <see cref="HSBColour"/>.
        /// </summary>
        /// <param name="rgb">A <see cref="RgbColor"/> object containing the <see cref="RgbColor"/> values that are to
        /// be converted to <see cref="HSBColour"/> values.</param>
        /// <returns>A <see cref="HSBColour"/> equivalent.</returns>
        public static HSBColour RGBToHSB(RGBColour rgb)
        {
            // _NOTE #1: Even though we're dealing with a very small range of
            // numbers, the accuracy of all calculations is fairly important.
            // For this reason, I've opted to use double data types instead
            // of float, which gives us a little bit extra precision (recall
            // that precision is the number of significant digits with which
            // the result is expressed).

            double r = rgb.Red / 255d;
            double g = rgb.Green / 255d;
            double b = rgb.Blue / 255d;

            double minValue = GetMinimumValue(r, g, b);
            double maxValue = GetMaximumValue(r, g, b);
            double delta    = maxValue - minValue;

            double hue = 0;
            double saturation;
            double brightness = maxValue * 100;

            if (maxValue == 0 || delta == 0)
            {
                hue        = 0;
                saturation = 0;
            }
            else
            {
                // _NOTE #2: FXCop insists that we avoid testing for floating
                // point equality (CA1902). Instead, we'll perform a series of
                // tests with the help of Double.Epsilon that will provide
                // a more accurate equality evaluation.

                if (minValue == 0)
                {
                    saturation = 100;
                }
                else
                {
                    saturation = (delta / maxValue) * 100;
                }

                if (Math.Abs(r - maxValue) < Double.Epsilon)
                {
                    hue = (g - b) / delta;
                }
                else if (Math.Abs(g - maxValue) < Double.Epsilon)
                {
                    hue = 2 + (b - r) / delta;
                }
                else if (Math.Abs(b - maxValue) < Double.Epsilon)
                {
                    hue = 4 + (r - g) / delta;
                }
            }

            hue *= 60;
            if (hue < 0)
            {
                hue += 360;
            }

            return(new HSBColour((int)Math.Round(hue), (int)Math.Round(saturation), (int)Math.Round(brightness)));
        }
Пример #3
0
 /// <summary>
 /// Creates from a given color.
 /// </summary>
 /// <param name="colour">The color.</param>
 /// <returns></returns>
 public static HSLColour FromRGBColour(RGBColour colour)
 {
     return(colour.ToHSLColour());
 }
 /// <summary>
 /// Converts a <see cref="RgbColor"/> color structure to a Color object.
 /// </summary>
 /// <param name="rgb">A <see cref="RgbColor"/> object representing the color that is to be
 /// converted.</param>
 /// <returns>A Color equivalent.</returns>
 public static Color RGBToColour(RGBColour rgb)
 {
     return(Color.FromArgb(rgb.Red, rgb.Green, rgb.Blue));
 }
 /// <summary>
 /// Creates from a given color.
 /// </summary>
 /// <param name="colour">The color.</param>
 /// <returns></returns>
 public static RGBColour FromRgbColour(RGBColour colour)
 {
     return(new RGBColour(colour.Red, colour.Green, colour.Blue));
 }