---
title: "gookit/color: zero-padded RGB to hex in Go | VulCode"
description: "A fix to gookit/color in Go: RgbToHex zero-pads channels below 16, so black is 000000 rather than 000 and a round trip through HexToRgb gives the same value."
url: "https://vulcode.com/en/opensource/contributions/gookit-color"
lang: "en"
---

[All contributions](https://vulcode.com/en/opensource/contributions)

gookit/color

# gookit/color

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

[See the repository](https://github.com/gookit/color) [All contributions](https://vulcode.com/en/opensource/contributions)

- **Language**: Go
- **GitHub stars**: 1,606
- **Merged changes**: 1
- **Project site**: [gookit.github.io](https://gookit.github.io/color/)

## 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](https://github.com/gookit/color/pull/120)

## Merged change 1

- [#120 fix(convert): zero-pad each channel in RgbToHex Merged 7 September 2026](https://github.com/gookit/color/pull/120)

## 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](https://vulcode.com/en/technologies/go)

## Other projects we write code for

- [**urfave/cli** Go · 24,329 stars A declarative Go package for building command line tools.](https://vulcode.com/en/opensource/contributions/urfave-cli)

- [**chezmoi** Go · 22,342 stars A dotfiles manager that keeps the same settings on many different machines, securely.](https://vulcode.com/en/opensource/contributions/chezmoi)

- [**goquery** Go · 14,995 stars A Go library for traversing and changing HTML documents in the style of jQuery, built on the golang.org/x/net/html parser.](https://vulcode.com/en/opensource/contributions/goquery)

- [**Ebitengine** Go · 13,647 stars A 2D game engine in Go, used in games released for computers, phones, browsers and consoles.](https://vulcode.com/en/opensource/contributions/ebiten)

- [**Fission** Go · 8,950 stars A serverless functions framework for Kubernetes that runs code as functions and scales them down to zero when they are idle.](https://vulcode.com/en/opensource/contributions/fission)

- [**Concourse** Go · 7,944 stars A container-based automation system written in Go and used mostly for CI/CD. Pipelines and tasks are described in YAML files.](https://vulcode.com/en/opensource/contributions/concourse)

## Describe your project.

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

### Rather skip the form?

Ask in the chat or write to us directly.

Open the chat

- **Email**: [hello@vulcode.com](mailto:hello@vulcode.com)
- **Phone**: [+48 576 043 920](tel:+48576043920)
- **Discord**: [discord.gg/vulcode](https://discord.gg/vulcode)
