/// <summary>Gets the saturation value, in percentages, for this <see cref="T:Licorne.Rgb" /> structure.</summary> /// <returns>The saturation, in percentages, of this <see cref="T:Licorne.Rgb" />. The saturation is measured in percentages, ranging from 0.0 through 100.0, in HSL color space.</returns> /// <param name="rgb">The <see cref="T:Licorne.Rgb" /> from which to get the saturation.</param> private static double GetSaturation(Rgb rgb) { const int greySaturation = 0; var r = rgb.R / 255d; var g = rgb.G / 255d; var b = rgb.B / 255d; var max = r; var min = r; if (g > max) { max = g; } if (b > max) { max = b; } if (g < min) { min = g; } if (b < min) { min = b; } // if max == min, then there is no color and // the saturation is zero. if (max.BasicallyEqualTo(min)) { return(greySaturation); } var l = (max + min) / 2; var s = (max - min) / (l <= .5 ? max + min : (2 - max - min)); // double return(s); }
/// <summary>Creates a <see cref="T:Licorne.Hsl" /> structure from the specified <see cref="T:Licorne.Rgb" /> structure.</summary> /// <returns>The <see cref="T:Licorne.Hsl" /> that this method creates.</returns> /// <param name="color">The <see cref="T:Licorne.Rgb" /> from which to create the new <see cref="T:Licorne.Hsl" />.</param> public Hsl(Rgb color) : this(GetHue(color) , GetSaturation(color) * 100d , GetLuminosity(color) * 100d) { }
/// <summary>Gets the hue value, in degrees, for this <see cref="T:Licorne.Rgb" /> structure.</summary> /// <returns>The hue, in degrees, of this <see cref="T:Licorne.Rgb" />. The hue is measured in degrees, ranging from 0.0 through 360.0, in HSL color space.</returns> /// <param name="rgb">The <see cref="T:Licorne.Rgb" /> from which to get the hue.</param> private static double GetHue(Rgb rgb) { // 0 makes as good an UNDEFINED value as any const int greyHue = 0; if (rgb.R.BasicallyEqualTo(rgb.G) && rgb.G.BasicallyEqualTo(rgb.B)) { return(greyHue); } var r = rgb.R / 255d; var g = rgb.G / 255d; var b = rgb.B / 255d; double max, min; double delta; var hue = 0d; max = r; min = r; if (g > max) { max = g; } if (b > max) { max = b; } if (g < min) { min = g; } if (b < min) { min = b; } delta = max - min; if (r.BasicallyEqualTo(max)) { hue = 0 + (g - b) / delta; } else if (g.BasicallyEqualTo(max)) { hue = 2 + (b - r) / delta; } else if (b.BasicallyEqualTo(max)) { hue = 4 + (r - g) / delta; } hue *= 60; if (hue < 0d) { hue += 360d; } return(hue); }