Reference
TypeScript types
All public types are exported from the package root:
ts
import type {
ColorType, // 'hex' | 'css-var' | 'rgb' | 'hsl' | 'named' | 'oklch' | 'color' | 'unknown'
WcagLevel, // 'AAA' | 'AA' | 'AA-large' | 'fail'
ColorBlindnessType, // 'protanopia' | 'deuteranopia' | 'tritanopia'
BackgroundSpec, // string | { type: 'semi-transparent'; ... } | { type: 'gradient'; ... }
PaletteScore, // { palette, minContrastRatio, avgContrastRatio }
} from 'color-value-tools'BackgroundSpec — discriminated union
ts
import type { BackgroundSpec } from 'color-value-tools'
const solid: BackgroundSpec = '#3498db'
const semiTransparent: BackgroundSpec = {
type: 'semi-transparent',
color: 'rgba(0,0,0,0.4)',
underlay: '#ffffff', // optional, defaults to white
}
const gradient: BackgroundSpec = {
type: 'gradient',
stops: ['#1a1a2e', '#e94560', '#f5a623'],
}PaletteScore — from bestContrastPalette
ts
import type { PaletteScore } from 'color-value-tools'
const result: PaletteScore & { paletteIndex: number } = bestContrastPalette(bg, palettes)
result.paletteIndex // number
result.palette // string[]
result.minContrastRatio // number
result.avgContrastRatio // numberInference from normalizeColor
ts
import { normalizeColor } from 'color-value-tools'
const n = normalizeColor('#3498db')
// TypeScript knows: n.hex, n.r, n.g, n.b, n.h, n.s, n.l, n.v, n.a, n.type
if (n.type !== 'unknown' && n.type !== 'css-var') {
const r: number = n.r! // available for all resolved color types
}Architecture
color-value-tools
│
├── Detection
│ getColorType, isHexColor, isRgbColor, isHslColor,
│ isOklchColor, isColorFunction, isCssVariable
│
├── Parsing
│ normalizeColor → universal entry point; handles all formats + objects
│ rgbaStringToRgba → rgb()/rgba() parser
│ hex8ToRgba → 8-digit and 4-digit hex with alpha
│ parseOklchString → oklch(L C H / alpha) with % L support
│ parseColorFn → color(display-p3 ...) / color(srgb ...)
│ parseHwbString → hwb() string
│ parseCssVar → var(--name, fallback)
│
├── Color Math — every space via RGB as the hub
│ sRGB ↔ Linear (srgbChanToLinear / linearChanToSrgb)
│ XYZ D65 intermediate for Lab/LCH
│ Oklab/Oklch — Björn Ottosson matrices
│ Display P3 — ICC sRGB→P3 and P3→sRGB matrices
│
├── Manipulation
│ lighten / darken / saturate / desaturate — via HSL
│ invertColor — RGB invert
│ grayscale — BT.709 perceptual weights
│ rotateHue — via HSL hue shift
│ adjustHexBrightness — linear channel blend toward 0/255
│ setAlpha / getAlpha — rgba string manipulation
│
├── mixColors (core of interpolation system)
│ 6 color space modes: rgb | hsl | lab | lch | oklab | oklch
│ 4 hue interpolation modes: shorter | longer | increasing | decreasing
│ 4 output formats: hex | rgb | rgba | hsl
│ Used internally by: interpolateColors, createColorScale,
│ midpointColor, tints, shades, tones,
│ generateGradientColors*, generateTints*, generateShades*
│
├── Color Harmonies
│ All implemented as hue rotations on normalized hex
│
├── Palette Generation
│ colorShades — HSL lightness sweep
│ monochromatic — HSL saturation sweep
│ tints / shades / tones — Oklab interpolation via mixColors
│
├── Accessibility
│ relativeLuminance — WCAG linearization formula
│ contrastRatio — (L1+0.05)/(L2+0.05)
│ wcagLevel — ratio thresholds 3 / 4.5 / 7
│ isReadableOnBackground — handles 3 background types;
│ semi-transparent: alpha-composites onto underlay before ratio check
│ gradient: evaluates each stop, uses minimum ratio
│ bestContrastPalette — score = avg×0.4 + min×0.6
│
├── Color Blindness
│ Vienot 1999 matrices on linear RGB
│ 3 types: protanopia / deuteranopia / tritanopia
│
├── Cache (Section 5.1)
│ Simple Map<string, NormalizeResult>
│ normalizeColorCached — Map lookup before calling normalizeColor
│ getCacheStats / clearColorCache / enableCache / disableCache
│
├── Generator functions (Section 5.2)
│ ES2015 generator syntax (*) — yield one color at a time
│ generateGradientColors* → mixColors inside the generator
│ generateTints* / generateShades* → Oklab mix toward white/black
│
└── CLI (dist/cli/bin/cli.js → cvt)
Commands: info | convert | contrast | shades | harmonies | nearest
Accepts any color format normalizeColor understandsBundle size & dependencies
| Runtime dependencies | None |
| Peer dependencies | None |
| Dev dependencies | TypeScript, Vitest |
| ESM entry | dist/esm/index.js (tree-shakeable) |
| CJS entry | dist/cjs/index.js |
| CLI entry | dist/cli/bin/cli.js → cvt |
The package ships both ESM (type: module) and CommonJS modules. Every function is a named export — unused functions are eliminated by bundlers that support tree-shaking.
License
MIT