/// <summary> /// Creates from a given color. /// </summary> /// <param name="color">The color.</param> /// <returns></returns> public static HslColor FromRgbColor( RgbColor color ) { return color.ToHslColor(); }
/// <summary> /// Creates from a given color. /// </summary> /// <param name="color">The color.</param> /// <returns></returns> public static RgbColor FromRgbColor( RgbColor color) { return(new RgbColor(color.Red, color.Green, color.Blue)); }
/// <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 RgbToColor( RgbColor rgb ) { return Color.FromArgb( rgb.Red, rgb.Green, rgb.Blue ); }
/// <summary> /// Converts <see cref="RgbColor"/> to <see cref="HsbColor"/>. /// </summary> /// <param name="rgb">A <see cref="RgbColor"/> object containing the <see cref="RgbColor"/> values that are to /// be converted to <see cref="HsbColor"/> values.</param> /// <returns>A <see cref="HsbColor"/> equivalent.</returns> public static HsbColor RgbToHsb( RgbColor 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 HsbColor( (int)Math.Round( hue ), (int)Math.Round( saturation ), (int)Math.Round( brightness ) ); }
/// <summary> /// Converts <see cref="RgbColor"/> to <see cref="HsbColor"/>. /// </summary> /// <param name="rgb">A <see cref="RgbColor"/> object containing the <see cref="RgbColor"/> values that are to /// be converted to <see cref="HsbColor"/> values.</param> /// <returns>A <see cref="HsbColor"/> equivalent.</returns> public static HsbColor RgbToHsb( RgbColor 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 HsbColor( (int)Math.Round(hue), (int)Math.Round(saturation), (int)Math.Round(brightness))); }
/// <summary> /// RGB to HSL. /// </summary> /// <param name="rgb">The RGB.</param> /// <returns></returns> public static HslColor RgbToHsl( RgbColor rgb ) { float _R = (rgb.Red / 255f); float _G = (rgb.Green / 255f); float _B = (rgb.Blue / 255f); float _Min = Math.Min(Math.Min(_R, _G), _B); float _Max = Math.Max(Math.Max(_R, _G), _B); float _Delta = _Max - _Min; float H = 0; float S = 0; float L = (float)((_Max + _Min) / 2.0f); if (_Delta != 0) { if (L < 0.5f) { S = (float)(_Delta / (_Max + _Min)); } else { S = (float)(_Delta / (2.0f - _Max - _Min)); } if (_R == _Max) { H = (_G - _B) / _Delta; } else if (_G == _Max) { H = 2f + (_B - _R) / _Delta; } else if (_B == _Max) { H = 4f + (_R - _G) / _Delta; } } H = H * 60f; if (H < 0) H += 360; if (H > 360) H = 360; return new HslColor(H, S * 100f, L * 100f); /* 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 HslColor( h * 360.0, s * 100.0, l * 100.0 ); */ }
/// <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 RgbToColor( RgbColor rgb) { return(Color.FromArgb(rgb.Red, rgb.Green, rgb.Blue)); }
/// <summary> /// RGB to HSL. /// </summary> /// <param name="rgb">The RGB.</param> /// <returns></returns> public static HslColor RgbToHsl( RgbColor rgb) { float _R = (rgb.Red / 255f); float _G = (rgb.Green / 255f); float _B = (rgb.Blue / 255f); float _Min = Math.Min(Math.Min(_R, _G), _B); float _Max = Math.Max(Math.Max(_R, _G), _B); float _Delta = _Max - _Min; float H = 0; float S = 0; float L = (float)((_Max + _Min) / 2.0f); if (_Delta != 0) { if (L < 0.5f) { S = (float)(_Delta / (_Max + _Min)); } else { S = (float)(_Delta / (2.0f - _Max - _Min)); } if (_R == _Max) { H = (_G - _B) / _Delta; } else if (_G == _Max) { H = 2f + (_B - _R) / _Delta; } else if (_B == _Max) { H = 4f + (_R - _G) / _Delta; } } H = H * 60f; if (H < 0) { H += 360; } if (H > 360) { H = 360; } return(new HslColor(H, S * 100f, L * 100f)); /* * 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 HslColor( * h * 360.0, * s * 100.0, * l * 100.0 ); */ }
/// <summary> /// Creates from a given color. /// </summary> /// <param name="color">The color.</param> /// <returns></returns> public static HsbColor FromRgbColor( RgbColor color) { return(color.ToHsbColor()); }
/// <summary> /// Creates from a given color. /// </summary> /// <param name="color">The color.</param> /// <returns></returns> public static RgbColor FromRgbColor( RgbColor color ) { return new RgbColor( color.Red, color.Green, color.Blue ); }
/// <summary> /// RGB to HSL. /// </summary> /// <param name="rgb">The RGB.</param> /// <returns></returns> public static HslColor RgbToHsl( RgbColor 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 HslColor( h * 360.0, s * 100.0, l * 100.0)); }