colorhash

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2026 License: 0BSD Imports: 7 Imported by: 1

README

colorhash

Map arbitrary strings and byte streams to deterministic colors from a given palette.

Features

  • Deterministic hashing — same input always produces the same color
  • FNV-64 based — fast, well-distributed hash function
  • OKLCH palette generation — perceptually uniform color palettes
  • ANSI terminal colors — built-in escape code wrappers for terminal output
  • True color support — 24-bit RGB terminal color output

Install

go get github.com/taigrr/colorhash

Usage

Map a string to a color
import (
    "github.com/taigrr/colorhash"
    "github.com/taigrr/simplecolorpalettes/palettes/html"
)

// Pick a color from a palette based on a string
// (html.GetPalette returns a simplecolor.SimplePalette,
// which WrapSimplePalette adapts to colorhash.ColorSet)
c := colorhash.StringToColor(colorhash.WrapSimplePalette(html.GetPalette()), "alice")
Hash a byte stream
func hashFile(reader io.Reader) error {
	hash, err := colorhash.HashReader(reader)
	if err != nil {
		return err
	}
	fmt.Println(hash)
	return nil
}
Generate an OKLCH palette
// 8 evenly-spaced hues at lightness 0.7, chroma 0.15
palette := colorhash.GenerateOKLCHPalette(8, 0.7, 0.15)

// Rotate the starting hue by 30 degrees
rotated := colorhash.GenerateOKLCHPaletteWithHueOffset(8, 0.7, 0.15, 30)
Assign terminal colors from a palette
sp := colorhash.CreateStringerPalette(colorhash.WrapSimplePalette(palette))
fmt.Println(sp.GetString("alice"))
Terminal color output
fmt.Println(colorhash.Red("error"))
fmt.Println(colorhash.Green("success"))
fmt.Println(colorhash.Info("info message"))

License

0BSD

Documentation

Overview

Package colorhash maps arbitrary strings and byte streams to deterministic colors from a given palette.

Index

Examples

Constants

View Source
const (
	// MaxUint is the maximum value of an unsigned integer.
	MaxUint = ^uint(0)
	// MaxInt is the maximum value of a signed integer.
	MaxInt = int(MaxUint >> 1)
)

Variables

View Source
var (
	Info  = Teal
	Warn  = Yellow
	Fatal = Red
)

Semantic color aliases for log-level styling.

