JSPM

@zakkster/lite-gradient

1.0.4
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 14
  • Score
    100M100P100Q73275F
  • License MIT

Zero-GC OKLCH gradient generator. N-stop perceptually uniform gradients for canvas and CSS.

Package Exports

  • @zakkster/lite-gradient

Readme

@zakkster/lite-gradient

npm version npm bundle size npm downloads npm total downloads TypeScript Dependencies License: MIT

๐ŸŽฌ Live Demo (Gradient)

https://cdpn.io/pen/debug/QwKqXKP

๐ŸŒˆ What is lite-gradient?

@zakkster/lite-gradient creates smooth, perceptually uniform gradients using OKLCH color space โ€” the same space used by your browser's native CSS oklch().

It gives you:

  • ๐ŸŒˆ N-stop OKLCH gradients (no gray dead zones unlike HSL/RGB)
  • ๐ŸŽฏ at(t, out) โ€” caller-owned output (zero-GC in render loops)
  • ๐Ÿ“Š sampleArray(out, count) โ€” fill a Float32Array LUT
  • ๐Ÿ–ผ๏ธ toLinear() / toRadial() โ€” Canvas2D gradient objects
  • ๐ŸŽจ toCssLinear() / toCssRadial() โ€” CSS gradient strings
  • ๐ŸŽจ palette(count) โ€” extract N CSS color strings
  • ๐Ÿ”ฅ 5 presets: Sunset, Ocean, Fire, Neon, Grey
  • ๐Ÿชถ < 2 KB minified

Part of the @zakkster/lite-* ecosystem โ€” micro-libraries built for deterministic, cache-friendly game development.

๐Ÿš€ Install

npm i @zakkster/lite-gradient

๐Ÿ•น๏ธ Quick Start

import { Gradient, gradientFire } from '@zakkster/lite-gradient';

// Custom gradient
const g = new Gradient([
    { l: 0.3, c: 0.15, h: 270 },  // deep purple
    { l: 0.6, c: 0.25, h: 330 },  // magenta
    { l: 0.9, c: 0.12, h: 60 },   // warm gold
]);

// Zero-GC sampling (render loop safe)
const color = { l: 0, c: 0, h: 0 };
g.at(particle.life, color);
ctx.fillStyle = toCssOklch(color);

// Canvas gradient (setup)
ctx.fillStyle = g.toLinear(ctx, 0, 0, canvas.width, 0);
ctx.fillRect(0, 0, canvas.width, canvas.height);

// CSS background
element.style.background = gradientFire.toCssLinear(135);

// LUT for particle color ramp
const lut = new Float32Array(256 * 3);
g.sampleArray(lut, 256);

๐Ÿ“Š Comparison

Library Size Color Space Zero-GC Canvas CSS Install
chroma.js ~14 KB LAB/LCH No No No npm i chroma-js
culori ~10 KB OKLCH No No No npm i culori
lite-gradient < 2 KB OKLCH Yes Yes Yes npm i @zakkster/lite-gradient

โš™๏ธ API

new Gradient(stops)

  • stops: [{ l, c, h, stop? }] โ€” OKLCH colors with optional position (0โ€“1)

.at(t, out) โ€” Sample into caller-owned { l, c, h }. Zero-GC.

.css(t) โ€” Sample as CSS oklch() string (allocates โ€” setup only)

.palette(count) โ€” Array of CSS color strings

.sampleArray(out, count) โ€” Fill Float32Array [l,c,h,l,c,h,...]

.toLinear(ctx, x0,y0,x1,y1, resolution?) โ€” Canvas2D linear gradient

.toRadial(ctx, cx,cy,r, resolution?) โ€” Canvas2D radial gradient

.toCssLinear(angle?, resolution?) โ€” CSS linear-gradient() string

.toCssRadial(resolution?) โ€” CSS radial-gradient() string

๐Ÿงช Benchmark

10,000 particle color lookups per frame:
  chroma.js:     Allocates Color object per sample
  lite-gradient:  at(t, out) mutates caller-owned object, zero allocation

๐Ÿ“ฆ TypeScript

Full declarations included in lite-gradient.d.ts.

๐Ÿ“š LLM-Friendly Documentation

See llms.txt for AI-optimized metadata and usage examples.

License

MIT