-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcolor_test.go
144 lines (111 loc) · 3.54 KB
/
color_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
package charts
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/image/colornames"
)
func BenchmarkParseColor(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = ParseColor("#333")
_ = ParseColor("#313233")
_ = ParseColor("rgb(31,32,33)")
_ = ParseColor("rgba(50,51,52,250)")
}
}
func BenchmarkColorString(b *testing.B) {
c := ParseColor("rgb(31,32,33)")
for i := 0; i < b.N; i++ {
_ = c.String()
}
}
const makeColorShadeSamples = false
func testColorShades(t *testing.T, colors ...Color) {
if !makeColorShadeSamples {
return // color samples are generated through a test failure
}
t.Helper()
p := NewPainter(PainterOptions{
OutputFormat: ChartOutputSVG,
Width: 600,
Height: 400,
})
sampleWidth := p.Width() / len(colors)
for i, c := range colors {
endX := (i + 1) * sampleWidth
if i == len(colors)-1 {
endX = p.Width() // ensure edge is painted
}
p.FilledRect(i*sampleWidth, 0, endX, p.Height(),
c, ColorTransparent, 0.0)
}
data, err := p.Bytes()
require.NoError(t, err)
assertEqualSVG(t, "", data)
}
func TestGrayColors(t *testing.T) {
testColorShades(t, ColorDarkGray, ColorGray, ColorSilver, ColorLightGray, ColorAzure,
ColorSlateGray, ColorLightSlateGray)
}
func TestBlueColors(t *testing.T) {
testColorShades(t, ColorBlue, ColorNavy, ColorBlueAlt1, ColorBlueAlt2, ColorLightSlateGray)
}
func TestGreenColors(t *testing.T) {
testColorShades(t, ColorGreen, ColorOlive, ColorLime,
ColorGreenAlt1, ColorGreenAlt2, ColorGreenAlt3, ColorGreenAlt4)
}
func TestRedColors(t *testing.T) {
testColorShades(t, ColorRed, ColorPink, ColorSalmon, ColorMaroon, ColorBrown, ColorChocolate,
ColorRedAlt1, ColorRedAlt2)
}
func TestOrangeColors(t *testing.T) {
testColorShades(t, ColorOrange, ColorOrangeAlt1, ColorOrangeAlt2, ColorOrangeAlt3)
}
func TestAquaColors(t *testing.T) {
testColorShades(t, ColorAqua, ColorTeal, ColorTurquoise, ColorAquaAlt1)
}
func TestYellowColors(t *testing.T) {
testColorShades(t, ColorYellow, ColorGold, ColorYellowAlt1)
}
func TestTanColors(t *testing.T) {
testColorShades(t, ColorAzure, ColorIvory, ColorBeige, ColorKhaki, ColorTan, ColorCoral, ColorSalmon)
}
func TestPurpleColors(t *testing.T) {
testColorShades(t, ColorPurple, ColorViolet, ColorIndigo, ColorPlum, ColorFuchsia)
}
func TestIsLightColor(t *testing.T) {
t.Parallel()
assert.True(t, isLightColor(Color{R: 255, G: 255, B: 255}))
assert.True(t, isLightColor(Color{R: 145, G: 204, B: 117}))
assert.False(t, isLightColor(Color{R: 88, G: 112, B: 198}))
assert.False(t, isLightColor(Color{R: 0, G: 0, B: 0}))
assert.False(t, isLightColor(Color{R: 16, G: 12, B: 42}))
}
func TestParseColor(t *testing.T) {
t.Parallel()
c := ParseColor("")
assert.True(t, c.IsZero())
c = ParseColor("unknown")
assert.Equal(t, ColorBlack, c)
c = ParseColor("#333")
assert.Equal(t, Color{R: 51, G: 51, B: 51, A: 255}, c)
c = ParseColor("#313233")
assert.Equal(t, Color{R: 49, G: 50, B: 51, A: 255}, c)
c = ParseColor("rgb(31,32,33)")
assert.Equal(t, Color{R: 31, G: 32, B: 33, A: 255}, c)
c = ParseColor("rgba(50,51,52,.981)")
assert.Equal(t, Color{R: 50, G: 51, B: 52, A: 250}, c)
c = ParseColor("rgba(50,51,52,250)")
assert.Equal(t, Color{R: 50, G: 51, B: 52, A: 250}, c)
}
func TestColorConvertGo(t *testing.T) {
t.Parallel()
goC := colornames.Lavender
ourC := ColorConvertGo(goC)
goR, goG, goB, goA := goC.RGBA()
ourR, ourG, ourB, ourA := ourC.RGBA()
assert.Equal(t, goR, ourR)
assert.Equal(t, goG, ourG)
assert.Equal(t, goB, ourB)
assert.Equal(t, goA, ourA)
}