| 1 | package sixel |
| 2 | |
| 3 | import ( |
| 4 | "image" |
| 5 | "image/color" |
| 6 | "slices" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | type testCase struct { |
| 11 | maxColors int |
| 12 | expectedPalette []sixelColor |
| 13 | } |
| 14 | |
| 15 | func TestPaletteCreationRedGreen(t *testing.T) { |
| 16 | redGreen := image.NewRGBA(image.Rect(0, 0, 2, 2)) |
| 17 | redGreen.Set(0, 0, color.RGBA{255, 0, 0, 255}) |
| 18 | redGreen.Set(0, 1, color.RGBA{128, 0, 0, 255}) |
| 19 | redGreen.Set(1, 0, color.RGBA{0, 255, 0, 255}) |
| 20 | redGreen.Set(1, 1, color.RGBA{0, 128, 0, 255}) |
| 21 | |
| 22 | testCases := map[string]testCase{ |
| 23 | "way too many colors": { |
| 24 | maxColors: 16, |
| 25 | expectedPalette: []sixelColor{ |
| 26 | {100, 0, 0, 100}, |
| 27 | {50, 0, 0, 100}, |
| 28 | {0, 100, 0, 100}, |
| 29 | {0, 50, 0, 100}, |
| 30 | }, |
| 31 | }, |
| 32 | "just the right number of colors": { |
| 33 | maxColors: 4, |
| 34 | expectedPalette: []sixelColor{ |
| 35 | {100, 0, 0, 100}, |
| 36 | {50, 0, 0, 100}, |
| 37 | {0, 100, 0, 100}, |
| 38 | {0, 50, 0, 100}, |
| 39 | }, |
| 40 | }, |
| 41 | "color reduction": { |
| 42 | maxColors: 2, |
| 43 | expectedPalette: []sixelColor{ |
| 44 | {75, 0, 0, 100}, |
| 45 | {0, 75, 0, 100}, |
| 46 | }, |
| 47 | }, |
| 48 | } |
| 49 | |
| 50 | runTests(t, redGreen, testCases) |
| 51 | } |
| 52 | |
| 53 | func TestPaletteWithSemiTransparency(t *testing.T) { |
| 54 | blueAlpha := image.NewRGBA(image.Rect(0, 0, 2, 2)) |
| 55 | blueAlpha.Set(0, 0, color.RGBA{0, 0, 255, 255}) |
| 56 | blueAlpha.Set(0, 1, color.RGBA{0, 0, 128, 255}) |
| 57 | blueAlpha.Set(1, 0, color.RGBA{0, 0, 255, 128}) |
| 58 | blueAlpha.Set(1, 1, color.RGBA{0, 0, 255, 0}) |
| 59 | |
| 60 | testCases := map[string]testCase{ |
| 61 | "just the right number of colors": { |
| 62 | maxColors: 4, |
| 63 | expectedPalette: []sixelColor{ |
| 64 | {0, 0, 100, 100}, |
| 65 | {0, 0, 50, 100}, |
| 66 | {0, 0, 100, 50}, |
| 67 | {0, 0, 100, 0}, |
| 68 | }, |
| 69 | }, |
| 70 | "color reduction": { |
| 71 | maxColors: 2, |
| 72 | expectedPalette: []sixelColor{ |
| 73 | {0, 0, 75, 100}, |
| 74 | {0, 0, 100, 25}, |
| 75 | }, |
| 76 | }, |
| 77 | } |
| 78 | runTests(t, blueAlpha, testCases) |
| 79 | } |
| 80 | |
| 81 | func runTests(t *testing.T, img image.Image, testCases map[string]testCase) { |
| 82 | for testName, test := range testCases { |
| 83 | t.Run(testName, func(t *testing.T) { |
| 84 | palette := newSixelPalette(img, test.maxColors) |
| 85 | if len(palette.PaletteColors) != len(test.expectedPalette) { |
| 86 | t.Errorf("Expected colors %+v in palette, but got %+v", test.expectedPalette, palette.PaletteColors) |
| 87 | return |
| 88 | } |
| 89 | |
| 90 | for _, c := range test.expectedPalette { |
| 91 | var foundColor bool |
| 92 | if slices.Contains(palette.PaletteColors, c) { |
| 93 | foundColor = true |
| 94 | } |
| 95 | |
| 96 | if !foundColor { |
| 97 | t.Errorf("Expected colors %+v in palette, but got %+v", test.expectedPalette, palette.PaletteColors) |
| 98 | return |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | for lookupRawColor, lookupPaletteColor := range palette.colorConvert { |
| 103 | paletteIndex, inReverseLookup := palette.paletteIndexes[lookupRawColor] |
| 104 | if !inReverseLookup { |
| 105 | t.Errorf("Color %+v maps to color %+v in the colorConvert map, but %+v is does not have a corresponding palette index.", lookupRawColor, lookupPaletteColor, lookupPaletteColor) |
| 106 | return |
| 107 | } |
| 108 | |
| 109 | if paletteIndex >= len(palette.PaletteColors) { |
| 110 | t.Errorf("Image color %+v maps to palette index %d, but there are only %d palette colors.", lookupRawColor, paletteIndex, len(palette.PaletteColors)) |
| 111 | return |
| 112 | } |
| 113 | |
| 114 | colorFromPalette := palette.PaletteColors[paletteIndex] |
| 115 | if colorFromPalette != lookupPaletteColor { |
| 116 | t.Errorf("Image color %+v maps to palette color %+v and palette index %d, but palette index %d is actually palette color %+v", lookupRawColor, lookupPaletteColor, paletteIndex, paletteIndex, colorFromPalette) |
| 117 | return |
| 118 | } |
| 119 | } |
| 120 | }) |
| 121 | } |
| 122 | } |