All contributions

gookit/color

gookit/color

A Go library for terminal colours: 16 and 256 colours plus RGB, with conversions between formats and Windows support.

Language
Go
GitHub stars
1,606
Merged changes
1
Project site
gookit.github.io

What we changed

RgbToHex zero-pads each channel, so dark colours produce a correct six-digit hex value and a round trip through HexToRgb returns the same value.

What this work shows

  • Number formatting with fmt
  • RGB and hex colour conversion
  • Round-trip conversion tests
What was broken

Dark colours lost digits in hex

RgbToHex wrote each channel as the shortest hex number, so values below 16 had one digit. The colour 1, 2, 3 gave 123 instead of 010203, and black gave 000 instead of 000000, so Rgb2basic missed it in its lookup table and fell back to an approximate colour.

How we fixed it

Every channel gets two digits

Each channel is formatted as %02x, as RgbTo256 in the same file already does. A test covers small channels and checks that RgbToHex(HexToRgb(hex)) returns the same value.

#120

Merged change 1

  1. #120 fix(convert): zero-pad each channel in RgbToHex Merged 7 September 2026

What it taught us

A hex colour has a fixed width, and a channel below 16 without padding shifts every channel after it. We catch this kind of bug with a round-trip test: the converted value has to come back to where it started.

More about this technology: Golang

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.