feat: add OKLCH-aware palette generation (#5)

* chore: update deps, fix escape codes, expand tests, add README

- Bump Go 1.18 → 1.26, simplecolorpalettes v0.9.5 → v0.9.7
- Fix swapped fg/bg ANSI escape code values in trueColorString
- Expand test suite from 2 to 13 tests (determinism, positivity, color assignment, background contrast, stringer palette)
- Add README with usage examples

* feat: add OKLCH-aware palette generation

Add GenerateOKLCHPalette() which creates n evenly-spaced colors in the
OKLCH color space at a given lightness and chroma. This produces
perceptually uniform palettes where all colors appear equally bright.

Uses the new OKLCH support from simplecolorpalettes.

Note: go.mod contains a replace directive for local development that
must be removed before merge (after simplecolorpalettes is published).

* build: update simplecolorpalettes to v0.9.8 (charmtone palette)
This commit is contained in:
2026-02-22 22:02:46 -05:00
committed by GitHub
parent e3c3a88354
commit bf06224d7a
7 changed files with 280 additions and 21 deletions

View File

@@ -1,17 +1,50 @@
# colorhash
Deterministic color assignment from arbitrary input. Feed it a string or byte stream and get back a consistent color every time.
- Take in arbitrary input and return a deterministic color
- Color chosen can be limited in several ways:
- only visually / noticibly distinct colors to choose from
- Color exclusions
- dynamic color exclusions (optional terminal context)
- colors within different terminal support classes (i.e. term-256)
## Features
- Offer to return Hex codes (6 digits or 3)
- Offer to return ascii escape codes
- If the input is text, offer to wrap the input text and return the output as a string
- **Deterministic** — same input always produces the same color
- **Pluggable palettes** — bring your own `ColorSet` or use [simplecolorpalettes](https://github.com/taigrr/simplecolorpalettes)
- **Multiple output formats** — `color.Color`, ANSI escape codes, true-color terminal strings
- **String and byte input** — hash strings directly or stream bytes via `io.Reader`
## Install
1. take input as bytes
1. hash the input
1. use modulo against the sum to choose the color to return from the subset of colors selected.
```bash
go get github.com/taigrr/colorhash
```
## Usage
### Hash a string to a color
```go
import (
"github.com/taigrr/colorhash"
"github.com/taigrr/simplecolorpalettes/palettes/html"
)
palette := html.GetPalette() // or any ColorSet
c := colorhash.StringToColor(palette, "username")
// c is a deterministic color.Color
```
### ANSI terminal colors
```go
fmt.Println(colorhash.Red("error message"))
fmt.Println(colorhash.Green("success"))
fmt.Println(colorhash.BIYellow("bold high-intensity yellow"))
```
### Automatic color from a palette
```go
sp := colorhash.CreateStringerPalette(false, false, palette)
colored := sp.GetString("username") // wraps "username" in its assigned color
```
## License
0BSD