Debian Codegraph @

ansi/color_test.go

raw
1 package ansi
2
3 import (
4 "image/color"
5 "testing"
6
7 "github.com/lucasb-eyer/go-colorful"
8 )
9
10 func TestRGBAToHex(t *testing.T) {
11 cases := []struct {
12 r, g, b, a uint32
13 want uint32
14 }{
15 {0, 0, 255, 0xffff, 0x0000ff},
16 {255, 255, 255, 0xffff, 0xffffff},
17 {255, 0, 0, 0xffff, 0xffff0000},
18 }
19
20 for _, c := range cases {
21 gotR, gotG, gotB, _ := TrueColor(c.want).RGBA()
22 gotR /= 256
23 gotG /= 256
24 gotB /= 256
25 if gotR != c.r || gotG != c.g || gotB != c.b {
26 t.Errorf("RGBA() of TrueColor(%06x): got (%v, %v, %v), want (%v, %v, %v)",
27 c.want, gotR, gotG, gotB, c.r, c.g, c.b)
28 }
29 }
30 }
31
32 func TestColorToHexString(t *testing.T) {
33 cases := []struct {
34 color color.Color
35 want string
36 }{
37 {TrueColor(0x0000ff), "#0000ff"},
38 {TrueColor(0xffffff), "#ffffff"},
39 {TrueColor(0xff0000), "#ff0000"},
40 }
41
42 for _, c := range cases {
43 got := colorToHexString(c.color)
44 if got != c.want {
45 t.Errorf("colorToHexString(%v): got %v, want %v", c.color, got, c.want)
46 }
47 }
48 }
49
50 func TestAnsiToRGB(t *testing.T) {
51 cases := []struct {
52 ansi byte
53 r, g, b uint32
54 }{
55 {0, 0, 0, 0}, // black
56 {1, 128, 0, 0}, // red
57 {255, 238, 238, 238}, // highest ANSI color (grayscale)
58 }
59
60 for _, c := range cases {
61 gotR, gotG, gotB, _ := ansiToRGB(c.ansi).RGBA()
62 // We need to shift the values down to 8 bits
63 gotR >>= 8
64 gotR &= 0xff
65 gotG >>= 8
66 gotG &= 0xff
67 gotB >>= 8
68 gotB &= 0xff
69 if gotR != c.r || gotG != c.g || gotB != c.b {
70 t.Errorf("ansiToRGB(%v): got (%v, %v, %v), want (%v, %v, %v)",
71 c.ansi, gotR, gotG, gotB, c.r, c.g, c.b)
72 }
73 }
74 }
75
76 func TestHexToRGB(t *testing.T) {
77 cases := []struct {
78 hex uint32
79 r, g, b uint32
80 }{
81 {0x0000FF, 0, 0, 255}, // blue
82 {0xFFFFFF, 255, 255, 255}, // white
83 {0xFF0000, 255, 0, 0}, // red
84 }
85
86 for _, c := range cases {
87 gotR, gotG, gotB := hexToRGB(c.hex)
88 if gotR != c.r || gotG != c.g || gotB != c.b {
89 t.Errorf("hexToRGB(%v): got (%v, %v, %v), want (%v, %v, %v)",
90 c.hex, gotR, gotG, gotB, c.r, c.g, c.b)
91 }
92 }
93 }
94
95 func TestHexTo256(t *testing.T) {
96 testCases := map[string]struct {
97 input colorful.Color
98 expectedHex string
99 expectedOutput IndexedColor
100 }{
101 "white": {
102 input: colorful.Color{R: 1, G: 1, B: 1},
103 expectedHex: "#ffffff",
104 expectedOutput: 231,
105 },
106 "offwhite": {
107 input: colorful.Color{R: 0.9333, G: 0.9333, B: 0.933},
108 expectedHex: "#eeeeee",
109 expectedOutput: 255,
110 },
111 "slightly brighter than offwhite": {
112 input: colorful.Color{R: 0.95, G: 0.95, B: 0.95},
113 expectedHex: "#f2f2f2",
114 expectedOutput: 255,
115 },
116 "red": {
117 input: colorful.Color{R: 1, G: 0, B: 0},
118 expectedHex: "#ff0000",
119 expectedOutput: 196,
120 },
121 "silver foil": {
122 input: colorful.Color{R: 0.6863, G: 0.6863, B: 0.6863},
123 expectedHex: "#afafaf",
124 expectedOutput: 145,
125 },
126 "silver chalice": {
127 input: colorful.Color{R: 0.698, G: 0.698, B: 0.698},
128 expectedHex: "#b2b2b2",
129 expectedOutput: 249,
130 },
131 "slightly closer to silver foil": {
132 input: colorful.Color{R: 0.692, G: 0.692, B: 0.692},
133 expectedHex: "#b0b0b0",
134 expectedOutput: 145,
135 },
136 "slightly closer to silver chalice": {
137 input: colorful.Color{R: 0.694, G: 0.694, B: 0.694},
138 expectedHex: "#b1b1b1",
139 expectedOutput: 249,
140 },
141 "gray": {
142 input: colorful.Color{R: 0.5, G: 0.5, B: 0.5},
143 expectedHex: "#808080",
144 expectedOutput: 244,
145 },
146 }
147
148 for testName, testCase := range testCases {
149 t.Run(testName, func(t *testing.T) {
150 // hex := fmt.Sprintf("#%02x%02x%02x", uint8(testCase.input.R*255), uint8(testCase.input.G*255), uint8(testCase.input.B*255))
151 output := Convert256(testCase.input)
152 if testCase.input.Hex() != testCase.expectedHex {
153 t.Errorf("Expected %+v to map to %s, but instead received %s", testCase.input, testCase.expectedHex, testCase.input.Hex())
154 }
155 if output != testCase.expectedOutput {
156 t.Errorf("Expected truecolor %+v to map to 256 color %d, but instead received %d", testCase.input, testCase.expectedOutput, output)
157 }
158 })
159 }
160 }