From 67dc0ccf40e9c2c5e991d286c340bf19c3b58fe7 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Fri, 14 Nov 2025 16:25:47 -0500 Subject: [PATCH] fix eee bug Signed-off-by: Jason Hall --- color_test.go | 12 ++++++++++++ quantize.go | 9 ++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/color_test.go b/color_test.go index 009d78d..548914e 100644 --- a/color_test.go +++ b/color_test.go @@ -93,6 +93,18 @@ func TestParseHex(t *testing.T) { wantANSI: 244, // Gray in ANSI 256 grayscale ramp wantErr: false, }, + { + desc: "near-white (eee)", + hex: "eee", + wantANSI: 231, // Should map to white, not black + wantErr: false, + }, + { + desc: "near-white (eeeeee)", + hex: "eeeeee", + wantANSI: 231, // Should map to white, not black + wantErr: false, + }, { desc: "invalid length (too short)", hex: "ff", diff --git a/quantize.go b/quantize.go index 608d09c..b29df70 100644 --- a/quantize.go +++ b/quantize.go @@ -35,12 +35,15 @@ func quantizeGrayscale(r, g, b uint8) uint8 { if avg < 8 { return 16 // Black from RGB cube } - if avg > 238 { + if avg >= 238 { return 231 // White from RGB cube } - // Map [8, 238] to [0, 23] - step := (avg - 8) * 24 / 230 + // Map [8, 237] to [0, 23] + step := (avg - 8) * 24 / 229 + if step > 23 { + step = 23 + } return 232 + uint8(step) }