This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 505
/
Color_Tests.cs
177 lines (162 loc) · 5.51 KB
/
Color_Tests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Drawing;
using Xamarin.Essentials;
using Xunit;
namespace Tests
{
public class Color_Tests
{
float h = 204;
float s = 69.9f;
float l = 53.1f;
int r = 52;
int g = 152;
int b = 219;
[Fact]
public void FromHsl()
{
var color = ColorConverters.FromHsl(h, s, l);
Assert.Equal(255, color.A);
Assert.Equal(r, color.R);
Assert.Equal(g, color.G);
Assert.Equal(b, color.B);
}
[Fact]
public void FromUint()
{
var color = ColorConverters.FromUInt(4294933007);
Assert.Equal(255, color.A);
Assert.Equal(255, color.R);
Assert.Equal(122, color.G);
Assert.Equal(15, color.B);
}
[Fact]
public void ToUInt()
{
var color = Color.FromArgb(255, 255, 122, 15);
var i = color.ToUInt();
Assert.Equal(4294933007U, i);
}
[Theory]
[InlineData("#3498DB", 255, 52, 152, 219)]
[InlineData("#C0C0C0", 255, 192, 192, 192)]
[InlineData("3498DB", 255, 52, 152, 219)]
[InlineData("C0C0C0", 255, 192, 192, 192)]
[InlineData("#903498DB", 144, 52, 152, 219)]
[InlineData("#90C0C0C0", 144, 192, 192, 192)]
public void FromHex(string hex, int a, int r, int g, int b)
{
var color = ColorConverters.FromHex(hex);
Assert.Equal(a, color.A);
Assert.Equal(r, color.R);
Assert.Equal(g, color.G);
Assert.Equal(b, color.B);
}
[Theory]
[InlineData("#FF0000", "#00FFFF")] // Red & Cyan
[InlineData("#00FF00", "#FF00FF")] // Green & Fuschia
[InlineData("#0000FF", "#FFFF00")] // Blue & Yellow
[InlineData("#0AF56C", "#F50A93")] // Lime green & bright purple (but with no limit values)
public void GetComplementary(string original, string expected)
{
var orig = ColorConverters.FromHex(original);
var expectedComplement = ColorConverters.FromHex(expected);
Assert.Equal(expectedComplement, orig.GetComplementary());
}
[Fact]
public void FromHsla()
{
var a = 186;
var color = ColorConverters.FromHsla(h, s, l, 186);
Assert.Equal(a, color.A);
Assert.Equal(r, color.R);
Assert.Equal(g, color.G);
Assert.Equal(b, color.B);
}
[Fact]
public void MuliplyAlpha()
{
var color = Color.FromArgb(r, g, b);
color = color.MultiplyAlpha(.5f);
Assert.Equal((int)(255 * .5f), color.A);
Assert.Equal(r, color.R);
Assert.Equal(g, color.G);
Assert.Equal(b, color.B);
}
[Fact]
public void WithHue()
{
var color = Color.FromArgb(r, g, b);
color = color.WithHue(10);
Assert.Equal(255, color.A);
Assert.Equal(219, color.R);
Assert.Equal(80, color.G);
Assert.Equal(52, color.B);
}
[Fact]
public void WithAlpha()
{
var color = Color.FromArgb(r, g, b);
color = color.WithAlpha(10);
Assert.Equal(10, color.A);
Assert.Equal(r, color.R);
Assert.Equal(g, color.G);
Assert.Equal(b, color.B);
}
[Fact]
public void WithSaturation()
{
var color = Color.FromArgb(r, g, b);
color = color.WithSaturation(10);
Assert.Equal(255, color.A);
Assert.Equal(124, color.R);
Assert.Equal(138, color.G);
Assert.Equal(147, color.B);
}
[Fact]
public void WithLuminosity()
{
var color = Color.FromArgb(r, g, b);
color = color.WithLuminosity(10);
Assert.Equal(255, color.A);
Assert.Equal(8, color.R);
Assert.Equal(29, color.G);
Assert.Equal(43, color.B);
}
[Theory]
[InlineData("black", 0, 0, 0)]
[InlineData("red", 0, 100, 100)]
[InlineData("white", 0, 0, 100)]
[InlineData("green", 120, 100, 50.2)]
[InlineData("lime", 120, 100, 100)]
[InlineData("yellow", 60, 100, 100)]
[InlineData("magenta", 300, 100, 100)]
[InlineData("cyan", 180, 100, 100)]
public void RgbToHsv(string name, double hTest, double sTest, double vTest)
{
var color = Color.FromName(name);
var (h, s, v) = color.ToHsv();
Assert.Equal(hTest, h);
Assert.Equal(sTest, s);
Assert.Equal(vTest, Math.Round(v, 1));
}
[Theory]
[InlineData("black", 0, 0, 0)]
[InlineData("red", 0, 100, 100)]
[InlineData("white", 0, 0, 100)]
[InlineData("green", 120, 100, 50.2)]
[InlineData("lime", 120, 100, 100)]
[InlineData("yellow", 60, 100, 100)]
[InlineData("magenta", 300, 100, 100)]
[InlineData("cyan", 180, 100, 100)]
public void HsvToRgba(string name, double hTest, double sTest, double vTest)
{
var colorExpected = Color.FromName(name);
var color = ColorExtensions.FromHsva(hTest, sTest, vTest, 50);
Assert.Equal(colorExpected.R, color.R);
Assert.Equal(colorExpected.G, color.G);
Assert.Equal(colorExpected.B, color.B);
Assert.Equal(50, color.A);
}
}
}