1+ using System ;
2+ using NUnit . Framework ;
3+ using FluentAssertions ;
4+
5+ namespace Algorithms . Other
6+ {
7+ public static class RgbHsvConversionTest
8+ {
9+ [ Test ]
10+ public static void HueOutOfRange_ThrowsArgumentOutOfRangeException ( )
11+ {
12+ Action act = ( ) => Algorithms . Other . RgbHsvConversion . HsvToRgb ( 400 , 0 , 0 ) ;
13+ act . Should ( ) . Throw < ArgumentOutOfRangeException > ( ) ;
14+ }
15+
16+ [ Test ]
17+ public static void SaturationOutOfRange_ThrowsArgumentOutOfRangeException ( )
18+ {
19+ Action act = ( ) => Algorithms . Other . RgbHsvConversion . HsvToRgb ( 0 , 2 , 0 ) ;
20+ act . Should ( ) . Throw < ArgumentOutOfRangeException > ( ) ;
21+ }
22+
23+ [ Test ]
24+ public static void ValueOutOfRange_ThrowsArgumentOutOfRangeException ( )
25+ {
26+ Action act = ( ) => Algorithms . Other . RgbHsvConversion . HsvToRgb ( 0 , 0 , 2 ) ;
27+ act . Should ( ) . Throw < ArgumentOutOfRangeException > ( ) ;
28+ }
29+
30+ // expected RGB-values taken from https://www.rapidtables.com/convert/color/hsv-to-rgb.html
31+ [ Test ]
32+ [ TestCase ( 0 , 0 , 0 , 0 , 0 , 0 ) ]
33+ [ TestCase ( 0 , 0 , 1 , 255 , 255 , 255 ) ]
34+ [ TestCase ( 0 , 1 , 1 , 255 , 0 , 0 ) ]
35+ [ TestCase ( 60 , 1 , 1 , 255 , 255 , 0 ) ]
36+ [ TestCase ( 120 , 1 , 1 , 0 , 255 , 0 ) ]
37+ [ TestCase ( 240 , 1 , 1 , 0 , 0 , 255 ) ]
38+ [ TestCase ( 300 , 1 , 1 , 255 , 0 , 255 ) ]
39+ [ TestCase ( 180 , 0.5 , 0.5 , 64 , 128 , 128 ) ]
40+ [ TestCase ( 234 , 0.14 , 0.88 , 193 , 196 , 224 ) ]
41+ [ TestCase ( 330 , 0.75 , 0.5 , 128 , 32 , 80 ) ]
42+ public static void TestRgbOutput (
43+ double hue ,
44+ double saturation ,
45+ double value ,
46+ byte expectedRed ,
47+ byte exptectedGreen ,
48+ byte exptectedBlue )
49+ {
50+ var rgb = Algorithms . Other . RgbHsvConversion . HsvToRgb ( hue , saturation , value ) ;
51+ rgb . Item1 . Should ( ) . Be ( expectedRed ) ;
52+ rgb . Item2 . Should ( ) . Be ( exptectedGreen ) ;
53+ rgb . Item3 . Should ( ) . Be ( exptectedBlue ) ;
54+ }
55+
56+ // Parameters of test-cases for TestRGBOutput reversed
57+ [ Test ]
58+ [ TestCase ( 0 , 0 , 0 , 0 , 0 , 0 ) ]
59+ [ TestCase ( 255 , 255 , 255 , 0 , 0 , 1 ) ]
60+ [ TestCase ( 255 , 0 , 0 , 0 , 1 , 1 ) ]
61+ [ TestCase ( 255 , 255 , 0 , 60 , 1 , 1 ) ]
62+ [ TestCase ( 0 , 255 , 0 , 120 , 1 , 1 ) ]
63+ [ TestCase ( 0 , 0 , 255 , 240 , 1 , 1 ) ]
64+ [ TestCase ( 255 , 0 , 255 , 300 , 1 , 1 ) ]
65+ [ TestCase ( 64 , 128 , 128 , 180 , 0.5 , 0.5 ) ]
66+ [ TestCase ( 193 , 196 , 224 , 234 , 0.14 , 0.88 ) ]
67+ [ TestCase ( 128 , 32 , 80 , 330 , 0.75 , 0.5 ) ]
68+ public static void TestHsvOutput (
69+ byte red ,
70+ byte green ,
71+ byte blue ,
72+ double expectedHue ,
73+ double expectedSaturation ,
74+ double expectedValue )
75+ {
76+ var hsv = Algorithms . Other . RgbHsvConversion . RgbToHsv ( red , green , blue ) ;
77+
78+ // approximate-assertions needed because of small deviations due to converting between byte-values and double-values.
79+ hsv . Item1 . Should ( ) . BeApproximately ( expectedHue , 0.2 ) ;
80+ hsv . Item2 . Should ( ) . BeApproximately ( expectedSaturation , 0.002 ) ;
81+ hsv . Item3 . Should ( ) . BeApproximately ( expectedValue , 0.002 ) ;
82+ }
83+ }
84+ }
0 commit comments