public void HsltoRgbIsConvertedCorrectly(ushort hue, float saturation, float luminance, Color expected) { var sut = new Hsl(hue, saturation, luminance); Color actual = sut; Assert.Equal(expected, actual); }
public void TryInt(int a, int b, int c) { var expected = new[] { a, b, c }; var hsl = new Hsl(a, b, c); var actual = new double[] { hsl.H, hsl.S, hsl.L }.Select(Convert.ToInt32).ToArray(); Assert.Equal(expected, actual); }
/// <summary> /// Converts a <see cref="Hsl"/> into a <see cref="Cmyk"/> /// </summary> /// <param name="color">The color to convert.</param> /// <returns>The <see cref="Cmyk"/></returns> public Cmyk ToCmyk(Hsl color) { Guard.NotNull(color, nameof(color)); var rgb = this.ToRgb(color); return(CmykAndRgbConverter.Convert(rgb)); }
public void ToHsv_ShouldBeCorrect_AtDecimals() { var hsv = Hsl.FromScaledValues(60.2, 53, 70.91).ToHsv(); hsv.RoundedScaledHue.ShouldBe(60.2); hsv.RoundedScaledSaturation.ShouldBe(35.72); hsv.RoundedScaledValue.ShouldBe(86.33); }
/// <summary> /// Converts a <see cref="Hsl"/> into a <see cref="LinearRgb"/> /// </summary> /// <param name="color">The color to convert.</param> /// <returns>The <see cref="LinearRgb"/></returns> public LinearRgb ToLinearRgb(Hsl color) { Guard.NotNull(color, nameof(color)); var rgb = this.ToRgb(color); return(this.ToLinearRgb(rgb)); }
/// <summary> /// Converts a <see cref="Hsl"/> into a <see cref="YCbCr"/> /// </summary> /// <param name="color">The color to convert.</param> /// <returns>The <see cref="YCbCr"/></returns> public YCbCr ToYCbCr(Hsl color) { Guard.NotNull(color, nameof(color)); var rgb = this.ToRgb(color); return(YCbCrAndRgbConverter.Convert(rgb)); }
/// <summary> /// Converts a <see cref="Hsl"/> into a <see cref="CieLuv"/> /// </summary> /// <param name="color">The color to convert.</param> /// <returns>The <see cref="CieLuv"/></returns> public CieLuv ToCieLuv(Hsl color) { Guard.NotNull(color, nameof(color)); var xyzColor = this.ToCieXyz(color); return(this.ToCieLuv(xyzColor)); }
public void Lighter_ShouldIncreaseLightness() { var hsl = Hsl.FromScaledValues(240, 100, 80); hsl.Lighter(5).ShouldBeEquivalentTo(hsl.WithScaledLuminance(85)); hsl.Lighter(11).ShouldBeEquivalentTo(hsl.WithScaledLuminance(91)); hsl.Lighter(30).ShouldBeEquivalentTo(hsl.WithScaledLuminance(100)); }
public void ToRgb_ShouldBeCorrect_AtMaxHue() { var rgb = Hsl.FromScaledValues(360, 50, 50).ToRgb(); rgb.ScaledRed.ShouldBeEquivalentTo(191); rgb.ScaledGreen.ShouldBeEquivalentTo(64); rgb.ScaledBlue.ShouldBeEquivalentTo(64); }
public void TryFloat(float a, float b, float c) { var expected = new[] { a, b, c }; var hsl = new Hsl(a, b, c); var actual = new double[] { hsl.H, hsl.S, hsl.L }.Select(Convert.ToSingle).ToArray(); Assert.Equal(expected, actual); }
public void ToHsv_ShouldBeCorrect_AtMaxHue() { var hsv = Hsl.FromScaledValues(360, 50, 50).ToHsv(); hsv.RoundedScaledHue.ShouldBe(360); hsv.RoundedScaledSaturation.ShouldBe(66.67); hsv.RoundedScaledValue.ShouldBe(75); }
public void Darker_ShouldDecreaseLightness() { var hsl = Hsl.FromScaledValues(240, 100, 20); hsl.Darker(5).ShouldBeEquivalentTo(hsl.WithScaledLuminance(15)); hsl.Darker(11).ShouldBeEquivalentTo(hsl.WithScaledLuminance(9)); hsl.Darker(30).ShouldBeEquivalentTo(hsl.WithScaledLuminance(0)); }
public void TryByte(byte a, byte b, byte c) { var expected = new[] { a, b, c }; var hsl = new Hsl(a, b, c); var actual = new double[] { hsl.H, hsl.S, hsl.L }.Select(Convert.ToByte).ToArray(); Assert.Equal(expected, actual); }
/// <summary> /// Converts a <see cref="Hsl"/> into a <see cref="Lms"/> /// </summary> /// <param name="color">The color to convert.</param> /// <returns>The <see cref="Lms"/></returns> public Lms ToLms(Hsl color) { Guard.NotNull(color, nameof(color)); var xyzColor = this.ToCieXyz(color); return(this.ToLms(xyzColor)); }
/// <summary> /// Converts a <see cref="Hsl"/> into a <see cref="Hsv"/> /// </summary> /// <param name="color">The color to convert.</param> /// <returns>The <see cref="Hsv"/></returns> public Hsv ToHsv(Hsl color) { Guard.NotNull(color, nameof(color)); var rgb = this.ToRgb(color); return(HsvAndRgbConverter.Convert(rgb)); }
/// <summary> /// Converts a <see cref="Hsl"/> into a <see cref="CieLab"/> /// </summary> /// <param name="color">The color to convert.</param> /// <returns>The <see cref="CieLab"/></returns> public CieLab ToCieLab(Hsl color) { Guard.NotNull(color, nameof(color)); CieXyz xyzColor = this.ToCieXyz(color); return(this.ToCieLab(xyzColor)); }
/// <summary> /// Converts a <see cref="Hsl"/> into a <see cref="CieXyz"/> /// </summary> /// <param name="color">The color to convert.</param> /// <returns>The <see cref="CieXyz"/></returns> public CieXyz ToCieXyz(Hsl color) { Guard.NotNull(color, nameof(color)); // Conversion var rgb = this.ToRgb(color); return(this.ToCieXyz(rgb)); }
public void AliceBlueHslToHex() { var knownColor = new Hsl { H = 208, S = 1, L = 0.97, }; var expectedColor = new Hex("f0f8ff"); ExpectedValuesForKnownColor(knownColor, expectedColor); }
public void Luminance_ShouldBeClamped() { var hsl = Hsl.FromScaledValues(120, 0, -1); hsl.ScaledLuminance.ShouldBe(0); hsl = Hsl.FromScaledValues(120, 0, 101); hsl.ScaledLuminance.ShouldBe(100); }
internal static Hsl ToHsl(this Color color) { var hsl = new Hsl(); hsl.Initialize(new Rgb { R = color.R, G = color.G, B = color.B }); return(hsl); }
public void Convert(double h, double s, double l, double r, double g, double b) { var expected = new Hsl(h, s, l); var actual = new Hsl(new Rgb(r, g, b)); // Assert.Equal(expected, actual); Assert.True(BasicallyEquals(expected, actual)); }
internal static void ToColorSpace(IRgb color, IHsb item) { var hsl = new Hsl(); HslConverter.ToColorSpace(color, hsl); item.H = hsl.H; item.S = hsl.S; item.B = hsl.L; }
public void AliceBlueMunsellToHsl() { var knownColor = new Munsell("10BG 8/4"); var expectedColor = new Hsl { H = 193, S = 0.56, L = 0.77, }; ExpectedValuesForKnownColor(knownColor, expectedColor); }
private static Color HslToRgb(Hsl hsl) { double r = 0, g = 0, b = 0; if (hsl.L == 0) { r = g = b = 0; } else { if (hsl.S == 0) { r = g = b = hsl.L; } else { double temp2 = ((hsl.L <= 0.5) ? hsl.L * (1.0 + hsl.S) : hsl.L + hsl.S - (hsl.L * hsl.S)); double temp1 = 2.0 * hsl.L - temp2; var t3 = new double[] { hsl.H + 1.0 / 3.0, hsl.H, hsl.H - 1.0 / 3.0 }; var clr = new double[] { 0, 0, 0 }; for (int i = 0; i < 3; i++) { if (t3[i] < 0) { t3[i] += 1.0; } if (t3[i] > 1) { t3[i] -= 1.0; } if (6.0 * t3[i] < 1.0) { clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0; } else if (2.0 * t3[i] < 1.0) { clr[i] = temp2; } else if (3.0 * t3[i] < 2.0) { clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0); } else { clr[i] = temp1; } } r = clr[0]; g = clr[1]; b = clr[2]; } } return(Color.FromArgb(0xff, (byte)(0xff * r), (byte)(0xff * g), (byte)(0xff * b))); }
public void AliceBlueHslToMunsell() { var knownColor = new Hsl { H = 208, S = 1, L = 0.97, }; var expectedColor = new Munsell("2PB 9.61/0.99"); ExpectedValuesForKnownColor(knownColor, expectedColor); }
internal static IRgb ToColor(IHsb item) { var hsl = new Hsl { H = item.H, S = item.S, L = item.B }; return HslConverter.ToColor(hsl); }
public static Color HslToRgb(double h, double s, double l) { var hsl = new Hsl { H = h, S = s, L = l }; var r = hsl.To <Rgb>(); var d = Color.FromArgb((int)r.R, (int)r.G, (int)r.B); return(d); }
public void AliceBlueHsbToHsl() { var knownColor = new Hsb { H = 208, S = 100, B = 97, }; var expectedColor = new Hsl { H = 208, S = 100, L = 97, }; ExpectedValuesForKnownColor(knownColor, expectedColor); }
public void AliceBlueHslToYxy() { var knownColor = new Hsl { H = 208, S = 100, L = 97, }; var expectedColor = new Yxy { Y1 = 92.880, X = .30363, Y2 = .32210, }; ExpectedValuesForKnownColor(knownColor, expectedColor); }
public void AliceBlueHslToXyz() { var knownColor = new Hsl { H = 208, S = 100, L = 97, }; var expectedColor = new Xyz { X = 87.553, Y = 92.880, Z = 107.921, }; ExpectedValuesForKnownColor(knownColor, expectedColor); }
public void AliceBlueHslToRgb() { var knownColor = new Hsl { H = 208, S = 100, L = 97, }; var expectedColor = new Rgb { R = 240, G = 248, B = 255, }; ExpectedValuesForKnownColor(knownColor, expectedColor); }
private static Color HslToRgb(Hsl hsl) { double r = 0, g = 0, b = 0; if (hsl.L == 0) { r = g = b = 0; } else { if (hsl.S == 0) { r = g = b = hsl.L; } else { double temp2 = ((hsl.L <= 0.5) ? hsl.L * (1.0 + hsl.S) : hsl.L + hsl.S - (hsl.L * hsl.S)); double temp1 = 2.0 * hsl.L - temp2; var t3 = new double[] { hsl.H + 1.0 / 3.0, hsl.H, hsl.H - 1.0 / 3.0 }; var clr = new double[] { 0, 0, 0 }; for (int i = 0; i < 3; i++) { if (t3[i] < 0) t3[i] += 1.0; if (t3[i] > 1) t3[i] -= 1.0; if (6.0 * t3[i] < 1.0) clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0; else if (2.0 * t3[i] < 1.0) clr[i] = temp2; else if (3.0 * t3[i] < 2.0) clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0); else clr[i] = temp1; } r = clr[0]; g = clr[1]; b = clr[2]; } } return Color.FromArgb(0xff, (byte)(0xff * r), (byte)(0xff * g), (byte)(0xff * b)); }
public static Hsl ToHsl(this Color color) { double r = (color.R / 255.0); double g = (color.G / 255.0); double b = (color.B / 255.0); double min = Math.Min(Math.Min(r, g), b); double max = Math.Max(Math.Max(r, g), b); double delta = max - min; Hsl hsl = new Hsl(); hsl.Alpha = color.A / 255.0; hsl.Luminosity = ((max + min) / 2.0); if (Math.Abs(delta) > Hsl.MaxHslColorPrecision) { if (hsl.Luminosity < 0.5) { hsl.Saturation = (delta / (max + min)); } else { hsl.Saturation = (delta / (2.0 - max - min)); } if (Math.Abs(r - max) < Hsl.MaxHslColorPrecision) { hsl.Hue = (g - b) / delta + (g < b ? 6 : 0); } else if (Math.Abs(g - max) < Hsl.MaxHslColorPrecision) { hsl.Hue = 2 + (b - r) / delta; } else { hsl.Hue = 4 + (r - g) / delta; } hsl.Hue = hsl.Hue/6.0; } return hsl; }
public static Color FromHsl(Hsl hsl) { Func<double, double, double, double> hueToRgb = (c, t1, t2) => { if (c < 0) c += 1.0; if (c > 1) c -= 1.0; if (6.0*c < 1.0) return t1 + (t2 - t1)*6.0*c; if (2.0*c < 1.0) return t2; if (3.0*c < 2.0) return t1 + (t2 - t1)*(2.0/3.0 - c)*6.0; return t1; }; int alpha = (int)Math.Round(hsl.Alpha*255.0); if (Math.Abs(hsl.Saturation) < Hsl.MaxHslColorPrecision) { var mono = (int) Math.Round(hsl.Luminosity*255.0); return Color.FromArgb(alpha, mono, mono, mono); } else { double t2 = hsl.Luminosity < 0.5 ? hsl.Luminosity*(1.0 + hsl.Saturation) : (hsl.Luminosity + hsl.Saturation) - (hsl.Luminosity*hsl.Saturation); double t1 = 2.0*hsl.Luminosity - t2; var r = (int) Math.Round(hueToRgb(hsl.Hue + 1.0/3.0, t1, t2)*255.0); var g = (int) Math.Round(hueToRgb(hsl.Hue, t1, t2)*255.0); var b = (int) Math.Round(hueToRgb(hsl.Hue - 1.0/3.0, t1, t2)*255.0); return Color.FromArgb(alpha, r, g, b); } }
/// <summary> /// Converts from an HSL color to Argb (system) color /// </summary> public static Color ToColor(Hsl value) { byte red; byte green; byte blue; double Saturation = value.Saturation; double Luminance = value.Luminance; double Hue = value.Hue; if (Saturation == 0.0) { red = (byte)(Luminance * 255.0F); green = red; blue = red; } else { double rm1; double rm2; if (Luminance <= 0.5f) { rm2 = Luminance + Luminance * Saturation; } else { rm2 = Luminance + Saturation - Luminance * Saturation; } rm1 = 2.0f * value.Luminance - rm2; red = ToRGB1(rm1, rm2, Hue + 120.0f); green = ToRGB1(rm1, rm2, Hue); blue = ToRGB1(rm1, rm2, Hue - 120.0f); } return new Color(value.Alpha, red, green, blue); }
/// <summary> /// Converts from an Argb (system) color to HSL /// </summary> public static Hsl ToHsl(Color value) { double luminance = 0f; double saturation = 0f; double hue = 0f; byte red = value.Red; byte green = value.Green; byte blue = value.Blue; byte minval = System.Math.Min(red, System.Math.Min(green, blue)); byte maxval = System.Math.Max(red, System.Math.Max(green, blue)); double mdiff = (double)(maxval - minval); double msum = (double)(maxval + minval); luminance = msum / 510.0f; if (maxval == minval) { saturation = 0.0f; hue = 0.0f; } else { double rnorm = (maxval - red) / mdiff; double gnorm = (maxval - green) / mdiff; double bnorm = (maxval - blue) / mdiff; saturation = (luminance <= 0.5f) ? (mdiff / msum) : (mdiff / (510.0f - msum)); if (red == maxval) { hue = 60.0f * (6.0f + bnorm - gnorm); } if (green == maxval) { hue = 60.0f * (2.0f + rnorm - bnorm); } if (blue == maxval) { hue = 60.0f * (4.0f + gnorm - rnorm); } if (hue > 360.0f) { hue = hue - 360.0f; } } Hsl result = new Hsl(hue, saturation, luminance); result.Alpha = value.Alpha; return result; }
// /// <summary> /// Converts RGB to HSL /// </summary> /// <remarks>Takes advantage of whats already built in to .NET by using the Color.GetHue, Color.GetSaturation and Color.GetBrightness methods</remarks> /// <param name="c">A Color to convert</param> /// <returns>An HSL value</returns> public static Hsl RGB_to_HSL(Color c) { var hsl = new Hsl(); var c2 = System.Drawing.Color.FromArgb(1, c.R, c.G, c.B); hsl.H = c2.GetHue() / 360.0; // we store hue as 0-1 as opposed to 0-360 hsl.L = c2.GetBrightness(); hsl.S = c2.GetSaturation(); return hsl; }