| 1 |
package sixel |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"image/color" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func TestWriteColor(t *testing.T) { |
| 10 |
tests := []struct { |
| 11 |
name string |
| 12 |
pc int |
| 13 |
pu int |
| 14 |
px int |
| 15 |
py int |
| 16 |
pz int |
| 17 |
expected string |
| 18 |
}{ |
| 19 |
{ |
| 20 |
name: "simple color number", |
| 21 |
pc: 1, |
| 22 |
pu: 0, |
| 23 |
expected: "#1", |
| 24 |
}, |
| 25 |
{ |
| 26 |
name: "RGB color", |
| 27 |
pc: 1, |
| 28 |
pu: 2, |
| 29 |
px: 50, |
| 30 |
py: 60, |
| 31 |
pz: 70, |
| 32 |
expected: "#1;2;50;60;70", |
| 33 |
}, |
| 34 |
{ |
| 35 |
name: "HLS color", |
| 36 |
pc: 2, |
| 37 |
pu: 1, |
| 38 |
px: 180, |
| 39 |
py: 50, |
| 40 |
pz: 100, |
| 41 |
expected: "#2;1;180;50;100", |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "invalid pu > 2", |
| 45 |
pc: 1, |
| 46 |
pu: 3, |
| 47 |
expected: "#1", |
| 48 |
}, |
| 49 |
} |
| 50 |
|
| 51 |
for _, tt := range tests { |
| 52 |
t.Run(tt.name, func(t *testing.T) { |
| 53 |
buf := &bytes.Buffer{} |
| 54 |
n, err := WriteColor(buf, tt.pc, tt.pu, tt.px, tt.py, tt.pz) |
| 55 |
if err != nil { |
| 56 |
t.Errorf("WriteColor() unexpected error = %v", err) |
| 57 |
return |
| 58 |
} |
| 59 |
if got := buf.String(); got != tt.expected { |
| 60 |
t.Errorf("WriteColor() = %v, want %v", got, tt.expected) |
| 61 |
} |
| 62 |
if n != len(tt.expected) { |
| 63 |
t.Errorf("WriteColor() returned length = %v, want %v", n, len(tt.expected)) |
| 64 |
} |
| 65 |
}) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestDecodeColor(t *testing.T) { |
| 70 |
tests := []struct { |
| 71 |
name string |
| 72 |
input []byte |
| 73 |
wantC Color |
| 74 |
wantN int |
| 75 |
}{ |
| 76 |
{ |
| 77 |
name: "simple color number", |
| 78 |
input: []byte("#1"), |
| 79 |
wantC: Color{Pc: 1}, |
| 80 |
wantN: 2, |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "RGB color", |
| 84 |
input: []byte("#1;2;50;60;70"), |
| 85 |
wantC: Color{Pc: 1, Pu: 2, Px: 50, Py: 60, Pz: 70}, |
| 86 |
wantN: 13, |
| 87 |
}, |
| 88 |
{ |
| 89 |
name: "HLS color", |
| 90 |
input: []byte("#2;1;180;50;100"), |
| 91 |
wantC: Color{Pc: 2, Pu: 1, Px: 180, Py: 50, Pz: 100}, |
| 92 |
wantN: 15, |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "empty input", |
| 96 |
input: []byte{}, |
| 97 |
wantC: Color{}, |
| 98 |
wantN: 0, |
| 99 |
}, |
| 100 |
{ |
| 101 |
name: "invalid introducer", |
| 102 |
input: []byte("X1"), |
| 103 |
wantC: Color{}, |
| 104 |
wantN: 0, |
| 105 |
}, |
| 106 |
{ |
| 107 |
name: "incomplete sequence", |
| 108 |
input: []byte("#"), |
| 109 |
wantC: Color{}, |
| 110 |
wantN: 0, |
| 111 |
}, |
| 112 |
} |
| 113 |
|
| 114 |
for _, tt := range tests { |
| 115 |
t.Run(tt.name, func(t *testing.T) { |
| 116 |
gotC, gotN := DecodeColor(tt.input) |
| 117 |
if gotC != tt.wantC { |
| 118 |
t.Errorf("DecodeColor() gotColor = %v, want %v", gotC, tt.wantC) |
| 119 |
} |
| 120 |
if gotN != tt.wantN { |
| 121 |
t.Errorf("DecodeColor() gotN = %v, want %v", gotN, tt.wantN) |
| 122 |
} |
| 123 |
}) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
func TestColor_RGBA(t *testing.T) { |
| 128 |
tests := []struct { |
| 129 |
name string |
| 130 |
color Color |
| 131 |
wantR uint32 |
| 132 |
wantG uint32 |
| 133 |
wantB uint32 |
| 134 |
wantA uint32 |
| 135 |
}{ |
| 136 |
{ |
| 137 |
name: "default color map 0 (black)", |
| 138 |
color: Color{Pc: 0}, |
| 139 |
wantR: 0x0000, |
| 140 |
wantG: 0x0000, |
| 141 |
wantB: 0x0000, |
| 142 |
wantA: 0xFFFF, |
| 143 |
}, |
| 144 |
{ |
| 145 |
name: "RGB mode (50%, 60%, 70%)", |
| 146 |
color: Color{Pc: 1, Pu: 2, Px: 50, Py: 60, Pz: 70}, |
| 147 |
wantR: 0x8080, |
| 148 |
wantG: 0x9999, |
| 149 |
wantB: 0xB3B3, |
| 150 |
wantA: 0xFFFF, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "HLS mode (180°, 50%, 100%)", |
| 154 |
color: Color{Pc: 1, Pu: 1, Px: 180, Py: 50, Pz: 100}, |
| 155 |
wantR: 0x0000, |
| 156 |
wantG: 0xFFFF, |
| 157 |
wantB: 0xFFFF, |
| 158 |
wantA: 0xFFFF, |
| 159 |
}, |
| 160 |
} |
| 161 |
|
| 162 |
for _, tt := range tests { |
| 163 |
t.Run(tt.name, func(t *testing.T) { |
| 164 |
gotR, gotG, gotB, gotA := tt.color.RGBA() |
| 165 |
if gotR != tt.wantR { |
| 166 |
t.Errorf("Color.RGBA() gotR = %v, want %v", gotR, tt.wantR) |
| 167 |
} |
| 168 |
if gotG != tt.wantG { |
| 169 |
t.Errorf("Color.RGBA() gotG = %v, want %v", gotG, tt.wantG) |
| 170 |
} |
| 171 |
if gotB != tt.wantB { |
| 172 |
t.Errorf("Color.RGBA() gotB = %v, want %v", gotB, tt.wantB) |
| 173 |
} |
| 174 |
if gotA != tt.wantA { |
| 175 |
t.Errorf("Color.RGBA() gotA = %v, want %v", gotA, tt.wantA) |
| 176 |
} |
| 177 |
}) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func TestSixelRGB(t *testing.T) { |
| 182 |
tests := []struct { |
| 183 |
name string |
| 184 |
r int |
| 185 |
g int |
| 186 |
b int |
| 187 |
want color.Color |
| 188 |
}{ |
| 189 |
{ |
| 190 |
name: "black", |
| 191 |
r: 0, |
| 192 |
g: 0, |
| 193 |
b: 0, |
| 194 |
want: color.NRGBA{R: 0, G: 0, B: 0, A: 255}, |
| 195 |
}, |
| 196 |
{ |
| 197 |
name: "white", |
| 198 |
r: 100, |
| 199 |
g: 100, |
| 200 |
b: 100, |
| 201 |
want: color.NRGBA{R: 255, G: 255, B: 255, A: 255}, |
| 202 |
}, |
| 203 |
{ |
| 204 |
name: "red", |
| 205 |
r: 100, |
| 206 |
g: 0, |
| 207 |
b: 0, |
| 208 |
want: color.NRGBA{R: 255, G: 0, B: 0, A: 255}, |
| 209 |
}, |
| 210 |
{ |
| 211 |
name: "half intensity", |
| 212 |
r: 50, |
| 213 |
g: 50, |
| 214 |
b: 50, |
| 215 |
want: color.NRGBA{R: 128, G: 128, B: 128, A: 255}, |
| 216 |
}, |
| 217 |
} |
| 218 |
|
| 219 |
for _, tt := range tests { |
| 220 |
t.Run(tt.name, func(t *testing.T) { |
| 221 |
got := sixelRGB(tt.r, tt.g, tt.b) |
| 222 |
gotR, gotG, gotB, gotA := got.RGBA() |
| 223 |
wantR, wantG, wantB, wantA := tt.want.RGBA() |
| 224 |
if gotR != wantR || gotG != wantG || gotB != wantB || gotA != wantA { |
| 225 |
t.Errorf("sixelRGB() = %v, want %v", got, tt.want) |
| 226 |
} |
| 227 |
}) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
func TestSixelHLS(t *testing.T) { |
| 232 |
tests := []struct { |
| 233 |
name string |
| 234 |
h int |
| 235 |
l int |
| 236 |
s int |
| 237 |
want color.Color |
| 238 |
}{ |
| 239 |
{ |
| 240 |
name: "black", |
| 241 |
h: 0, |
| 242 |
l: 0, |
| 243 |
s: 0, |
| 244 |
want: color.NRGBA{R: 0, G: 0, B: 0, A: 255}, |
| 245 |
}, |
| 246 |
{ |
| 247 |
name: "white", |
| 248 |
h: 0, |
| 249 |
l: 100, |
| 250 |
s: 0, |
| 251 |
want: color.NRGBA{R: 255, G: 255, B: 255, A: 255}, |
| 252 |
}, |
| 253 |
{ |
| 254 |
name: "pure red", |
| 255 |
h: 0, |
| 256 |
l: 50, |
| 257 |
s: 100, |
| 258 |
want: color.NRGBA{R: 255, G: 0, B: 0, A: 255}, |
| 259 |
}, |
| 260 |
{ |
| 261 |
name: "pure green", |
| 262 |
h: 120, |
| 263 |
l: 50, |
| 264 |
s: 100, |
| 265 |
want: color.NRGBA{R: 0, G: 255, B: 0, A: 255}, |
| 266 |
}, |
| 267 |
{ |
| 268 |
name: "pure blue", |
| 269 |
h: 240, |
| 270 |
l: 50, |
| 271 |
s: 100, |
| 272 |
want: color.NRGBA{R: 0, G: 0, B: 255, A: 255}, |
| 273 |
}, |
| 274 |
} |
| 275 |
|
| 276 |
for _, tt := range tests { |
| 277 |
t.Run(tt.name, func(t *testing.T) { |
| 278 |
got := sixelHLS(tt.h, tt.l, tt.s) |
| 279 |
gotR, gotG, gotB, gotA := got.RGBA() |
| 280 |
wantR, wantG, wantB, wantA := tt.want.RGBA() |
| 281 |
if gotR != wantR || gotG != wantG || gotB != wantB || gotA != wantA { |
| 282 |
t.Errorf("sixelHLS() = %v, want %v", got, tt.want) |
| 283 |
} |
| 284 |
}) |
| 285 |
} |
| 286 |
} |