View Source
var (
	Black  = ColorString("\033[0;30m%s\033[0m")
	Red    = ColorString("\033[0;31m%s\033[0m")
	Green  = ColorString("\033[0;32m%s\033[0m")
	Yellow = ColorString("\033[0;33m%s\033[0m")
	// Purple and Magenta intentionally share ANSI code 35: the base
	// 16-color palette has no distinct "purple", so Purple is an
	// alias of Magenta, matching BPurple/UPurple/OnPurple/etc. below
	// (none of which have a "BMagenta"/"UMagenta"/... counterpart).
	Purple  = ColorString("\033[0;35m%s\033[0m")
	Magenta = ColorString("\033[0;35m%s\033[0m")
	Teal    = ColorString("\033[0;36m%s\033[0m")
	White   = ColorString("\033[0;37m%s\033[0m")
	//  Bold
	BBlack  = ColorString("\033[1;30m%s\033[0m")
	BRed    = ColorString("\033[1;31m%s\033[0m")
	BGreen  = ColorString("\033[1;32m%s\033[0m")
	BYellow = ColorString("\033[1;33m%s\033[0m")
	BBlue   = ColorString("\033[1;34m%s\033[0m")
	BPurple = ColorString("\033[1;35m%s\033[0m")
	BCyan   = ColorString("\033[1;36m%s\033[0m")
	BWhite  = ColorString("\033[1;37m%s\033[0m")

	//  Underline
	UBlack  = ColorString("\033[4;30m%s\033[0m")
	URed    = ColorString("\033[4;31m%s\033[0m")
	UGreen  = ColorString("\033[4;32m%s\033[0m")
	UYellow = ColorString("\033[4;33m%s\033[0m")
	UBlue   = ColorString("\033[4;34m%s\033[0m")
	UPurple = ColorString("\033[4;35m%s\033[0m")
	UCyan   = ColorString("\033[4;36m%s\033[0m")
	UWhite  = ColorString("\033[4;37m%s\033[0m")

	//  Background
	OnBlack  = ColorString("\033[40m%s\033[0m")
	OnRed    = ColorString("\033[41m%s\033[0m")
	OnGreen  = ColorString("\033[42m%s\033[0m")
	OnYellow = ColorString("\033[43m%s\033[0m")
	OnBlue   = ColorString("\033[44m%s\033[0m")
	OnPurple = ColorString("\033[45m%s\033[0m")
	OnCyan   = ColorString("\033[46m%s\033[0m")
	OnWhite  = ColorString("\033[47m%s\033[0m")

	//  High Intensity
	IBlack  = ColorString("\033[0;90m%s\033[0m")
	IRed    = ColorString("\033[0;91m%s\033[0m")
	IGreen  = ColorString("\033[0;92m%s\033[0m")
	IYellow = ColorString("\033[0;93m%s\033[0m")
	IBlue   = ColorString("\033[0;94m%s\033[0m")
	IPurple = ColorString("\033[0;95m%s\033[0m")
	ICyan   = ColorString("\033[0;96m%s\033[0m")
	IWhite  = ColorString("\033[0;97m%s\033[0m")

	//  Bold High Intensity
	BIBlack  = ColorString("\033[1;90m%s\033[0m")
	BIRed    = ColorString("\033[1;91m%s\033[0m")
	BIGreen  = ColorString("\033[1;92m%s\033[0m")
	BIYellow = ColorString("\033[1;93m%s\033[0m")
	BIBlue   = ColorString("\033[1;94m%s\033[0m")
	BIPurple = ColorString("\033[1;95m%s\033[0m")
	BICyan   = ColorString("\033[1;96m%s\033[0m")
	BIWhite  = ColorString("\033[1;97m%s\033[0m")

	//  High Intensity backgrounds
	OnIBlack  = ColorString("\033[0;100m%s\033[0m")
	OnIRed    = ColorString("\033[0;101m%s\033[0m")
	OnIGreen  = ColorString("\033[0;102m%s\033[0m")
	OnIYellow = ColorString("\033[0;103m%s\033[0m")
	OnIBlue   = ColorString("\033[0;104m%s\033[0m")
	OnIPurple = ColorString("\033[0;105m%s\033[0m")
	OnICyan   = ColorString("\033[0;106m%s\033[0m")
	OnIWhite  = ColorString("\033[0;107m%s\033[0m")
)

Functions

func BytesToColor added in v0.6.0

func BytesToColor(p ColorSet, r io.Reader) color.Color

BytesToColor hashes the data from r and maps it to a color in p. It returns nil if p is empty.

func GenerateOKLCHPalette added in v0.6.0

func GenerateOKLCHPalette(n int, l, c float64) simplecolor.SimplePalette

GenerateOKLCHPalette generates n evenly-spaced colors in the OKLCH color space at the given lightness and chroma. This produces a perceptually uniform palette where all colors appear equally bright and saturated.

Example
package main

import (
	"fmt"

	"github.com/taigrr/colorhash"
)

func main() {
	palette := colorhash.GenerateOKLCHPalette(8, 0.7, 0.15)
	fmt.Println(palette.Len())
}
Output:
8

func GenerateOKLCHPaletteWithHueOffset added in v0.8.0

func GenerateOKLCHPaletteWithHueOffset(n int, l, c, startHue float64) simplecolor.SimplePalette

GenerateOKLCHPaletteWithHueOffset generates n evenly-spaced colors in the OKLCH color space, starting at startHue degrees.

func GetBackgroundColor added in v0.6.0

func GetBackgroundColor(c color.Color) color.Color

GetBackgroundColor returns black or white depending on the perceived luminance of c, suitable for readable text on a colored background.

func HashBytes

func HashBytes(r io.Reader) int

HashBytes returns a deterministic non-negative integer hash of the data read from r using FNV-64. Read errors from r are ignored (the hash is computed over whatever was read before the error); use HashReader to detect them.

func HashReader added in v0.8.0

func HashReader(r io.Reader) (int, error)

HashReader returns a deterministic non-negative integer hash of the data read from r using FNV-64, along with any read error encountered.

