A pragmatic set of modern colour space transforms for the Odin language

Lobsters Hottest Tools

Summary

A library for the Odin language providing correct, minimal colour space transforms for UI frameworks and creative tooling, covering OKLCh, OKLab, sRGB, Display P3, and Rec. 2020.

<p><a href="https://lobste.rs/s/flliys/pragmatic_set_modern_colour_space">Comments</a></p>
Original Article
View Cached Full Text

Cached at: 05/31/26, 12:19 PM

heavyrain266/colourspace

Source: https://github.com/heavyrain266/colourspace

colourspace

A pragmatic set of modern colour space transforms

Overview

colourspace provides correct, minimal colour space transforms for UI frameworks and creative tooling, covering OKLCh, OKLab, sRGB, Display P3, and Rec. 2020. Transforms between linear RGB spaces are direct matrix multiplications with pre-computed matrices. Perceptual manipulation and colour appearance work uses OKLab as the working space.

Gamut mapping (in progress) follows Björn Ottosson’s cusp-projection approach rather than the chroma-reduction method in CSS Color Module 4, which produces hue drift on sRGB displays, most visibly P3 blues rendering as purple.

Quick Start

Construct colours in any covered space, convert between them freely, and encode for display. All conversions are hue-preserving and operate in linear light unless explicitly encoded.

package main

import "core:fmt"
import cs "colourspace"


main :: proc() {
	// Construct colours
	lch:  cs.OKLCh       = cs.oklch(0.6456, 0.1003, 71.27)
	lab:  cs.OKLab       = cs.oklab(0.6456, 0.0337, 0.0942)
	srgb: cs.Linear_sRGB = cs.linear_srgb(0.5, 0.3, 0.1)
	p3:   cs.Linear_P3   = cs.linear_p3(0.5, 0.3, 0.1)

	// OKLab roundtrip
	linear_from_lch: cs.Linear_sRGB = cs.oklch_to_srgb(lch)
	lch_from_linear: cs.OKLCh       = cs.srgb_to_oklch(linear_from_lch)
	linear_from_lab: cs.Linear_sRGB = cs.oklab_to_srgb(lab)

	// Linear roundtrip
	p3_to_srgb: cs.Linear_sRGB = cs.p3_to_srgb(p3)
	srgb_to_p3: cs.Linear_P3   = cs.srgb_to_p3(srgb)

	// Gamma (de)compression
	decoded: cs.Linear_sRGB = cs.srgb_decode(cs.sRGB{0.5, 0.3, 0.1})
	encoded: cs.sRGB        = cs.linear_srgb_encode(decoded)

	fmt.println(linear_from_lch, lch_from_linear, linear_from_lab, p3_to_srgb, srgb_to_p3, decoded, encoded)
}

References

Björn Ottosson

ITU-R (Rec. 2020)

W3C / CSS Working Group

International Color Consortium

Similar Articles

Color picking Oklch for mortals

Lobsters Hottest

A guide explaining the Oklch color space in simple terms, emphasizing its perceptual uniformity for consistent color picking in design.

Generative colors with CSS

Lobsters Hottest

A tutorial on using the CSS oklch() function and relative colors to dynamically generate full color palettes from a single hex code, simplifying color management on websites.

Understanding the Odin Programming Language

Hacker News Top

A book teaching the Odin programming language, covering manual memory management, parametric polymorphism, and data-oriented design. Version 1.10 has been released with updates.

Odin dev-2026-06 Released

Lobsters Hottest

Odin dev-2026-06 has been released. Odin is a data-oriented programming language designed for high-performance systems development.