fix(convert): zero-pad each channel in RgbToHex
RgbToHex emitted a single hex digit for channel values below 0x10 because strconv.FormatInt produces the minimal representation. This broke the fixed-width hex format, the round-trip with HexToRgb, and the hex2basicMap fast path in Rgb2basic. Format each channel with %02x, matching RgbTo256.
This commit is contained in:
1 parent
9df61ac4fe
commit
f78554106d
3 files changed
+84
-1
No files matched your search
@@ -119,12 +119,21 @@ func TestRgbToHex(t *testing.T) {
|
||||
{"cccccc", []int{204, 204, 204}},
|
||||
{"aabbcc", []int{170, 187, 204}},
|
||||
{"aa99cd", []int{170, 153, 205}},
|
||||
// channels below 0x10 must be zero-padded to two hex digits
|
||||
{"000000", []int{0, 0, 0}},
|
||||
{"010203", []int{1, 2, 3}},
|
||||
{"0a0b0c", []int{10, 11, 12}},
|
||||
}
|
||||
|
||||
for _, item := range tests {
|
||||
assert.Equal(t, RgbToHex(item.given), item.want)
|
||||
assert.Equal(t, Rgb2hex(item.given), item.want)
|
||||
}
|
||||
|
||||
// RgbToHex must be the inverse of HexToRgb, including for small channels.
|
||||
for _, hex := range []string{"010203", "000000", "0a0b0c", "aabbcc"} {
|
||||
assert.Equal(t, RgbToHex(HexToRgb(hex)), hex)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRgbToAnsi(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user