summaryrefslogtreecommitdiff
path: root/internal/colors/colors_test.go
blob: 569d7e2b1c8a78468df16af61137d660e4ef3f22 (plain)
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
package colors

import (
	"math"
	"testing"
)

const (
	PCmaxDelta     = 1e-8
	AssertTemplate = "Testing '%v'. Expected %v to become: %v Got: %v"
)

type equivalentColors struct {
	name string
	pc   PreciseColor
	rgb  RGB
	cmyk CMYK
	hsl  HSL
}

func pcDeltaOk(a, b PreciseColor) bool {
	return math.Abs(a.R-b.R) < PCmaxDelta &&
		math.Abs(a.G-b.G) < PCmaxDelta &&
		math.Abs(a.B-b.B) < PCmaxDelta
}

func getEquivalents() []equivalentColors {
	return []equivalentColors{
		// Black & White {{{
		{
			"Pure White",
			PreciseColor{1, 1, 1},
			RGB{255, 255, 255},
			CMYK{0, 0, 0, 0},
			HSL{0, 0, 100},
		},
		{
			"Pure Black",
			PreciseColor{0, 0, 0},
			RGB{0, 0, 0},
			CMYK{0, 0, 0, 100},
			HSL{0, 0, 0},
		},
		// }}}
		// Pure RGB {{{
		{
			"Red",
			PreciseColor{1, 0, 0},
			RGB{255, 0, 0},
			CMYK{0, 100, 100, 0},
			HSL{0, 100, 50},
		},
		{
			"Green",
			PreciseColor{0, 1, 0},
			RGB{0, 255, 0},
			CMYK{100, 0, 100, 0},
			HSL{120, 100, 50},
		},
		{
			"Blue",
			PreciseColor{0, 0, 1},
			RGB{0, 0, 255},
			CMYK{100, 100, 0, 0},
			HSL{240, 100, 50},
		},
		// }}}
		// Pure CMYK {{{
		{
			"Cyan",
			PreciseColor{0, 1, 1},
			RGB{0, 255, 255},
			CMYK{100, 0, 0, 0},
			HSL{180, 100, 50},
		},
		{
			"Magenta",
			PreciseColor{1, 0, 1},
			RGB{255, 0, 255},
			CMYK{0, 100, 0, 0},
			HSL{300, 100, 50},
		},
		{
			"Yellow",
			PreciseColor{1, 1, 0},
			RGB{255, 255, 0},
			CMYK{0, 0, 100, 0},
			HSL{60, 100, 50},
		},
		// note: Black is already tested
		// }}}
		// TODO: add less pure colors to test luminance and saturation better
	}
}

func TestToPreciseColor(t *testing.T) {
	for _, ce := range getEquivalents() {
		target := ce.pc
		for _, cs := range []ColorSpace{ce.rgb, ce.cmyk, ce.hsl} {
			pc := cs.ToPrecise()
			if !pcDeltaOk(pc, target) {
				t.Errorf(AssertTemplate, ce.name, cs, target, pc)
			}
		}
	}
}

func TestToRgb(t *testing.T) {
	for _, ce := range getEquivalents() {
		target := ce.rgb
		for _, cs := range []ColorSpace{ce.pc, ce.cmyk, ce.hsl} {
			rgb := RGB{}.FromPrecise(cs.ToPrecise()).(RGB)
			if rgb != target {
				t.Errorf(AssertTemplate, ce.name, cs, target, rgb)
			}
		}
	}
}

func TestToCmyk(t *testing.T) {
	for _, ce := range getEquivalents() {
		target := ce.cmyk
		for _, cs := range []ColorSpace{ce.pc, ce.rgb, ce.hsl} {
			cmyk := CMYK{}.FromPrecise(cs.ToPrecise()).(CMYK)
			if cmyk != target {
				t.Errorf(AssertTemplate, ce.name, cs, target, cmyk)
			}
		}
	}
}

func TestToHsl(t *testing.T) {
	for _, ce := range getEquivalents() {
		target := ce.hsl
		for _, cs := range []ColorSpace{ce.pc, ce.rgb, ce.cmyk} {
			hsl := HSL{}.FromPrecise(cs.ToPrecise()).(HSL)
			if hsl != target {
				t.Errorf(AssertTemplate, ce.name, cs, target, hsl)
			}
		}
	}
}