func HashString

func HashString(s string) int

HashString returns a deterministic non-negative integer hash of s using FNV-64.

Example
package main

import (
	"fmt"

	"github.com/taigrr/colorhash"
)

func main() {
	fmt.Println(colorhash.HashString("hello colorhash"))
}
Output:
893132354324239558

func StringToColor added in v0.6.0

func StringToColor(p ColorSet, s string) color.Color

StringToColor hashes s and maps it to a color in p. It returns nil if p is empty.

Example
package main

import (
	"fmt"
	"image/color"

	"github.com/taigrr/colorhash"
	"github.com/taigrr/simplecolorpalettes/simplecolor"
)

type examplePalette []color.Color

func (p examplePalette) ToPalette() color.Palette { return color.Palette(p) }
func (p examplePalette) Get(i int) color.Color    { return p[i] }
func (p examplePalette) Len() int                 { return len(p) }

func main() {
	palette := examplePalette{
		simplecolor.FromRGBA(255, 0, 0, 255),
		simplecolor.FromRGBA(0, 255, 0, 255),
		simplecolor.FromRGBA(0, 0, 255, 255),
	}

	c := colorhash.StringToColor(palette, "alice")
	fmt.Println(c == nil)
}
Output:
false

Types

type ColorSet added in v0.6.0

type ColorSet interface {
	ToPalette() color.Palette
	Get(int) color.Color
	Len() int
}

ColorSet is a palette of colors that can be indexed by position.

func WrapSimplePalette added in v0.8.0

func WrapSimplePalette(p simplecolor.SimplePalette) ColorSet

WrapSimplePalette adapts p, a github.com/taigrr/simplecolorpalettes SimplePalette such as one returned by a palettes/* package or by GenerateOKLCHPalette, to the ColorSet interface expected by StringToColor, BytesToColor, and CreateStringerPalette.

Example
package main

import (
	"fmt"

	"github.com/taigrr/colorhash"
	"github.com/taigrr/simplecolorpalettes/palettes/html"
)

func main() {
	c := colorhash.StringToColor(colorhash.WrapSimplePalette(html.GetPalette()), "alice")
	fmt.Println(c == nil)
}
Output:
false

type ColorStringer added in v0.6.0

type ColorStringer func(...interface{}) string

ColorStringer wraps a string in ANSI escape codes for terminal coloring.

func ColorString

func ColorString(colorString string) ColorStringer

ColorString returns a ColorStringer that wraps text using the given ANSI escape code format string.

type StringerPalette added in v0.6.0

type StringerPalette []ColorStringer

StringerPalette is a slice of ColorStringer functions used to map strings to colorized terminal output.

func CreateStringerPalette added in v0.8.0

func CreateStringerPalette(c ...ColorSet) StringerPalette

CreateStringerPalette returns a palette of ColorStringer functions using foreground true-color ANSI escape codes.

Example
package main

import (
	"fmt"

	"github.com/taigrr/colorhash"
)

func main() {
	palette := colorhash.GenerateOKLCHPalette(8, 0.7, 0.15)
	sp := colorhash.CreateStringerPalette(colorhash.WrapSimplePalette(palette))
	fmt.Println(sp.GetString("alice") != "")
}
Output:
true

func CreateStringerPaletteWithOptions added in v0.8.0

func CreateStringerPaletteWithOptions(opts StringerPaletteOptions, c ...ColorSet) StringerPalette

CreateStringerPaletteWithOptions returns a palette of ColorStringer functions configured with opts.

func (StringerPalette) GetString added in v0.6.0

func (sp StringerPalette) GetString(s string) string

GetString hashes s and returns it wrapped in the corresponding palette entry's ANSI escape codes.

type StringerPaletteOptions added in v0.8.0

type StringerPaletteOptions struct {
	// BackgroundFillMode selects background (rather than foreground)
	// coloring. It only takes effect when DisableSmartMode is true;
	// smart mode (the default) always colors the foreground.
	BackgroundFillMode bool
	// DisableSmartMode opts out of the default foreground-only smart
	// mode, enabling BackgroundFillMode to select background coloring.
	DisableSmartMode bool
}

StringerPaletteOptions configures how stringer palettes emit ANSI colors.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL