| 1 | package png2svg |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestShortenColor(t *testing.T) { |
| 9 | tests := []struct { |
| 10 | name string |
| 11 | hexColorBytes []byte |
| 12 | colorOptimize bool |
| 13 | expectedOutput []byte |
| 14 | }{ |
| 15 | { |
| 16 | name: "No shorten #0c0000 with colorOptimize false", |
| 17 | hexColorBytes: []byte("#0c0000"), |
| 18 | colorOptimize: false, |
| 19 | expectedOutput: []byte("#0c0000"), |
| 20 | }, |
| 21 | { |
| 22 | name: "Lossy shorten #0c0000 with colorOptimize true", |
| 23 | hexColorBytes: []byte("#0c0000"), |
| 24 | colorOptimize: true, |
| 25 | expectedOutput: []byte("#000"), // Rounded to nearest single hex digit equivalent |
| 26 | }, |
| 27 | { |
| 28 | name: "Lossless shorten #ffffff with colorOptimize true", |
| 29 | hexColorBytes: []byte("#ffffff"), |
| 30 | colorOptimize: true, |
| 31 | expectedOutput: []byte("#fff"), // Lossless compression as each pair is identical |
| 32 | }, |
| 33 | { |
| 34 | name: "No shorten #ffffff with colorOptimize false", |
| 35 | hexColorBytes: []byte("#ffffff"), |
| 36 | colorOptimize: false, |
| 37 | expectedOutput: []byte("#fff"), // Should still shorten losslessly |
| 38 | }, |
| 39 | { |
| 40 | name: "Lossy shorten #112233 with colorOptimize true", |
| 41 | hexColorBytes: []byte("#112233"), |
| 42 | colorOptimize: true, |
| 43 | expectedOutput: []byte("#123"), // Each pair different, so simplified to nearest single hex equivalent |
| 44 | }, |
| 45 | { |
| 46 | name: "No shorten #123456 with colorOptimize false", |
| 47 | hexColorBytes: []byte("#123456"), |
| 48 | colorOptimize: false, |
| 49 | expectedOutput: []byte("#123456"), // No simplification as no pairs are identical |
| 50 | }, |
| 51 | } |
| 52 | |
| 53 | for _, tt := range tests { |
| 54 | t.Run(tt.name, func(t *testing.T) { |
| 55 | var result []byte |
| 56 | if tt.colorOptimize { |
| 57 | result = shortenColorLossy(tt.hexColorBytes) |
| 58 | } else { |
| 59 | result = shortenColorLossless(tt.hexColorBytes) |
| 60 | } |
| 61 | if !reflect.DeepEqual(result, tt.expectedOutput) { |
| 62 | t.Errorf("shortenColor(%s, %v) = %s, want %s", tt.hexColorBytes, tt.colorOptimize, result, tt.expectedOutput) |
| 63 | } |
| 64 | }) |
| 65 | } |
| 66 | } |