niji logo

niji

Package Version Hex Docs

A practical OKLCH color toolkit for Gleam.

It includes:

What Is OKLCH?

OKLCH is a perceptual color space based on OKLab, expressed as:

Compared to RGB/HSL, OKLCH is usually easier to reason about when designing palettes:

In this library, to_rgb/1 applies CSS-inspired gamut mapping so colors that cannot be displayed in sRGB are adjusted more gracefully than simple channel clipping.

Installation

gleam add niji@1

Quick Start

import niji

pub fn main() {
  let brand = niji.oklch(0.62, 0.19, 250.0, 1.0)

  // Convert to CSS and hex
  let css = niji.to_css(brand)
  let hex = niji.to_hex(brand)

  // Generate a complementary color
  let complement = niji.complementary(brand)

  // Mix 20% toward complement
  let mixed = niji.mix(brand, complement, 0.2)

  // Guarantee sRGB-safe output for UI
  let safe = niji.gamut_map(mixed)

  // Print values in terminal color
  let preview = niji.ansi_bg(safe, "  OKLCH  ")
  preview
}

Core API

Constructors

All constructors normalize/clamp inputs to safe ranges.

Conversion

Manipulation

Palette helpers

Gamut + distance

Accessibility

ANSI output

gleam_community/colour interop

Examples

Parse hex and rotate hue

import niji
import gleam_community/colour
import gleam/result

pub fn main() {
  let assert Ok(rotated) =
    colour.from_rgb_hex_string("#3366FF")
    |> result.map(niji.oklch_from_colour)
    |> result.map(niji.rotate_hue(_, 45.0))

  niji.to_css(rotated)
}

Check contrast

import niji

pub fn main() {
  let text = niji.oklch(0.15, 0.02, 250.0, 1.0)
  let bg = niji.oklch(0.96, 0.01, 250.0, 1.0)

  let ratio = niji.contrast_ratio(text, bg)
  let passes = niji.wcag_aa(ratio)

  #(ratio, passes)
}

Development

gleam test
gleam run

Notes

Documentation

Search Document