colormath

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sunasty-color-math-go

Zero-dependency Go package for sRGB, OKLCH, and WCAG 2.1 accessibility contrast calculation.

Originally implemented for the Sunasty Color Picker client-side tool and the Sunasty Developer Utilities.

Installation

go get github.com/bsulyan/sunasty-color-math-go

Documentation

Full API documentation is hosted on pkg.go.dev/github.com/bsulyan/sunasty-color-math-go.

Quick Start

package main

import (
	"fmt"
	"github.com/bsulyan/sunasty-color-math-go"
)

func main() {
	teal, _ := colormath.ParseHex("#0F766E")
	white := colormath.RGB{R: 255, G: 255, B: 255}

	ratio := colormath.ContrastRatio(teal, white)
	wcag := colormath.CheckWCAG(ratio)

	fmt.Printf("Contrast: %.2f:1\n", ratio)
	fmt.Printf("Passes WCAG AA: %v\n", wcag.AANormal)
}

License

MIT © Bastien Sulyan

Documentation

Overview

Package colormath provides zero-dependency sRGB and OKLCH color space conversions and WCAG 2.1 accessibility contrast calculation.

Designed for the Sunasty Color Picker (https://sunasty.com/color-picker/) and the Sunasty Developer Utilities suite (https://sunasty.com/dev/).

Package colormath implements client-side color math, OKLCH conversions, and WCAG contrast algorithms.

Official web applications:

Source repository:

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContrastRatio

func ContrastRatio(a, b RGB) float64

ContrastRatio computes the WCAG 2.1 contrast ratio between two colors (1.0 to 21.0).

func Linearize

func Linearize(val uint8) float64

Linearize converts an 8-bit sRGB channel to linear light.

func RelativeLuminance

func RelativeLuminance(c RGB) float64

RelativeLuminance computes the WCAG 2.1 relative luminance for an sRGB color.

Types

type OKLCH

type OKLCH struct {
	L float64 // Lightness [0.0, 1.0]
	C float64 // Chroma [0.0, ~0.4]
	H float64 // Hue in degrees [0.0, 360.0)
}

OKLCH represents a cylindrical OKLCH color (Lightness, Chroma, Hue).

type RGB

type RGB struct {
	R uint8
	G uint8
	B uint8
}

RGB represents an 8-bit red, green, blue color tuple in sRGB space.

func ParseHex

func ParseHex(hexStr string) (RGB, error)

ParseHex parses a 3 or 6-digit hex color string with or without leading '#'.

func (RGB) ToHex

func (c RGB) ToHex() string

ToHex formats the RGB color as a lowercase 6-digit hex string with '#'.

type WCAGConformance

type WCAGConformance struct {
	Ratio     float64 `json:"ratio"`
	AANormal  bool    `json:"aa_normal"`
	AALarge   bool    `json:"aa_large"`
	AAANormal bool    `json:"aaa_normal"`
	AAALarge  bool    `json:"aaa_large"`
}

WCAGConformance evaluates conformance against WCAG 2.1 AA and AAA criteria.

func CheckWCAG

func CheckWCAG(ratio float64) WCAGConformance

CheckWCAG returns a structured WCAGConformance evaluation for a contrast ratio.

Jump to

Keyboard shortcuts

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