fix(convert): zero-pad each channel in RgbToHex - #120
Merged
Merged
Conversation
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.
dualfroz
force-pushed
the
dualfroz/rgbtohex-zeropad
branch
from
September 5, 2026 22:44
c6ab3d8 to
3be6cf3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
RgbToHex(and its aliasRgb2hex) does not zero-pad channel values below0x10, so a single hex digit is emitted for those channels:This breaks two contracts:
The round-trip with
HexToRgbis no longer an identity for colors with anychannel below 16:
Rgb2basiclooks a color up inhex2basicMap, whose keys are allfixed-width 6-digit hex strings (e.g. "000000"). Because
RgbToHex(0,0,0)returns "000", pure black never matches the fast-path map and silently falls
through to the approximate
RgbToAnsi.The sibling converter
RgbTo256already builds its hex key withfmt.Sprintf("%02x%02x%02x", ...), so this is an inconsistency within the samefile.
Root cause
convert.go,RgbToHex:strconv.FormatInt(_, 16)produces the minimal representation, i.e. one digitfor values 0-15.
Fix
Format each channel as a fixed two-digit hex value:
fmtis already imported in the file. Existing values >= 16 are unchanged.Test
Extended
TestRgbToHexinutils_test.gowith small-channel cases(
{0,0,0},{1,2,3},{10,11,12}) and an explicitRgbToHex(HexToRgb(hex)) == hexround-trip check.go test ./-> ok (PASS)go vet ./...-> exit 0go build ./...-> exit 0Counterfactual: reverting
RgbToHextostrconv.FormatInt(int64(v), 16)makesthe new test fail with
expect: "abc"vsactual: "0a0b0c".Note:
gofmt -l convert.goreports the file, but this is pre-existing (a doccomment near line 866, unrelated to this change and present on the base commit).
The changed region is gofmt-clean, so unrelated lines were intentionally left
untouched.