Skip to content

Commit

Permalink
Cleanup. TODO: Remove auto-clamping.
Browse files Browse the repository at this point in the history
  • Loading branch information
simshaun committed May 9, 2020
1 parent 81bb01a commit 5c6dcb4
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 210 deletions.
4 changes: 0 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ dotnet_naming_rule.method_rule.severity=warning
dotnet_naming_rule.method_rule.style=upper_camel_case_underscore_tolerant_style
dotnet_naming_rule.method_rule.symbols=method_symbols
dotnet_naming_rule.static_readonly_rule.severity=warning
dotnet_naming_rule.static_readonly_rule.style=min_upper_camel_case_style
dotnet_naming_rule.static_readonly_rule.symbols=static_readonly_symbols
dotnet_naming_style.min_upper_camel_case_style.capitalization=pascal_case
dotnet_naming_style.min_upper_camel_case_style.required_prefix=Min
dotnet_naming_style.upper_camel_case_style.capitalization=pascal_case
dotnet_naming_style.upper_camel_case_underscore_tolerant_style.capitalization=pascal_case
dotnet_naming_style.upper_camel_case_underscore_tolerant_style.word_separator=_
dotnet_naming_symbols.constants_symbols.applicable_accessibilities=public,internal,protected,protected_internal,private_protected
Expand Down Expand Up @@ -72,7 +69,6 @@ resharper_csharp_align_multiple_declaration=true
resharper_csharp_max_line_length=150
resharper_csharp_naming_rule.constants=AaBb
resharper_csharp_naming_rule.method=AaBb_AaBb, AaBb
resharper_csharp_naming_rule.static_readonly=Min + AaBb, Max + AaBb
resharper_csharp_outdent_commas=true
resharper_csharp_stick_comment=false
resharper_csharp_use_indent_from_previous_element=false
Expand Down
28 changes: 14 additions & 14 deletions PixelPalette.Tests/Color/ColorTestDataAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public IEnumerable<object[]> GetData(MethodInfo methodInfo)
Rgb = new Rgb(0, 0, 0),
Hsl = new Hsl(0, 0, 0),
Hsv = new Hsv(0, 0, 0),
Cmyk = new Cmyk(0, 0, 0, 100),
Cmyk = new Cmyk(0, 0, 0, 1),
Xyz = new Xyz(0, 0, 0),
Lab = new Lab(0, 0, 0)
}
Expand All @@ -43,9 +43,9 @@ public IEnumerable<object[]> GetData(MethodInfo methodInfo)
new ColorData
{
Name = "White",
Rgb = new Rgb(255, 255, 255),
Hsl = Hsl.FromScaledValues(0, 0, 100),
Hsv = Hsv.FromScaledValues(0, 0, 100),
Rgb = new Rgb(1, 1, 1),
Hsl = new Hsl(0, 0, 1),
Hsv = new Hsv(0, 0, 1),
Cmyk = new Cmyk(0, 0, 0, 0),
Xyz = new Xyz(95.047, 100, 108.883),
Lab = new Lab(100, 0, 0)
Expand All @@ -56,10 +56,10 @@ public IEnumerable<object[]> GetData(MethodInfo methodInfo)
new ColorData
{
Name = "Red",
Rgb = new Rgb(255, 0, 0),
Hsl = Hsl.FromScaledValues(0, 100, 50),
Hsv = Hsv.FromScaledValues(0, 100, 100),
Cmyk = new Cmyk(0, 100, 100, 0),
Rgb = new Rgb(1, 0, 0),
Hsl = new Hsl(0, 1, 0.5),
Hsv = new Hsv(0, 1, 1),
Cmyk = new Cmyk(0, 1, 1, 0),
Xyz = new Xyz(41.2456, 21.2673, 1.9334),
Lab = new Lab(53.2408, 80.0925, 67.2032)
}
Expand All @@ -69,10 +69,10 @@ public IEnumerable<object[]> GetData(MethodInfo methodInfo)
new ColorData
{
Name = "Green",
Rgb = new Rgb(0, 255, 0),
Rgb = new Rgb(0, 1, 0),
Hsl = Hsl.FromScaledValues(120, 100, 50),
Hsv = Hsv.FromScaledValues(120, 100, 100),
Cmyk = new Cmyk(100, 0, 100, 0),
Cmyk = new Cmyk(1, 0, 1, 0),
Xyz = new Xyz(35.7576, 71.5152, 11.9192),
Lab = new Lab(87.7347, -86.1827, 83.1793)
}
Expand All @@ -82,10 +82,10 @@ public IEnumerable<object[]> GetData(MethodInfo methodInfo)
new ColorData
{
Name = "Blue",
Rgb = new Rgb(0, 0, 255),
Rgb = new Rgb(0, 0, 1),
Hsl = Hsl.FromScaledValues(240, 100, 50),
Hsv = Hsv.FromScaledValues(240, 100, 100),
Cmyk = new Cmyk(100, 100, 0, 0),
Cmyk = new Cmyk(1, 1, 0, 0),
Xyz = new Xyz(18.0437, 7.2175, 95.0304),
Lab = new Lab(32.297, 79.1875, -107.8602)
}
Expand All @@ -95,10 +95,10 @@ public IEnumerable<object[]> GetData(MethodInfo methodInfo)
new ColorData
{
Name = "Material Blue",
Rgb = new Rgb(33, 150, 243),
Rgb = Rgb.FromScaledValues(33, 150, 243),
Hsl = Hsl.FromScaledValues(206.5716, 89.744, 54.118),
Hsv = Hsv.FromScaledValues(206.5716, 86.42, 95.294),
Cmyk = new Cmyk(86.419753, 38.271604, 0, 4.705882),
Cmyk = Cmyk.FromScaledValues(86.419753, 38.271604, 0, 4.705882),
Xyz = new Xyz(27.704960365063513, 28.60350077011483, 88.83745176406208),
Lab = new Lab(60.4301, 2.0799, -55.1094)
}
Expand Down
42 changes: 23 additions & 19 deletions PixelPalette.Tests/Color/HslTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ public void FromScaledString_ShouldReturnObject(
[TestMethod, ColorTestData]
public void ColorConversions_ShouldBeAccurate(ColorData color)
{
color.Hsl.ToRgb().ShouldBeEquivalentTo(color.Rgb);
var rgb = color.Hsl.ToRgb();
rgb.RoundedRed.ShouldBeEquivalentTo(color.Rgb.RoundedRed);
rgb.RoundedGreen.ShouldBeEquivalentTo(color.Rgb.RoundedGreen);
rgb.RoundedBlue.ShouldBeEquivalentTo(color.Rgb.RoundedBlue);

var hsv = color.Hsl.ToHsv();
hsv.RoundedHue.ShouldBeEquivalentTo(color.Hsv.RoundedHue);
Expand All @@ -81,15 +84,16 @@ public void ColorConversions_ShouldBeAccurate(ColorData color)
[TestMethod]
public void ToRgb_ShouldBeCorrect_AtMaxHue()
{
var hsl = Hsl.FromScaledValues(360, 50, 50);
hsl.ToRgb().ShouldBeEquivalentTo(new Rgb(191, 64, 64));
var rgb = Hsl.FromScaledValues(360, 50, 50).ToRgb();
rgb.ScaledRed.ShouldBeEquivalentTo(191);
rgb.ScaledGreen.ShouldBeEquivalentTo(64);
rgb.ScaledBlue.ShouldBeEquivalentTo(64);
}

[TestMethod]
public void ToHsv_ShouldBeCorrect_AtMaxHue()
{
var hsl = Hsl.FromScaledValues(360, 50, 50);
var hsv = hsl.ToHsv();
var hsv = Hsl.FromScaledValues(360, 50, 50).ToHsv();
hsv.RoundedScaledHue.ShouldBe(360);
hsv.RoundedScaledSaturation.ShouldBe(66.67);
hsv.RoundedScaledValue.ShouldBe(75);
Expand All @@ -98,8 +102,7 @@ public void ToHsv_ShouldBeCorrect_AtMaxHue()
[TestMethod]
public void ToHsv_ShouldBeCorrect_AtDecimals()
{
var hsl = Hsl.FromScaledValues(60.2, 53, 70.91);
var hsv = hsl.ToHsv();
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);
Expand All @@ -108,8 +111,15 @@ public void ToHsv_ShouldBeCorrect_AtDecimals()
[TestMethod]
public void ToRgb_ShouldBeCorrect_AtDecimals()
{
var hsl = Hsl.FromScaledValues(60.2, 53, 70.91);
hsl.ToRgb().ShouldBeEquivalentTo(new Rgb(220, 220, 142));
var rgb = Hsl.FromScaledValues(60.2, 53, 70.91).ToRgb();
rgb.ScaledRed.ShouldBeEquivalentTo(220);
rgb.ScaledGreen.ShouldBeEquivalentTo(220);
rgb.ScaledBlue.ShouldBeEquivalentTo(142);

rgb = Hsl.FromScaledValues(60.5, 53, 70.91).ToRgb();
rgb.ScaledRed.ShouldBeEquivalentTo(219);
rgb.ScaledGreen.ShouldBeEquivalentTo(220);
rgb.ScaledBlue.ShouldBeEquivalentTo(142);
}

[TestMethod]
Expand All @@ -133,21 +143,15 @@ public void Darker_ShouldDecreaseLightness()
[TestMethod]
public void Hue_ShouldBeClamped()
{
var hsl = Hsl.FromScaledValues(-0.01, 100, 100);
hsl.ScaledHue.ShouldBe(0);

hsl = Hsl.FromScaledValues(360.01, 100, 100);
hsl.ScaledHue.ShouldBe(360);
Hsl.FromScaledValues(-0.01, 100, 100).ScaledHue.ShouldBe(0);
Hsl.FromScaledValues(360.01, 100, 100).ScaledHue.ShouldBe(360);
}

[TestMethod]
public void Saturation_ShouldBeClamped()
{
var hsl = Hsl.FromScaledValues(120, -1, 100);
hsl.ScaledSaturation.ShouldBe(0);

hsl = Hsl.FromScaledValues(120, 101, 100);
hsl.ScaledSaturation.ShouldBe(100);
Hsl.FromScaledValues(120, -1, 100).ScaledSaturation.ShouldBe(0);
Hsl.FromScaledValues(120, 101, 100).ScaledSaturation.ShouldBe(100);
}

[TestMethod]
Expand Down
77 changes: 36 additions & 41 deletions PixelPalette.Tests/Color/HsvTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public void FromScaledString_ShouldReturnObject(
[TestMethod, ColorTestData]
public void ColorConversions_ShouldBeAccurate(ColorData color)
{
color.Hsv.ToRgb().ShouldBeEquivalentTo(color.Rgb);
var rgb = color.Hsv.ToRgb();
rgb.RoundedRed.ShouldBeEquivalentTo(color.Rgb.RoundedRed);
rgb.RoundedGreen.ShouldBeEquivalentTo(color.Rgb.RoundedGreen);
rgb.RoundedBlue.ShouldBeEquivalentTo(color.Rgb.RoundedBlue);

var hsl = color.Hsv.ToHsl();
hsl.RoundedHue.ShouldBeEquivalentTo(color.Hsl.RoundedHue);
Expand All @@ -51,32 +54,39 @@ public void ColorConversions_ShouldBeAccurate(ColorData color)
[TestMethod]
public void ToRgb_ShouldBeCorrect_AtMaxHue()
{
var hsv = Hsv.FromScaledValues(360, 50, 50);
hsv.ToRgb().ShouldBeEquivalentTo(new Rgb(128, 64, 64));
}

[TestMethod]
public void ToRgb_ShouldBeCorrect_AtDecimals()
{
var hsv = Hsv.FromScaledValues(60.2, 53, 70.91);
hsv.ToRgb().ShouldBeEquivalentTo(new Rgb(181, 181, 85));
var rgb = Hsv.FromScaledValues(360, 50, 50).ToRgb();
rgb.ScaledRed.ShouldBeEquivalentTo(128);
rgb.ScaledGreen.ShouldBeEquivalentTo(64);
rgb.ScaledBlue.ShouldBeEquivalentTo(64);
}

[TestMethod]
public void ToHsl_ShouldBeCorrect_AtMaxHue()
{
var hsv = Hsv.FromScaledValues(360, 50, 50);
var hsl = hsv.ToHsl();
var hsl = Hsv.FromScaledValues(360, 50, 50).ToHsl();
hsl.RoundedScaledHue.ShouldBe(360);
hsl.RoundedScaledSaturation.ShouldBe(33.33);
hsl.RoundedScaledLuminance.ShouldBe(37.5);
}

[TestMethod]
public void ToRgb_ShouldBeCorrect_AtDecimals()
{
var rgb = Hsv.FromScaledValues(60.2, 53, 70.91).ToRgb();
rgb.ScaledRed.ShouldBeEquivalentTo(181);
rgb.ScaledGreen.ShouldBeEquivalentTo(181);
rgb.ScaledBlue.ShouldBeEquivalentTo(85);

rgb = Hsv.FromScaledValues(60.3, 53, 70.91).ToRgb();
rgb.ScaledRed.ShouldBeEquivalentTo(180);
rgb.ScaledGreen.ShouldBeEquivalentTo(181);
rgb.ScaledBlue.ShouldBeEquivalentTo(85);
}

[TestMethod]
public void ToHsl_ShouldBeCorrect_AtDecimals()
{
var hsv = Hsv.FromScaledValues(60.2, 53, 70.91);
var hsl = hsv.ToHsl();
var hsl = Hsv.FromScaledValues(60.2, 53, 70.91).ToHsl();
hsl.RoundedScaledHue.ShouldBe(60.2);
hsl.RoundedScaledSaturation.ShouldBe(39.25);
hsl.RoundedScaledLuminance.ShouldBe(52.12);
Expand All @@ -85,67 +95,52 @@ public void ToHsl_ShouldBeCorrect_AtDecimals()
[TestMethod]
public void Hue_ShouldBeClamped()
{
var hsv = Hsv.FromScaledValues(-0.01, 100, 100);
hsv.ScaledHue.ShouldBe(0);

hsv = new Hsv(360.01, 100, 100);
hsv.ScaledHue.ShouldBe(360);
Hsv.FromScaledValues(-0.01, 100, 100).ScaledHue.ShouldBe(0);
new Hsv(360.01, 100, 100).ScaledHue.ShouldBe(360);
}

[TestMethod]
public void Saturation_ShouldBeClamped()
{
var hsv = Hsv.FromScaledValues(120, -1, 100);
hsv.ScaledSaturation.ShouldBe(0);

hsv = Hsv.FromScaledValues(120, 101, 100);
hsv.ScaledSaturation.ShouldBe(100);
Hsv.FromScaledValues(120, -1, 100).ScaledSaturation.ShouldBe(0);
Hsv.FromScaledValues(120, 101, 100).ScaledSaturation.ShouldBe(100);
}

[TestMethod]
public void Value_ShouldBeClamped()
{
var hsv = Hsv.FromScaledValues(120, 0, -1);
hsv.ScaledValue.ShouldBe(0);

hsv = Hsv.FromScaledValues(120, 0, 101);
hsv.ScaledValue.ShouldBe(100);
Hsv.FromScaledValues(120, 0, -1).ScaledValue.ShouldBe(0);
Hsv.FromScaledValues(120, 0, 101).ScaledValue.ShouldBe(100);
}

[TestMethod]
public void RoundedHue_ShouldRound()
{
// Half-up
var hsv = Hsv.FromScaledValues(240.585, 100, 50);
hsv.RoundedScaledHue.ShouldBe(240.59);
Hsv.FromScaledValues(240.585, 100, 50).RoundedScaledHue.ShouldBe(240.59);

// Down
hsv = Hsv.FromScaledValues(240.584, 100, 50);
hsv.RoundedScaledHue.ShouldBe(240.58);
Hsv.FromScaledValues(240.584, 100, 50).RoundedScaledHue.ShouldBe(240.58);
}

[TestMethod]
public void RoundedSaturation100_ShouldRound()
{
// Half-up
var hsv = Hsv.FromScaledValues(240, 59.645, 100);
hsv.RoundedScaledSaturation.ShouldBe(59.65);
Hsv.FromScaledValues(240, 59.645, 100).RoundedScaledSaturation.ShouldBe(59.65);

// Down
hsv = Hsv.FromScaledValues(240, 59.644, 100);
hsv.RoundedScaledSaturation.ShouldBe(59.64);
Hsv.FromScaledValues(240, 59.644, 100).RoundedScaledSaturation.ShouldBe(59.64);
}

[TestMethod]
public void RoundedValue100_ShouldRound()
{
// Half-up
var hsv = Hsv.FromScaledValues(240, 100, 59.645);
hsv.RoundedScaledValue.ShouldBe(59.65);
Hsv.FromScaledValues(240, 100, 59.645).RoundedScaledValue.ShouldBe(59.65);

// Down
hsv = Hsv.FromScaledValues(240, 100, 59.644);
hsv.RoundedScaledValue.ShouldBe(59.64);
Hsv.FromScaledValues(240, 100, 59.644).RoundedScaledValue.ShouldBe(59.64);
}
}
}
52 changes: 15 additions & 37 deletions PixelPalette.Tests/Color/RgbTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PixelPalette.Color;
using System;
using PixelPalette.Color;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

Expand Down Expand Up @@ -144,45 +145,22 @@ public void ColorConversions_ShouldBeAccurate(ColorData color)
}

[TestMethod]
public void Red_ShouldBeClamped()
public void NewRgb_WithOutOfRangeValue_ShouldThrowException()
{
var rgb = new Rgb(-1, 0, 0);
rgb.ScaledRed.ShouldBe(0);
rgb = new Rgb(256, 0, 0);
rgb.ScaledRed.ShouldBe(255);
Assert.ThrowsException<ArgumentException>(() => new Rgb(-0.1, 0, 0));
Assert.ThrowsException<ArgumentException>(() => new Rgb(1.1, 0, 0));
Assert.ThrowsException<ArgumentException>(() => Rgb.FromScaledValues(-1, 0, 0));
Assert.ThrowsException<ArgumentException>(() => Rgb.FromScaledValues(256, 0, 0));

rgb = new Rgb(-0.1, 0, 0);
rgb.Red.ShouldBe(0.0);
rgb = new Rgb(1.1, 0, 0);
rgb.Red.ShouldBe(1.0);
}

[TestMethod]
public void Green_ShouldBeClamped()
{
var rgb = new Rgb(0, -1, 0);
rgb.ScaledGreen.ShouldBe(0);
rgb = new Rgb(0, 256, 0);
rgb.ScaledGreen.ShouldBe(255);
Assert.ThrowsException<ArgumentException>(() => new Rgb(0, -0.1, 0));
Assert.ThrowsException<ArgumentException>(() => new Rgb(0, 1.1, 0));
Assert.ThrowsException<ArgumentException>(() => Rgb.FromScaledValues(0, -1, 0));
Assert.ThrowsException<ArgumentException>(() => Rgb.FromScaledValues(0, 256, 0));

rgb = new Rgb(0, -0.1, 0);
rgb.Green.ShouldBe(0.0);
rgb = new Rgb(0, 1.1, 0);
rgb.Green.ShouldBe(1.0);
}

[TestMethod]
public void Blue_ShouldBeClamped()
{
var rgb = new Rgb(0, 0, -1);
rgb.ScaledBlue.ShouldBe(0);
rgb = new Rgb(0, 0, 256);
rgb.ScaledBlue.ShouldBe(255);

rgb = new Rgb(0, 0, -0.1);
rgb.Blue.ShouldBe(0.0);
rgb = new Rgb(0, 0, 1.1);
rgb.Blue.ShouldBe(1.0);
Assert.ThrowsException<ArgumentException>(() => new Rgb(0, 0, -0.1));
Assert.ThrowsException<ArgumentException>(() => new Rgb(0, 0, 1.1));
Assert.ThrowsException<ArgumentException>(() => Rgb.FromScaledValues(0, 0, -1));
Assert.ThrowsException<ArgumentException>(() => Rgb.FromScaledValues(0, 0, 256));
}
}
}
Loading

0 comments on commit 5c6dcb4

Please sign in to comment.