-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors_test.go
147 lines (131 loc) · 5.01 KB
/
colors_test.go
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
package smetana
import "testing"
func TestCanConvertHexToRgb(t *testing.T) {
assertEqual(t, RGB{0, 0, 0}, Hex("#000000"))
assertEqual(t, RGB{255, 255, 255}, Hex("#FFFFFF"))
assertEqual(t, RGB{255, 255, 255}, Hex("#ffffff"))
assertEqual(t, RGB{255, 0, 0}, Hex("#ff0000"))
assertEqual(t, RGB{0, 255, 0}, Hex("#00ff00"))
assertEqual(t, RGB{0, 0, 255}, Hex("#0000ff"))
assertEqual(t, RGB{136, 136, 136}, Hex("#888888"))
assertEqual(t, RGB{0, 0, 0}, Hex("#000"))
assertEqual(t, RGB{255, 255, 255}, Hex("#fff"))
assertEqual(t, RGB{255, 0, 0}, Hex("#f00"))
assertEqual(t, RGB{0, 255, 0}, Hex("#0f0"))
assertEqual(t, RGB{0, 0, 255}, Hex("#00f"))
assertEqual(t, RGB{136, 136, 136}, Hex("#888"))
assertEqual(t, RGB{0, 0, 0}, Hex("invalid-color"))
assertEqual(t, RGB{0, 0, 0}, Hex("#xxxxxx"))
assertEqual(t, RGB{0, 0, 0}, Hex("#xxx"))
assertEqual(t, RGB{0, 0, 0}, Hex("xxxxxxx"))
assertEqual(t, RGB{0, 0, 0}, Hex("xxxx"))
}
func TestRgbToString(t *testing.T) {
assertEqual(t, "#000000", Rgb(0, 0, 0).String())
assertEqual(t, "#FF0000", Rgb(255, 0, 0).String())
assertEqual(t, "#00FF00", Rgb(0, 255, 0).String())
assertEqual(t, "#0000FF", Rgb(0, 0, 255).String())
assertEqual(t, "#FFFFFF", Rgb(255, 255, 255).String())
assertEqual(t, "#828282", Rgb(130, 130, 130).String())
}
func TestRgbaToString(t *testing.T) {
assertEqual(t, "rgba(0, 0, 0, 1.00)", Rgba(0, 0, 0, 255).String())
assertEqual(t, "rgba(255, 0, 0, 0.00)", Rgba(255, 0, 0, 0).String())
assertEqual(t, "rgba(0, 200, 0, 0.51)", Rgba(0, 200, 0, 130).String())
}
func TestHslToString(t *testing.T) {
assertEqual(t, "hsl(0, 40.0%, 80.0%)", Hsl(0, 0.4, 0.8).String())
assertEqual(t, "hsl(120, 0.0%, 100.0%)", Hsl(120, 0.0, 1.0).String())
assertEqual(t, "hsl(240, 90.0%, 10.0%)", Hsl(240, 0.9, 0.1).String())
}
func TestHslaToString(t *testing.T) {
assertEqual(t, "hsla(0, 40.0%, 80.0%, 0.00)", Hsla(0, 0.4, 0.8, 0.0).String())
assertEqual(t, "hsla(120, 0.0%, 100.0%, 1.00)", Hsla(120, 0.0, 1.0, 1.0).String())
assertEqual(t, "hsla(240, 90.0%, 10.0%, 0.60)", Hsla(240, 0.9, 0.1, 0.6).String())
}
func TestRgbToHsla(t *testing.T) {
black := Rgb(0, 0, 0)
assertEqual(t, Hsla(0, 0, 0, 1), black.ToHsla())
white := Rgb(255, 255, 255)
assertEqual(t, Hsla(0, 0, 1, 1), white.ToHsla())
red := Rgb(255, 0, 0)
assertEqual(t, Hsla(0, 1, 0.5, 1), red.ToHsla())
green := Rgb(0, 255, 0)
assertEqual(t, Hsla(120, 1, 0.5, 1), green.ToHsla())
blue := Rgb(0, 0, 255)
assertEqual(t, Hsla(240, 1, 0.5, 1), blue.ToHsla())
pink := Rgb(255, 0, 100)
assertEqual(t, Hsla(336, 1, 0.5, 1), pink.ToHsla())
cyan := Rgb(0, 102, 102)
assertEqual(t, Hsla(180, 1, 0.2, 1), cyan.ToHsla())
}
func TestRgbaToHsla(t *testing.T) {
black := Rgba(0, 0, 0, 255)
assertEqual(t, Hsla(0, 0, 0, 1), black.ToHsla())
white := Rgba(255, 255, 255, 255)
assertEqual(t, Hsla(0, 0, 1, 1), white.ToHsla())
red := Rgba(255, 0, 0, 255)
assertEqual(t, Hsla(0, 1, 0.5, 1), red.ToHsla())
green := Rgba(0, 255, 0, 255)
assertEqual(t, Hsla(120, 1, 0.5, 1), green.ToHsla())
blue := Rgba(0, 0, 255, 255)
assertEqual(t, Hsla(240, 1, 0.5, 1), blue.ToHsla())
alpha := Rgba(0, 0, 255, 127)
assertEqual(t, Hsla(240, 1, 0.5, 0.49803922), alpha.ToHsla())
}
func TestHslToHsla(t *testing.T) {
hsl := Hsl(120, 0.3, 0.6)
assertEqual(t, Hsla(120, 0.3, 0.6, 1.0), hsl.ToHsla())
}
func TestHslaToHsla(t *testing.T) {
hsla := Hsla(120, 0.3, 0.6, 0.8)
assertEqual(t, hsla, hsla.ToHsla())
}
func TestRgbToRgba(t *testing.T) {
rgb := Rgb(100, 40, 230)
assertEqual(t, Rgba(100, 40, 230, 255), rgb.ToRgba())
}
func TestRgbaToRgba(t *testing.T) {
rgba := Rgba(100, 40, 230, 120)
assertEqual(t, Rgba(100, 40, 230, 120), rgba.ToRgba())
}
func TestHslToRgba(t *testing.T) {
black := Hsl(0, 0, 0)
assertEqual(t, Rgba(0, 0, 0, 255), black.ToRgba())
white := Hsl(0, 0, 1)
assertEqual(t, Rgba(255, 255, 255, 255), white.ToRgba())
red := Hsl(0, 1, 0.5)
assertEqual(t, Rgba(255, 0, 0, 255), red.ToRgba())
green := Hsl(120, 1, 0.5)
assertEqual(t, Rgba(0, 255, 0, 255), green.ToRgba())
blue := Hsl(240, 1, 0.5)
assertEqual(t, Rgba(0, 0, 255, 255), blue.ToRgba())
}
func TestHslaToRgba(t *testing.T) {
black := Hsla(0, 0, 0, 1)
assertEqual(t, Rgba(0, 0, 0, 255), black.ToRgba())
black2 := Hsla(0, 1, 0, 1)
assertEqual(t, Rgba(0, 0, 0, 255), black2.ToRgba())
black3 := Hsla(360, 1, 0, 1)
assertEqual(t, Rgba(0, 0, 0, 255), black3.ToRgba())
white := Hsla(0, 0, 1, 1)
assertEqual(t, Rgba(255, 255, 255, 255), white.ToRgba())
red := Hsla(0, 1, 0.5, 1)
assertEqual(t, Rgba(255, 0, 0, 255), red.ToRgba())
green := Hsla(120, 1, 0.5, 1)
assertEqual(t, Rgba(0, 255, 0, 255), green.ToRgba())
blue := Hsla(240, 1, 0.5, 1)
assertEqual(t, Rgba(0, 0, 255, 255), blue.ToRgba())
alpha := Hsla(240, 1, 0.5, 0.49803922)
assertEqual(t, Rgba(0, 0, 255, 127), alpha.ToRgba())
}
func TestDarken(t *testing.T) {
value := Hsla(120, 0.5, 0.5, 1.0)
darkened := Darken(value, 0.1)
assertEqual(t, Hsla(120, 0.5, 0.45, 1.0), darkened)
}
func TestLighten(t *testing.T) {
value := Hsla(120, 0.5, 0.5, 1.0)
lightened := Lighten(value, 0.1)
assertEqual(t, Hsla(120, 0.5, 0.55, 1.0), lightened)
}