Files
colorhash/oklch_test.go
Tai Groot bf06224d7a 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)
2026-02-22 22:02:46 -05:00

69 lines
1.6 KiB
Go

package colorhash
import (
"math"
"testing"
)
func TestGenerateOKLCHPalette(t *testing.T) {
palette := GenerateOKLCHPalette(8, 0.7, 0.15)
if len(palette) != 8 {
t.Fatalf("expected 8 colors, got %d", len(palette))
}
// All colors should have approximately the same lightness and chroma
for i, c := range palette {
oklch := c.ToOKLCH()
if math.Abs(oklch.L-0.7) > 0.05 {
t.Errorf("color %d: L = %f, expected ~0.7", i, oklch.L)
}
if math.Abs(oklch.C-0.15) > 0.03 {
t.Errorf("color %d: C = %f, expected ~0.15", i, oklch.C)
}
}
// Hues should be evenly spaced (45° apart for 8 colors)
for i, c := range palette {
oklch := c.ToOKLCH()
expectedH := float64(i) * 45.0
diff := math.Abs(oklch.H - expectedH)
if diff > 180 {
diff = 360 - diff
}
if diff > 10.0 {
t.Errorf("color %d: H = %f, expected ~%f", i, oklch.H, expectedH)
}
}
}
func TestGenerateOKLCHPaletteEmpty(t *testing.T) {
palette := GenerateOKLCHPalette(0, 0.7, 0.15)
if len(palette) != 0 {
t.Errorf("expected empty palette, got %d colors", len(palette))
}
}
func TestGenerateOKLCHPaletteSingle(t *testing.T) {
palette := GenerateOKLCHPalette(1, 0.6, 0.1)
if len(palette) != 1 {
t.Fatalf("expected 1 color, got %d", len(palette))
}
oklch := palette[0].ToOKLCH()
if oklch.H > 5.0 && oklch.H < 355.0 {
t.Errorf("single color H = %f, expected ~0", oklch.H)
}
}
func TestGenerateOKLCHPaletteDistinct(t *testing.T) {
palette := GenerateOKLCHPalette(6, 0.7, 0.15)
// All colors should be distinct
seen := make(map[int]bool)
for _, c := range palette {
if seen[int(c)] {
t.Errorf("duplicate color: %s", c.ToHex())
}
seen[int(c)] = true
}
}