-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhsv_test.go
134 lines (118 loc) · 2.9 KB
/
hsv_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
package color_test
import (
"fmt"
"math/rand"
"testing"
"github.com/shelepuginivan/color"
"github.com/stretchr/testify/assert"
)
func TestHSV(t *testing.T) {
assert.Implements(t, (*color.Color)(nil), color.HSV{})
}
func TestNewHSV(t *testing.T) {
tests := []struct {
h, s, v int
expected *color.HSV
}{
{0, 100, 100, &color.HSV{H: 0, S: 100, V: 100}},
{360, 50, 50, &color.HSV{H: 0, S: 50, V: 50}},
{180, 200, 150, &color.HSV{H: 180, S: 100, V: 100}},
}
for _, c := range tests {
actual := color.NewHSV(c.h, c.s, c.v)
assert.Equal(t, c.expected, actual)
}
}
func TestHSV_HSL(t *testing.T) {
tests := []struct {
color *color.HSV
expected *color.HSL
}{
{&color.HSV{H: 0, S: 100, V: 100}, &color.HSL{H: 0, S: 100, L: 50}},
{&color.HSV{H: 120, S: 100, V: 100}, &color.HSL{H: 120, S: 100, L: 50}},
{&color.HSV{H: 240, S: 100, V: 100}, &color.HSL{H: 240, S: 100, L: 50}},
{&color.HSV{H: 0, S: 0, V: 0}, &color.HSL{H: 0, S: 0, L: 0}},
{&color.HSV{H: 0, S: 0, V: 100}, &color.HSL{H: 0, S: 0, L: 100}},
}
for _, c := range tests {
actual := c.color.HSL()
assert.Equal(t, c.expected, actual)
}
}
func TestHSV_RGB(t *testing.T) {
tests := []struct {
color *color.HSV
expected *color.RGB
}{
{&color.HSV{0, 100, 100}, &color.RGB{255, 0, 0}},
{&color.HSV{120, 100, 100}, &color.RGB{0, 255, 0}},
{&color.HSV{240, 100, 100}, &color.RGB{0, 0, 255}},
{&color.HSV{300, 100, 50}, &color.RGB{128, 0, 128}},
}
for _, c := range tests {
actual := c.color.RGB()
assert.Equal(t, c.expected, actual)
}
}
func TestHSV_Edit(t *testing.T) {
cases := []struct {
color *color.HSL
expected *color.HSL
editfn func(*color.HSL)
}{
{
color: &color.HSL{H: 120, S: 100, L: 50},
expected: &color.HSL{H: 120, S: 50, L: 50},
editfn: func(c *color.HSL) {
c.S = 50
},
},
{
color: &color.HSL{H: 240, S: 100, L: 50},
expected: &color.HSL{H: 240, S: 100, L: 75},
editfn: func(c *color.HSL) {
c.L = 75
},
},
{
color: &color.HSL{H: 0, S: 0, L: 0},
expected: &color.HSL{H: 0, S: 0, L: 100},
editfn: func(c *color.HSL) {
c.L = 100
},
},
{
color: &color.HSL{H: 360, S: 100, L: 100},
expected: &color.HSL{H: 360, S: 100, L: 0},
editfn: func(c *color.HSL) {
c.L = 0
},
},
}
for _, c := range cases {
actual := c.color.Edit(c.editfn)
assert.Equal(t, c.expected, actual)
assert.Equal(t, c.expected, c.color)
}
}
func ExampleHSV_Edit() {
green := color.NewHSV(72, 100, 40) // rgb(78, 102, 0)
// Increase value and print as RGB.
fmt.Println(green.Edit(func(c *color.HSV) {
c.V = 100
}).RGB()) // Output: rgb(204, 255, 0)
}
func TestHSV_String(t *testing.T) {
for range 1000 {
var (
h = rand.Intn(360)
s = rand.Intn(101)
v = rand.Intn(101)
)
var (
expected = fmt.Sprintf("hsv(%d, %d%%, %d%%)", h, s, v)
actual = color.NewHSV(h, s, v).String()
)
assert.Equal(t, expected, actual)
}
}