{"name":"@colordx/core","version":"1.2.1","keywords":["color","colour","oklch","oklab","display-p3","wide-gamut","hsl","rgb","hex","hsv","hwb","lab","lch","xyz","cmyk","color-space","color-manipulation","color-conversion","performance"],"author":"","license":"MIT","_id":"@colordx/core@1.2.1","maintainers":[{"name":"dmkryak","email":"dmkryak@gmail.com"}],"homepage":"https://colordx.dev","bugs":{"url":"https://github.com/dkryaklin/colordx/issues"},"dist":{"shasum":"fabf53bbb7c6d9ed8a05428150192dfee4de9951","tarball":"http://registry-npm.ywwl.com/@colordx/core/download/@colordx/core-1.2.1.tgz","fileCount":65,"integrity":"sha512-AALYrTajEgGUzdV4FbsXVbAMklZ433aAc09KgPtcUSneunzByJkmvhgKctPDjoiJvtPH56/dUw9zw89fMuINJw==","signatures":[{"sig":"MEQCIAcyWQrLnn0xj5dHBsxYxiWsXbOL4vS5d7ePd8WDFnw0AiARSToJOGVT+jRB7bqZtr202yUPabclvhk5dc8Z2YDKMg==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@colordx%2fcore@1.2.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":493648,"size":104369,"noattachment":false,"key":"/@colordx/core/-/@colordx/core-1.2.1.tgz"},"main":"./dist/index.cjs","type":"module","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"import":{"types":"./dist/index.d.ts","default":"./dist/index.mjs"},"require":{"types":"./dist/index.d.cts","default":"./dist/index.cjs"}},"./plugins/*":{"import":{"types":"./dist/plugins/*.d.ts","default":"./dist/plugins/*.mjs"},"require":{"types":"./dist/plugins/*.d.cts","default":"./dist/plugins/*.cjs"}}},"gitHead":"8eee6a5258530bee399df00c544dd232f3f0dc7b","scripts":{"dev":"tsup --watch","lint":"eslint src","test":"vitest run","build":"tsup","format":"prettier --write \"src/**/*.ts\"","prepare":"node --eval \"if (process.env.CI) process.exit(0)\" && husky || true","release":"yarn build && publint && changeset publish","version":"changeset version","changeset":"changeset","typecheck":"tsc --noEmit","playground":"npx serve . -p 3000 --config serve.json","test:watch":"vitest","changeset:ai":"tsx scripts/changeset.ts","format:check":"prettier --check \"src/**/*.ts\"","test:coverage":"vitest run --coverage","deploy:playground":"yarn build && tsx scripts/deploy-playground.ts"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:f554ef5f-d63e-43e2-864c-f33dd3fe073d"}},"repository":{"url":"git+https://github.com/dkryaklin/colordx.git","type":"git"},"_npmVersion":"11.12.0","description":"A high-performance color library with extended support for modern color spaces including OKLCH, OKLAB, Display-P3, and more","directories":{},"lint-staged":{"src/**/*.ts":["prettier --write","eslint --fix"]},"sideEffects":false,"_nodeVersion":"24.14.0","_hasShrinkwrap":false,"packageManager":"yarn@4.13.0","devDependencies":{"tsx":"^4.21.0","tsup":"^8.5.1","husky":"^9.1.7","colord":"^2.9.3","eslint":"^9.39.0","vitest":"^4.1.0","publint":"^0.3.18","prettier":"^3.8.1","typescript":"^5.9.3","lint-staged":"^16.4.0","@changesets/cli":"^2.30.0","@anthropic-ai/sdk":"^0.80.0","typescript-eslint":"^8.57.1","@vitest/coverage-v8":"^4.1.0","eslint-config-prettier":"^10.1.8","@typescript-eslint/parser":"^8.57.1","@changesets/changelog-github":"^0.6.0","@typescript-eslint/eslint-plugin":"^8.57.1","@trivago/prettier-plugin-sort-imports":"^6.0.2"},"_npmOperationalInternal":{"tmp":"tmp/core_1.2.1_1774291742900_0.11665537449893759","host":"s3://npm-registry-packages-npm-production"},"readme":"<p align=\"center\">\n  <img src=\"assets/logo.svg\" width=\"128\" height=\"128\" alt=\"colordx\" />\n</p>\n\n# @colordx/core\n\n[![npm version](https://img.shields.io/npm/v/@colordx/core?labelColor=764be5&color=ffc200)](https://www.npmjs.com/package/@colordx/core)\n[![used by cssnano](https://img.shields.io/badge/used_by-cssnano-ffc200?labelColor=764be5)](https://github.com/cssnano/cssnano)\n[![used by oklch-picker](https://img.shields.io/badge/used_by-oklch--picker-ffc200?labelColor=764be5)](https://github.com/evilmartians/oklch-picker)\n[![bundle size](https://img.shields.io/bundlejs/size/@colordx/core?labelColor=764be5&color=ffc200)](https://bundlejs.com/?q=@colordx/core)\n[![npm downloads](https://img.shields.io/npm/dw/@colordx/core?labelColor=764be5&color=ffc200)](https://www.npmjs.com/package/@colordx/core)\n[![zero dependencies](https://img.shields.io/badge/dependencies-0-ffc200?labelColor=764be5)](https://github.com/dkryaklin/colordx/blob/main/package.json)\n[![CI](https://img.shields.io/github/actions/workflow/status/dkryaklin/colordx/ci.yml?branch=main&label=ci&labelColor=764be5&color=ffc200)](https://github.com/dkryaklin/colordx/actions/workflows/ci.yml)\n\n**[Try it on colordx.dev](https://colordx.dev)**\n\nA modern color manipulation library built for the CSS Color 4 era, with first-class support for **OKLCH** and **OKLab**. **8.4 KB gzipped. 0 Dependencies.**\n\n## Performance\n\nApple M5 Pro, Node 24, using [mitata](https://github.com/evanwashere/mitata). Operations per\nsecond — higher is better. Versions: colord 2.10.0, culori 4.0.2, chroma-js 3.2.0, color 5.0.3,\ntinycolor2 1.6.0, @texel/color 1.1.11.\n\n| Benchmark | **colordx** | @texel/color | colord | culori | chroma-js | color | tinycolor2 |\n|---|---|---|---|---|---|---|---|\n| Parse HEX → toHex | **39M** | 8.2M | 7.8M | 7.2M | 3.7M | 1.6M | 2.9M |\n| Parse HEX → toHsl | **37M** | — | 29M | 7.9M | 3.8M | 3.4M | 2.9M |\n| Parse RGB object → toHex | **51M** | — | 35M | 49M | 5.2M | 1.6M | 5.9M |\n| Parse rgb() string → toHex | **11M** | — | 7.3M | 3.1M | 230K | 1.5M | 3.0M |\n| Parse hsl() string → toHex | **6.7M** | — | 4.9M | 3.1M | 222K | 1.4M | 2.0M |\n| Parse named color → toHex | **11M** | — | 4.6M | 3.9M | 4.5M | 1.3M | 2.6M |\n| Parse HEX → lighten → toHex | **19M** | — | 13M | 5.3M | 1.9M | 1.3M | 1.2M |\n| Mix two colors | **16M** | 6.3M | 2.6M | 1.3M | 1.3M | 694K | 1.3M |\n| WCAG contrast ratio | **12M** | — | 3.7M | 3.3M | 2.0M | — | 1.5M |\n| Parse HEX → toOklch | **11M** | 6.7M | — | 4.9M | 1.4M | 2.6M | — |\n| inGamutP3 | **6.6M** | 3.9M | — | 1.5M | — | — | — |\n| inGamutRec2020 | **6.5M** | 3.8M | — | 1.5M | — | — | — |\n| CIEDE2000 delta | **4.2M** | — | — | 2.0M | 1.3M | — | — |\n| OKLCH string → HEX | **3.6M** | 2.8M | — | 1.7M | 192K | — | — |\n| Gamut map → sRGB | **1.0M** | — | — | 522K | — | — | — |\n\nIn the RGB object row culori gets its own `{ mode, r, g, b }` format, so it skips parsing.\n\nMean of two runs. Run `yarn bench` to check.\n\n## Install\n\n```bash\nnpm install @colordx/core\n```\n\n## Quick start\n\n```ts\nimport { colordx } from '@colordx/core';\n\n// Parse any CSS color string or color object, then chain conversions:\ncolordx('#ff0000').toRgbString();     // 'rgb(255 0 0)'\ncolordx('#ff0000').toHex();           // '#ff0000'\ncolordx('#ff0000').toOklch();         // { l: 0.62796, c: 0.25768, h: 29.23389, alpha: 1 }\ncolordx('#ff0000').toOklchString();   // 'oklch(0.62796 0.25768 29.23389)'\n\n// Works from any input format — hex, rgb(), hsl(), oklch(), oklab(), plain objects:\ncolordx('oklch(0.5 0.2 240)').toHex();                     // '#0069c7'\ncolordx({ r: 255, g: 0, b: 0 }).toHslString();             // 'hsl(0 100% 50%)'\n\n// Chain manipulations — each call returns a new immutable Colordx:\ncolordx('#ff0000').lighten(0.1).saturate(0.2).toHex();\ncolordx('#3d7a9f').rotate(30).darken(0.1).toRgbString();\n```\n\nThe `colordx()` factory is all you need for day-to-day work. For out-of-gamut `oklch()` / `oklab()` inputs, `.toHex()` / `.toRgbString()` clip in linear sRGB — the same strategy browsers use when rendering `background: oklch(...)` — so your output matches what users see on screen. If you need stricter hue/lightness preservation for authoring workflows, see [Gamut](#gamut).\n\n## API\n\nAll methods are immutable — they return a new `Colordx` instance.\n\n### Parsing\n\nAccepts any CSS color string or color object:\n\n```ts\ncolordx('#ff0000');\ncolordx('#f00');\ncolordx('rgb(255 0 0)');\ncolordx('color(srgb 1 0 0)'); // CSS Color 4 color() form of sRGB\ncolordx('rgba(255, 0, 0, 0.5)');\ncolordx('hsl(0 100% 50%)');\ncolordx('oklab(0.6279 0.2249 0.1257)');\ncolordx('oklch(0.6279 0.2577 29.23)');\ncolordx({ r: 255, g: 0, b: 0 });           // alpha defaults to 1\ncolordx({ r: 255, g: 0, b: 0, alpha: 0.5 });\ncolordx({ h: 0, s: 100, l: 50 });\ncolordx({ l: 0.6279, a: 0.2249, b: 0.1257 }); // OKLab\ncolordx({ l: 0.6279, c: 0.2577, h: 29.23 }); // OKLch\n// With p3 plugin loaded:\ncolordx('color(display-p3 0.9176 0.2003 0.1386)'); // Display-P3 string\n// With rec2020 plugin loaded:\ncolordx('color(rec2020 0.7919 0.2307 0.0739)'); // Rec.2020 string\n// With a98rgb plugin loaded:\ncolordx('color(a98-rgb 0.8586 0 0)'); // A98 (Adobe RGB 1998) string\n// With prophoto plugin loaded:\ncolordx('color(prophoto-rgb 0.7022 0.2757 0.1035)'); // ProPhoto string\n// With srgb-linear plugin loaded:\ncolordx('color(srgb-linear 1 0 0)'); // linear-light sRGB string\n// With hwb plugin loaded:\ncolordx('hwb(0 0% 0%)');\ncolordx({ h: 0, w: 0, b: 0 });\n// With hsv plugin loaded:\ncolordx({ h: 0, s: 100, v: 100 }); // HSV\n```\n\nChannels are clamped the way CSS Color 4 clamps them at parsed-value time: `rgb()` / `hsl()` channels to their ranges, `lab()` / `lch()` / `oklab()` / `oklch()` lightness to `[0, 100]` / `[0, 1]`, chroma to `≥ 0`. Lab/LCH `a`, `b`, `c` and OKLab/OKLCh `a`, `b`, `c` are unbounded, which is what makes out-of-gamut colors representable (see [Gamut](#gamut)). Alpha is clamped to `[0, 1]`. One consequence: an imaginary `lab()` / `lch()` / `color(xyz …)` input can have an OKLab lightness outside `[0, 1]`; `.toOklchString()` reports it faithfully, but feeding that string back clamps L, so only colors with L in range round-trip through OKLCh. Two object-only rules: an OKLab/OKLCh object with `l > 1` is rejected as invalid — it is almost certainly a CIE Lab/LCH value missing its `colorSpace: 'lab' | 'lch'` brand, and clamping it to white would hide the mistake — and `NaN` in any channel reads as `0` (an infinite hue reads as `0°`).\n\nTypeScript: input color objects use `*ColorInput` types (`alpha` optional, defaults to 1).\nOutput methods like `.toRgb()` / `.toOklch()` return `*Color` types (`alpha` always present).\n\n```ts\nimport type { RgbColor, RgbColorInput } from '@colordx/core';\n\nconst input: RgbColorInput = { r: 255, g: 0, b: 0 };       // alpha optional\nconst output: RgbColor = colordx(input).toRgb();           // alpha guaranteed\n```\n\n### Conversion\n\n```ts\n.toRgb()           // { r: 255, g: 0, b: 0, alpha: 1 }\n.toRgbString()                    // 'rgb(255 0 0)'                — CSS Color 4 (default)\n.toRgbString({ legacy: true })    // 'rgb(255, 0, 0)'              — CSS Color 3 comma syntax\n// Legacy form also switches to `rgba()` when alpha < 1:\ncolordx({ r: 255, g: 0, b: 0, alpha: 0.5 }).toRgbString({ legacy: true }); // 'rgba(255, 0, 0, 0.5)'\n.toHex()           // '#ff0000'\n.toNumber()        // 16711680  (0xff0000 — PixiJS / Discord integer format)\n.toNumber32()      // 4278190335 (0xff0000ff — 32-bit RGBA, alpha as the low byte)\n.toHsl()           // { h: 0, s: 100, l: 50, alpha: 1 }\n.toHslString()     // 'hsl(0 100% 50%)'\n// toHsl accepts an optional precision argument (decimal places):\ncolordx('#3d7a9f').toHsl()         // { h: 202.65, s: 44.55, l: 43.14, alpha: 1 }      — default (2)\ncolordx('#3d7a9f').toHsl(4)        // { h: 202.6531, s: 44.5455, l: 43.1373, alpha: 1 }\ncolordx('#3d7a9f').toHsl(0)        // { h: 203, s: 45, l: 43, alpha: 1 }               — integers\ncolordx('#3d7a9f').toHslString()   // 'hsl(202.65 44.55% 43.14%)'\ncolordx('#3d7a9f').toHslString(4)  // 'hsl(202.6531 44.5455% 43.1373%)'\n// With hwb plugin loaded:\n.toHwb()           // { h: 0, w: 0, b: 0, alpha: 1 }\n.toHwbString()     // 'hwb(0 0% 0%)'\n.toOklab()         // { l: 0.62796, a: 0.22486, b: 0.12585, alpha: 1 }\n.toOklabString()   // 'oklab(0.62796 0.22486 0.12585)'\n.toOklch()         // { l: 0.62796, c: 0.25768, h: 29.23389, alpha: 1 }\n.toOklchString()   // 'oklch(0.62796 0.25768 29.23389)'\n// With p3 plugin loaded:\n.toP3()            // { r: 0.9175, g: 0.2003, b: 0.1386, alpha: 1, colorSpace: 'display-p3' }\n.toP3String()      // 'color(display-p3 0.9175 0.2003 0.1386)'\n// With srgb-linear plugin loaded:\n.toSrgbLinear()    // { r: 1, g: 0, b: 0, alpha: 1, colorSpace: 'srgb-linear' }\n.toSrgbLinearString() // 'color(srgb-linear 1 0 0)'\n```\n\n### Manipulation\n\n```ts\n.lighten(0.1)                        // increase lightness by 10 percentage points\n.lighten(0.1, { relative: true })    // increase lightness by 10% of current value\n.darken(0.1)                         // decrease lightness by 10 percentage points\n.darken(0.1, { relative: true })     // decrease lightness by 10% of current value\n.saturate(0.1)                       // increase saturation by 10 percentage points\n.saturate(0.1, { relative: true })   // increase saturation by 10% of current value\n.desaturate(0.1)                     // decrease saturation by 10 percentage points\n.desaturate(0.1, { relative: true }) // decrease saturation by 10% of current value\n.grayscale()       // fully desaturate\n.invert()          // invert RGB channels\n.rotate(30)        // rotate hue by 30°\n.alpha(0.5)        // set alpha\n.hue(120)          // set hue (HSL)\n.lightness(0.5)    // set lightness (OKLCH, 0–1)\n.chroma(0.1)       // set chroma (OKLCH, 0–0.4)\n```\n\nThe HSL-based methods (`lighten`, `darken`, `saturate`, `desaturate`, `grayscale`, `rotate`, `hue`) and `invert` work on the sRGB-clipped color — the same color `.toHex()` prints — so on a wide-gamut input they start from what is displayed. `lightness()` and `chroma()` work in OKLCH and clip the result into sRGB.\n\n### Getters\n\n```ts\n.isValid()         // true if input was parseable\n.alpha()           // get alpha (0–1)\n.hue()             // get HSL hue, [0, 360)\n.lightness()       // get OKLCH lightness (0–1)\n.chroma()          // get OKLCH chroma (0–0.4)\n.brightness()      // perceived brightness (0–1), of the sRGB-clipped color\n.isDark()          // brightness < 0.5\n.isLight()         // brightness >= 0.5\n.isEqual('#f00')   // same RGBA bytes after rounding\n.over('#fff')      // source-over composite of a translucent color onto a bg\n// With a11y plugin loaded:\n.luminance()       // relative luminance (0–1, WCAG), optional precision\n.contrast('#fff')  // WCAG 2.x contrast ratio (1–21), optional precision\n// With mix plugin loaded:\n.mix('#0000ff', 0.5)       // mix in sRGB space (CSS spec)\n.mixOklab('#0000ff', 0.5)  // mix in Oklab space (perceptually uniform)\n```\n\n### Utilities\n\n```ts\nimport { getFormat, nearest, oklchToLinear, oklchToRgbChannels, random } from '@colordx/core';\n\ngetFormat('#ff0000'); // 'hex'\ngetFormat('rgb(255 0 0)'); // 'rgb'\ngetFormat('hsl(0 100% 50%)'); // 'hsl'\ngetFormat('oklch(0.5 0.2 240)'); // 'oklch'\ngetFormat('oklab(0.6279 0.2249 0.1257)'); // 'oklab'\ngetFormat({ r: 255, g: 0, b: 0 }); // 'rgb'\ngetFormat({ h: 0, s: 100, l: 50 }); // 'hsl'\ngetFormat('notacolor'); // undefined\n// Plugin-added parsers register their own format:\n// p3 → 'p3', hsv → 'hsv', hwb → 'hwb', cmyk → 'cmyk', lch → 'lch', lab → 'lab' | 'xyz' | 'xyz-d65', names → 'name', rec2020 → 'rec2020', a98rgb → 'a98-rgb', prophoto → 'prophoto-rgb', srgb-linear → 'srgb-linear'\n\nnearest('#800', ['#f00', '#ff0', '#00f']); // '#f00' — perceptual distance via OKLab\nnearest('#ffe', ['#f00', '#ff0', '#00f']); // '#ff0'\n\nrandom(); // random Colordx instance\n\n// Low-level functional converters — no object allocation, for hot paths (canvas gradients, etc.)\noklchToRgbChannels(0.5, 0.2, 240); // [r, g, b] gamma-encoded sRGB in [0, 1]\n// Out-of-gamut channels may exceed [0, 1] — callers clamp before byte encoding\n\nconst linear = oklchToLinear(0.5, 0.2, 240); // unclamped linear sRGB — also a free sRGB gamut check\n\n// Non-OKLCH inputs → linear sRGB (same output scale and gamut-check behavior as oklchToLinear).\n// Use these when you already have RGB/Lab/LCH values and want linear pixels without round-tripping through OKLCH.\nimport {\n  labToLinearAndSrgb,\n  labToLinearSrgb,\n  labToRgbChannels,\n  lchToLinearAndSrgb,\n  lchToLinearSrgb,\n  lchToRgbChannels,\n  rgbToLinear,\n} from '@colordx/core';\n\nrgbToLinear(1, 0, 0);          // [1, 0, 0]           — vector sibling of srgbToLinear (0–1 input)\nlabToLinearSrgb(54.29, 80.8, 69.89); // Lab D50 → linear sRGB (via XYZ D50)\nlchToLinearSrgb(54.29, 106.84, 40.86); // LCH D50 → Lab → linear sRGB\n\n// Gamma-encoded sRGB in one call (skips the manual srgbFromLinear step):\nlabToRgbChannels(54.29, 80.8, 69.89); // → [r, g, b] gamma sRGB in [0, 1]\nlchToRgbChannels(54.29, 106.84, 40.86);\n\n// Both linear (for gamut check) and gamma (for display) in a single pass:\nconst [lin, srgb] = labToLinearAndSrgb(54.29, 80.8, 69.89); // or lchToLinearAndSrgb\n\n// Hex/RGB input? Parse once, then divide by 255:\nconst { r, g, b } = colordx('#ff0000').toRgb();\nrgbToLinear(r / 255, g / 255, b / 255); // [1, 0, 0]\n\n// P3/Rec.2020 channel functions live in their plugins:\nimport { labToP3Channels, lchToP3Channels, linearToP3Channels, oklchToP3Channels } from '@colordx/core/plugins/p3';\nimport {\n  labToRec2020Channels,\n  lchToRec2020Channels,\n  linearToRec2020Channels,\n  oklchToRec2020Channels,\n} from '@colordx/core/plugins/rec2020';\n\noklchToP3Channels(0.5, 0.2, 240);      // [r, g, b] gamma-encoded Display-P3 in [0, 1]\noklchToRec2020Channels(0.5, 0.2, 240); // [r, g, b] gamma-encoded Rec.2020 in [0, 1] (BT.2020 gamma)\n\n// CIE Lab/LCH → P3 / Rec.2020 (hot path for LCH renderers without OKLCH detour):\nlabToP3Channels(54.29, 80.8, 69.89);      // [r, g, b] gamma P3\nlchToP3Channels(54.29, 106.84, 40.86);\nlabToRec2020Channels(54.29, 80.8, 69.89); // [r, g, b] gamma Rec.2020\nlchToRec2020Channels(54.29, 106.84, 40.86);\n\n// Split-step API: compute the shared expensive OKLCH→linear sRGB step once,\n// then apply cheap per-space steps to avoid repeating 3× Math.cbrt + OKLab matrix.\nlinearToP3Channels(...linear);      // linear sRGB → gamma-encoded P3\nlinearToRec2020Channels(...linear); // linear sRGB → gamma-encoded Rec.2020 (BT.2020 gamma)\n\n// sRGB ↔ HSL / HSV on the same scale toHsl() / toHsv() report: h in degrees, s and l/v in 0–100.\n// RGB is 0–1 gamma sRGB, like rgbToLinear. This is the per-pixel path for pickers, hue wheels\n// and vectorscopes — no parsing, no object, no rounding.\nimport { hslToRgbChannels, rgbToHslChannels } from '@colordx/core';\nimport { hsvToRgbChannels, rgbToHsvChannels } from '@colordx/core/plugins/hsv';\n\nrgbToHslChannels(1, 0, 0);       // [0, 100, 50]\nrgbToHsvChannels(0, 0, 1);       // [240, 100, 100]\nhslToRgbChannels(120, 100, 50);  // [0, 1, 0]\nhsvToRgbChannels(-30, 50, 50);   // hue wraps: same as hsvToRgbChannels(330, 50, 50)\n// Greys report h = 0, s = 0 (same ACHROMATIC_EPS threshold as toHsl()). Nothing is clipped:\n// HSL/HSV live on the sRGB cube, so clip a wide-gamut color first — for bytes, pass r/255, g/255, b/255.\n```\n\n**Zero-allocation tight-loop variants (`*Into`).** Every channel function has an `*Into` sibling that writes into a caller-provided `Float64Array | number[]` instead of allocating a new tuple. For per-pixel work (canvas renderers, gradient grids, wide-gamut data viz), this eliminates ~10× the GC pressure and makes interactive redraws smoother. Output is bit-for-bit identical to the allocating version.\n\n```ts\nimport {\n  oklchToLinearInto,\n  oklchToRgbChannelsInto,\n  oklchToLinearAndSrgbInto,\n} from '@colordx/core';\nimport { linearToP3ChannelsInto, oklchToP3ChannelsInto } from '@colordx/core/plugins/p3';\nimport { linearToRec2020ChannelsInto, oklchToRec2020ChannelsInto } from '@colordx/core/plugins/rec2020';\n\n// Pixel-renderer pattern: allocate one buffer, reuse for every pixel.\nconst buf = new Float64Array(3);\nfor (let y = 0; y < height; y++) {\n  for (let x = 0; x < width; x++) {\n    const [l, c, h] = getOklch(x, y);\n    oklchToP3ChannelsInto(buf, l, c, h);\n    imageData[i++] = Math.floor(buf[0] * 255);\n    imageData[i++] = Math.floor(buf[1] * 255);\n    imageData[i++] = Math.floor(buf[2] * 255);\n    imageData[i++] = 255;\n  }\n}\n```\n\nFull `*Into` surface (all tree-shakable — unused ones have zero bundle cost):\n\n```ts\n// from '@colordx/core' — OKLCH → linear / sRGB\noklchToLinearInto(out, l, c, h);           // → [lr, lg, lb] linear sRGB\noklchToRgbChannelsInto(out, l, c, h);      // → [r, g, b] gamma-encoded sRGB\noklchToLinearAndSrgbInto(linOut, srgbOut, l, c, h); // both at once (distinct buffers)\n\n// from '@colordx/core' — non-OKLCH inputs → linear / gamma sRGB (complements oklchToLinear)\nrgbToLinearInto(out, r, g, b);             // 0–1 gamma sRGB → linear sRGB\nlabToLinearSrgbInto(out, l, a, b);         // CIE Lab (D50) → linear sRGB (via XYZ D50)\nlabToRgbChannelsInto(out, l, a, b);        // CIE Lab (D50) → gamma sRGB\nlabToLinearAndSrgbInto(linOut, srgbOut, l, a, b); // both (distinct buffers)\nlchToLinearSrgbInto(out, l, c, h);         // CIE LCH (D50) → linear sRGB\nlchToRgbChannelsInto(out, l, c, h);        // CIE LCH (D50) → gamma sRGB\nlchToLinearAndSrgbInto(linOut, srgbOut, l, c, h);\n\n// from '@colordx/core' — sRGB ↔ HSL (h degrees, s/l 0–100; RGB 0–1)\nrgbToHslChannelsInto(out, r, g, b);        // → [h, s, l]\nhslToRgbChannelsInto(out, h, s, l);        // → [r, g, b] gamma sRGB\n\n// from '@colordx/core/plugins/hsv' — sRGB ↔ HSV (h degrees, s/v 0–100; RGB 0–1)\nrgbToHsvChannelsInto(out, r, g, b);        // → [h, s, v]\nhsvToRgbChannelsInto(out, h, s, v);        // → [r, g, b] gamma sRGB\n\n// from '@colordx/core/plugins/p3'\nlinearToP3ChannelsInto(out, lr, lg, lb);\noklchToP3ChannelsInto(out, l, c, h);\nlabToP3ChannelsInto(out, l, a, b);\nlchToP3ChannelsInto(out, l, c, h);\n\n// from '@colordx/core/plugins/rec2020'\nlinearToRec2020ChannelsInto(out, lr, lg, lb);\noklchToRec2020ChannelsInto(out, l, c, h);\nlabToRec2020ChannelsInto(out, l, a, b);\nlchToRec2020ChannelsInto(out, l, c, h);\n```\n\nGuidance:\n- Use `Float64Array(3)` for the buffer when you can — it's the convention and keeps the V8 call site monomorphic. `number[]` also works.\n- One buffer per loop is plenty; don't allocate per iteration.\n- `linOut` and `srgbOut` in `oklchToLinearAndSrgbInto` **must be distinct buffers** (the function writes to both).\n- If you're outside a hot loop, the regular allocating versions are more ergonomic — reach for `*Into` only when you've profiled and GC is the bottleneck.\n\n### Gamut\n\n`oklch()` and `oklab()` can describe colors outside the sRGB gamut. **For everyday conversion, `.toRgbString()` / `.toHex()` already do the right thing** — they naive-clip in linear sRGB to match browser rendering, so your output matches what `background: oklch(...)` displays on screen. You only need the methods below when that default isn't what you want.\n\nInternally, out-of-gamut `oklch()` / `oklab()` inputs are stored **unclamped**, so the authored color is preserved losslessly. That means `.toOklchString()` round-trips the original, and you can choose when (and how) to fold the color into sRGB:\n\n```ts\nconst input = 'oklch(0.5 0.4 180)';  // out of sRGB gamut\n\n// 1. Preserve — keep the authored oklch as-is, clip only at sRGB output time\ncolordx(input).toOklchString();          // 'oklch(0.5 0.4 180)'\ncolordx(input).toRgbString();            // 'rgb(0 152 108)' — naive clip, matches browser\n\n// 2. Map — CSS Color 4 gamut mapping (preserves lightness + hue, reduces chroma)\ncolordx(input).mapSrgb().toOklchString();   // 'oklch(0.50907 0.09379 177.84892)'\ncolordx(input).mapSrgb().toRgbString();     // 'rgb(0 119 102)'\n\n// 3. Clamp — naive-clip into sRGB as a Colordx (matches browser, but hue drifts)\ncolordx(input).clampSrgb().toOklchString(); // 'oklch(0.60125 0.1276 164.29892)'\ncolordx(input).clampSrgb().toRgbString();   // 'rgb(0 152 108)' — same bytes as (1)\n```\n\n- **`.mapSrgb()`** — CSS Color 4 chroma-reduction binary search. Preserves lightness and hue; sacrifices chroma. Use when hue stability matters — design tokens, palettes, programmatic harmonies, OKLCH pickers.\n- **`.clampSrgb()`** — naive clip in linear sRGB. Hue and lightness may drift. Use when you want a `Colordx` whose `.toOklchString()` describes what browsers actually render.\n\nWhich color a method sees follows from its model. Wide-gamut models (`toOklab`, `toOklch`, `toLab`, `toLch`, `toXyz*`, `toP3`, `toRec2020`, `toA98`, `toProphoto`, `toSrgbLinear`, `mixOklab`, `mixLab`, `delta`) read the unclamped color. sRGB-bounded models (`toRgb`, `toHex`, `toHsl`, `toHsv`, `toHwb`, `toCmyk`, `toName`, `brightness`) and the HSL-based manipulators read the naive-clipped color, so `.toHslString()` always names the same color as `.toHex()`. The a11y and cvd plugins gamut-map (not clip) first.\n\nA static form is also available for one-shot conversion without wrapping first — `Colordx.toGamutSrgb(input)` is equivalent to `colordx(input).mapSrgb()`.\n\ncolordx also includes standalone utilities for checking and mapping into wider gamuts (Display-P3 / Rec.2020, via plugins):\n\n```ts\nimport { Colordx, inGamutSrgb } from '@colordx/core';\nimport { inGamutP3 } from '@colordx/core/plugins/p3';\nimport { inGamutRec2020 } from '@colordx/core/plugins/rec2020';\nimport p3 from '@colordx/core/plugins/p3';\nimport rec2020 from '@colordx/core/plugins/rec2020';\nextend([p3, rec2020]);\n\n// Check: is this color displayable in sRGB?\ninGamutSrgb('#ff0000'); // true  — hex is always sRGB\ninGamutSrgb('oklch(0.5 0.1 30)'); // true  — clearly in sRGB\ninGamutSrgb('oklch(0.5 0.4 180)'); // false — too much cyan chroma\n\n// Map: reduce chroma until in-gamut (preserves lightness and hue)\nColordx.toGamutSrgb('oklch(0.5 0.4 180)'); // → Colordx at the sRGB boundary\nColordx.toGamutSrgb('#ff0000'); // → unchanged, already in sRGB\n\n// Display-P3 gamut (wider than sRGB) — available after extend([p3])\ninGamutP3('oklch(0.64 0.27 29)'); // true  — inside P3 but outside sRGB\ninGamutP3('oklch(0.5 0.4 180)'); // false — outside P3\nColordx.toGamutP3('oklch(0.5 0.4 180)'); // → Colordx at the P3 boundary\n\n// Rec.2020 gamut (wider than P3) — available after extend([rec2020])\ninGamutRec2020('oklch(0.5 0.4 180)'); // false — outside Rec.2020\nColordx.toGamutRec2020('oklch(0.5 0.4 180)'); // → Colordx at the Rec.2020 boundary\n```\n\nGamut containment is largely hierarchical: sRGB ⊂ Display-P3 ⊂ Rec.2020 ⊂ ProPhoto. A98 (Adobe RGB 1998) sits between sRGB and Rec.2020 — wider than sRGB, mostly in the greens — but is not a strict superset of Display-P3. All `inGamut*` functions always return `true` for sRGB-bounded inputs (hex, rgb, hsl, hsv, hwb). The `toGamut*` functions use a binary chroma-reduction search following the [CSS Color 4 gamut mapping algorithm](https://www.w3.org/TR/css-color-4/#css-gamut-mapping).\n\nGamut checks and mapping accept wide-gamut inputs in every supported form — `oklab()` / `oklch()`, CIE `lab()` / `lch()` strings, `color(display-p3 …)` and friends, and the corresponding object shapes (including branded `{ colorSpace: 'lab' | 'lch' }` objects). `oklab` / `oklch` are read by core; any other format needs its plugin loaded via `extend()`, exactly like `colordx()` itself. A plugin's own helpers (`inGamutP3`, `inGamutRec2020`, …) always understand their own format:\n\n```ts\nextend([lab, lch]);\n\n// CIE LCH object — recognized as wide-gamut, not clamped at parse time\nconst lch = { l: 50, c: 100, h: 180, alpha: 1, colorSpace: 'lch' as const };\ninGamutSrgb(lch);  // false — outside sRGB\ninGamutP3(lch);    // false — outside P3\ncolordx(lch).toHex();           // '#009774' — naive clip\ncolordx(lch).mapSrgb().toHex(); // '#008471' — chroma-reduced (CSS Color 4)\n\n// CIE Lab string works too\ninGamutSrgb('lab(50 100 0)'); // false\nColordx.toGamutSrgb('lab(50 100 0)'); // → Colordx at the sRGB boundary\n```\n\n## Plugins\n\nOpt-in plugins for less common color spaces and utilities:\n\n```ts\nimport { extend } from '@colordx/core';\nimport a11y from '@colordx/core/plugins/a11y';\n// isReadable(), readableScore(), minReadable(), apcaContrast(), isReadableApca()\nimport cmyk from '@colordx/core/plugins/cmyk';\n// toCmyk(), toCmykString(), parses device-cmyk() strings and CMYK objects\nimport cvd from '@colordx/core/plugins/cvd';\n// simulate('protanopia' | 'deuteranopia' | 'tritanopia')\nimport harmonies from '@colordx/core/plugins/harmonies';\n// harmonies()\nimport hwb from '@colordx/core/plugins/hwb';\n// toHwb(), toHwbString(), parses hwb() strings and HWB objects\nimport hsv from '@colordx/core/plugins/hsv';\n// toHsv(), toHsvString(), parses hsv() strings and HSV objects\nimport lab from '@colordx/core/plugins/lab';\n// toLab(), toLabString(), toXyz(), toXyzString(), toXyzD65(), toXyzD65String(), mixLab(), delta(), parses Lab/XYZ(D50+D65) objects and strings\nimport lch from '@colordx/core/plugins/lch';\n// toLch(), toLchString(), parses lch() strings and LCH objects\nimport minify from '@colordx/core/plugins/minify';\n// minify() — shortest CSS string\nimport mix from '@colordx/core/plugins/mix';\n// tints(), shades(), tones(), palette()\nimport names from '@colordx/core/plugins/names';\n// toName(), parses CSS color names\nimport p3 from '@colordx/core/plugins/p3';\n// toP3(), toP3String(), inGamutP3(), Colordx.toGamutP3(), linearToP3Channels(), oklchToP3Channels(), parses color(display-p3 ...) strings\nimport rec2020 from '@colordx/core/plugins/rec2020';\n// toRec2020(), toRec2020String(), inGamutRec2020(), Colordx.toGamutRec2020(), linearToRec2020Channels(), oklchToRec2020Channels(), parses color(rec2020 ...) strings\nimport a98rgb from '@colordx/core/plugins/a98rgb';\n// toA98(), toA98String(), inGamutA98(), Colordx.toGamutA98(), linearToA98Channels(), oklchToA98Channels(), parses color(a98-rgb ...) strings\nimport prophoto from '@colordx/core/plugins/prophoto';\n// toProphoto(), toProphotoString(), inGamutProphoto(), Colordx.toGamutProphoto(), linearToProphotoChannels(), oklchToProphotoChannels(), parses color(prophoto-rgb ...) strings\nimport srgbLinear from '@colordx/core/plugins/srgb-linear';\n// toSrgbLinear(), toSrgbLinearString(), parses color(srgb-linear ...) strings\n\nextend([lab, lch, cmyk, names, a11y, harmonies, hwb, hsv, mix, minify, p3, rec2020, a98rgb, prophoto, srgbLinear]);\n```\n\n### lab plugin\n\nCIE Lab (D50), CIE XYZ (D50), and CIE XYZ (D65) color models. Lab and XYZ objects are also accepted as color input (Lab requires a `colorSpace: 'lab'` discriminant; XYZ D65 requires `colorSpace: 'xyz-d65'`; plain `{ x, y, z }` parses as D50). Also adds `.mixLab()` for perceptual mixing in CIE Lab, `.delta()` for CIEDE2000 color difference, and string conversion methods.\n\n```ts\nimport lab from '@colordx/core/plugins/lab';\n\nextend([lab]);\n\ncolordx('#ff0000').toLab(); // { l: 54.29, a: 80.8, b: 69.89, alpha: 1, colorSpace: 'lab' }\ncolordx('#ff0000').toLabString(); // 'lab(54.29 80.8 69.89)'\ncolordx('lab(54.29 80.8 69.89)').toHex(); // '#ff0000'  — lab strings are parseable\n\n// XYZ D50 (chromatic-adapted; matches Lab's white point)\ncolordx('#ff0000').toXyz(); // { x: 43.61, y: 22.25, z: 1.39, alpha: 1 }\ncolordx('#ff0000').toXyzString(); // 'color(xyz-d50 0.4361 0.2225 0.0139)' — CSS scale (1 = reference white)\n\n// XYZ D65 (screen-native; no Bradford adaptation — same illuminant as sRGB/OKLab)\ncolordx('#ff0000').toXyzD65(); // { x: 41.24, y: 21.26, z: 1.93, alpha: 1, colorSpace: 'xyz-d65' }\ncolordx('#ff0000').toXyzD65String(); // 'color(xyz-d65 0.4124 0.2126 0.0193)'\n\n// Lab and XYZ objects parse as color input (with lab plugin loaded)\n// Lab objects require colorSpace: 'lab' to distinguish from OKLab (which has the same l/a/b shape)\ncolordx({ l: 54.29, a: 80.8, b: 69.89, colorSpace: 'lab' as const }).toHex(); // '#ff0000'\ncolordx({ x: 43.61, y: 22.25, z: 1.39 }).toHex(); // '#ff0000' (D50)\ncolordx({ x: 41.24, y: 21.26, z: 1.93, colorSpace: 'xyz-d65' as const }).toHex(); // '#ff0000'\n\n// color() strings parse for both white points. Per CSS Color 4 the string channels are 0–1\n// (1 = reference-white Y, 100% = 1), unlike the 0–100 object convention above.\ncolordx('color(xyz-d50 0.4361 0.2225 0.0139)').toHex(); // '#ff0000'\ncolordx('color(xyz-d65 0.4124 0.2126 0.0193)').toHex(); // '#ff0000'\ncolordx('color(xyz 0.4124 0.2126 0.0193)').toHex(); // '#ff0000' — bare xyz is the spec alias for xyz-d65\ncolordx('color(xyz-d65 41.24% 21.26% 1.93%)').toHex(); // '#ff0000'\n\n// Mix in CIE Lab space\ncolordx('#000000').mixLab('#ffffff').toHex(); // '#777777'\n\n// CIEDE2000 perceptual color difference (0 = identical, ~1 = maximum)\ncolordx('#ff0000').delta('#ff0000'); // 0\ncolordx('#000000').delta('#ffffff'); // ~1\ncolordx('#ff0000').delta(); // compared against white (default)\ncolordx('#ef4444').delta('#10b981', 5); // 0.71465 — optional precision (default 3)\n```\n\n### lch plugin\n\nCIE LCH (D50) — the polar form of CIE Lab. Parses `lch()` CSS strings and LCH objects.\n\n```ts\nimport lch from '@colordx/core/plugins/lch';\n\nextend([lch]);\n\ncolordx('#ff0000').toLch(); // { l: 54.29, c: 106.84, h: 40.86, alpha: 1, colorSpace: 'lch' }\ncolordx('#ff0000').toLchString(); // 'lch(54.29 106.84 40.86)'\ncolordx('lch(54.29 106.84 40.86)').toHex(); // '#ff0000'\n// LCH objects require colorSpace: 'lch' to distinguish from OKLCH (which has the same l/c/h shape)\ncolordx({ l: 50, c: 50, h: 180, colorSpace: 'lch' as const }).toHex(); // parses as LCH object\n```\n\n### cmyk plugin\n\nCMYK color model. Parses `device-cmyk()` CSS strings and CMYK objects.\n\n```ts\nimport cmyk from '@colordx/core/plugins/cmyk';\n\nextend([cmyk]);\n\ncolordx('#ff0000').toCmyk(); // { c: 0, m: 100, y: 100, k: 0, alpha: 1 }\ncolordx('#ff0000').toCmykString(); // 'device-cmyk(0% 100% 100% 0%)'\ncolordx('device-cmyk(0% 100% 100% 0%)').toHex(); // '#ff0000'\ncolordx({ c: 0, m: 100, y: 100, k: 0 }).toHex(); // '#ff0000'\n```\n\n### names plugin\n\nCSS named color support (140 names from the CSS spec). `toName()` returns `undefined` for colors with no CSS name.\n\n```ts\nimport names from '@colordx/core/plugins/names';\n\nextend([names]);\n\ncolordx('red').toHex(); // '#ff0000'\ncolordx('rebeccapurple').toHex(); // '#663399'\ncolordx('#ff0000').toName(); // 'red'\ncolordx('#c06060').toName(); // undefined — no CSS name for this color\ncolordx('#c06060').toName({ closest: true }); // nearest named color by RGB distance\n```\n\n### hsv plugin\n\nHSV/HSVa color model. Parses `hsv()` / `hsva()` strings and HSV objects.\n\n```ts\nimport hsv from '@colordx/core/plugins/hsv';\n\nextend([hsv]);\n\ncolordx('#ff0000').toHsv(); // { h: 0, s: 100, v: 100, alpha: 1 }\ncolordx('#ff0000').toHsvString(); // 'hsv(0 100% 100%)'\ncolordx('hsv(0 100% 100%)').toHex(); // '#ff0000'\ncolordx({ h: 0, s: 100, v: 100, alpha: 1 }).toHex(); // '#ff0000'\n```\n\nThe plugin entry also exports the HSV channel functions — `rgbToHsvChannels`, `hsvToRgbChannels` and their `*Into` siblings — for per-pixel work such as a saturation/value plane:\n\n```ts\nimport { hsvToRgbChannelsInto } from '@colordx/core/plugins/hsv';\n\n// 256×256 S/V plane for a fixed hue → RGBA bytes. One buffer, no allocation per pixel.\nconst buf = new Float64Array(3);\nconst plane = new Uint8ClampedArray(256 * 256 * 4);\nlet i = 0;\nfor (let y = 0; y < 256; y++) {\n  for (let x = 0; x < 256; x++) {\n    hsvToRgbChannelsInto(buf, 210, (x / 255) * 100, (1 - y / 255) * 100);\n    plane[i++] = buf[0] * 255;\n    plane[i++] = buf[1] * 255;\n    plane[i++] = buf[2] * 255;\n    plane[i++] = 255;\n  }\n}\n```\n\n### harmonies plugin\n\nColor harmony generation using hue rotation.\n\n```ts\nimport harmonies from '@colordx/core/plugins/harmonies';\n\nextend([harmonies]);\n\ncolordx('#ff0000').harmonies();                              // complementary (default) — 2 colors\ncolordx('#ff0000').harmonies('complementary');               // [0°, 180°] — 2 colors\ncolordx('#ff0000').harmonies('analogous');                   // [−30°, 0°, 30°] — 3 colors\ncolordx('#ff0000').harmonies('split-complementary');         // [0°, 150°, 210°] — 3 colors\ncolordx('#ff0000').harmonies('triadic');                     // [0°, 120°, 240°] — 3 colors\ncolordx('#ff0000').harmonies('tetradic');                    // [0°, 90°, 180°, 270°] — 4 colors (square)\ncolordx('#ff0000').harmonies('rectangle');                   // [0°, 60°, 180°, 240°] — 4 colors\ncolordx('#ff0000').harmonies('double-split-complementary');  // [−30°, 0°, 30°, 150°, 210°] — 5 colors\n```\n\n### hwb plugin\n\nCSS Color Level 4 HWB (Hue, Whiteness, Blackness) color model.\n\n```ts\nimport hwb from '@colordx/core/plugins/hwb';\n\nextend([hwb]);\n\ncolordx('#ff0000').toHwb();         // { h: 0, w: 0, b: 0, alpha: 1 }\ncolordx('#ff0000').toHwbString();   // 'hwb(0 0% 0%)'\ncolordx('hwb(0 0% 0%)').toHex();   // '#ff0000'\ncolordx({ h: 0, w: 0, b: 0, alpha: 1 }).toHex(); // '#ff0000'\n\n// toHwb accepts an optional precision argument (decimal places):\ncolordx('#3d7a9f').toHwb();    // { h: 203, w: 24, b: 38, alpha: 1 }   — default (0)\ncolordx('#3d7a9f').toHwb(2);   // { h: 202.65, w: 23.92, b: 37.65, alpha: 1 }\ncolordx('#3d7a9f').toHwbString();  // 'hwb(203 24% 38%)'\ncolordx('#3d7a9f').toHwbString(2); // 'hwb(202.65 23.92% 37.65%)'\n```\n\n### mix plugin\n\nColor mixing helpers built on top of `.mix()`.\n\n```ts\nimport mix from '@colordx/core/plugins/mix';\n\nextend([mix]);\n\ncolordx('#ff0000').tints(5); // [#ff0000, #ff4040, #ff8080, #ffbfbf, #ffffff]\ncolordx('#ff0000').shades(3); // [#ff0000, #800000, #000000]\ncolordx('#ff0000').tones(3);  // [#ff0000, #c04040, #808080]\n\n// palette: N evenly-spaced stops toward any target (default: white)\ncolordx('#ff0000').palette(3, '#0000ff'); // [#ff0000, #800080, #0000ff]\n```\n\n### minify plugin\n\nReturns the shortest valid CSS representation of a color. By default tries hex, RGB, and HSL and picks the shortest.\n\n```ts\nimport minify from '@colordx/core/plugins/minify';\n\nextend([minify]);\n\ncolordx('#ff0000').minify(); // '#f00'\ncolordx('#ffffff').minify(); // '#fff'\ncolordx('#ff0000').minify({ name: true }); // 'red'  — requires names plugin\ncolordx({ r: 0, g: 0, b: 0, alpha: 0 }).minify({ transparent: true }); // 'transparent'\ncolordx({ r: 255, g: 0, b: 0, alpha: 0.5 }).minify({ alphaHex: true }); // '#ff000080'\n\n// Disable specific formats to exclude them from candidates:\ncolordx('#ff0000').minify({ hsl: false }); // skips HSL, picks from hex/RGB\n```\n\n### a11y plugin\n\nWCAG 2.x contrast:\n\n```ts\ncolordx('#000').isReadable('#fff'); // true  — AA normal (ratio >= 4.5)\ncolordx('#000').isReadable('#fff', { level: 'AAA' }); // true  — AAA normal (ratio >= 7)\ncolordx('#000').isReadable('#fff', { size: 'large' }); // true  — AA large (ratio >= 3)\ncolordx('#000').readableScore('#fff'); // 'AAA'\ncolordx('#e60000').readableScore('#ffff47'); // 'AA'\ncolordx('#949494').readableScore('#fff'); // 'AA large'\ncolordx('#aaa').readableScore('#fff'); // 'fail'\ncolordx('#777').minReadable('#fff'); // fixContrast at 4.5, or the input if nothing passes\n```\n\n`fixContrast` finds the nearest color that passes a gate. It keeps hue, moves OKLCH lightness, lets the gamut map reduce chroma only when it must, and returns `null` when no color with that hue passes. A darker fg stays darker (APCA sign is kept) unless nothing on that side passes.\n\n```ts\ncolordx('#3b82f6').fixContrast('#fff'); // #2c72e5 — WCAG 4.5 by default\ncolordx('#3b82f6').fixContrast('#fff', { apca: 75 }); // APCA only, |Lc| >= 75\ncolordx('#3b82f6').fixContrast('#fff', { wcag: 4.5, apca: 75 }); // #2168da — both\ncolordx('#999').fixContrast('#777', { wcag: 7 }); // null — no grey reaches 7:1 on #777\ncolordx('oklch(0.9 0.3 145)').fixContrast('#fff', { space: 'p3' }); // fix stays inside Display-P3\n```\n\nNumbers are rounded for display only (`contrast` to 2 decimals, `apcaContrast` to 1). Every `isReadable*` / `readableScore` gate uses the unrounded value, so a true 4.496:1 fails AA even though `contrast()` shows 4.5. Pass a precision to see more digits:\n\n```ts\ncolordx('#d200d2').contrast('#fff'); // 4.5\ncolordx('#d200d2').contrast('#fff', 4); // 4.4959\ncolordx('#d200d2').isReadable('#fff'); // false\n```\n\nA translucent fg is composited over the bg first. To check a stack (fg over a translucent surface over the page), flatten it bottom up with `over()`:\n\n```ts\nconst surface = colordx('rgba(255, 255, 255, 0.2)').over('#000');\ncolordx('rgba(255, 255, 255, 0.6)').over(surface).contrast('#000');\n```\n\nColors outside sRGB are gamut-mapped (not clipped) before the check. WCAG always runs on the sRGB-mapped color; APCA can run on the Display-P3-mapped color with its own coefficients via `{ space: 'p3' }`.\n\nAPCA (Accessible Perceptual Contrast Algorithm) — the projected replacement for WCAG 2.x in WCAG 3.0:\n\n```ts\n// Returns a signed Lc value: positive = dark text on light bg, negative = light text on dark bg\ncolordx('#000').apcaContrast('#fff'); //  106.0\ncolordx('#fff').apcaContrast('#000'); // -107.9\ncolordx('#202122').apcaContrast('#cf674a'); //  37.2  ← dark text on orange\ncolordx('#ffffff').apcaContrast('#cf674a'); // -69.5  ← white text on orange\n\n// Checks readability using |Lc| thresholds: >= 75 for normal text, >= 60 for large text/headings\ncolordx('#000').isReadableApca('#fff'); // true\ncolordx('#777').isReadableApca('#fff'); // false\ncolordx('#777').isReadableApca('#fff', { size: 'large' }); // true\n\n// Options: precision (default 1) and space ('srgb' | 'p3', default 'srgb')\ncolordx('#9900ff').apcaContrast('#fff', { precision: 3 }); // 74.956\ncolordx('oklch(0.8 0.3 145)').apcaContrast('#000', { space: 'p3' }); // -73.3 (P3-mapped), vs -74.8 in sRGB\ncolordx('#777').isReadableApca('#fff', { space: 'p3' }); // false\n```\n\nAPCA is better suited than WCAG 2.x for dark color pairs and more accurately reflects human perception. See [Introduction to APCA](https://git.apcacontrast.com/documentation/APCAeasyIntro) for background.\n\n### cvd plugin\n\nSimulates colour vision deficiency. Machado 2009 (severity 1.0) for protanopia and deuteranopia, the same matrices Chrome and Firefox DevTools use; Brettel 1997 for tritanopia, where Machado is weak. Runs on linear sRGB; wide-gamut input is gamut-mapped first. Reference values are checked against DaltonLens in the test suite.\n\n```ts\ncolordx('#ff0000').simulate('protanopia'); // #6d5f00\ncolordx('#ff0000').simulate('deuteranopia'); // #a39000\ncolordx('#0000ff').simulate('tritanopia'); // #006087\n\n// Do two status colours collapse? Compare ΔE2000 before and after (lab plugin).\nconst [a, b] = [colordx('#ef4444'), colordx('#10b981')];\na.delta(b, 5); // 0.71465 — clearly distinct\na.simulate('deuteranopia').delta(b.simulate('deuteranopia'), 5); // ~0.14 — nearly the same colour\n```\n\n### p3 plugin\n\nAdds Display-P3 color space support. P3 has a wider gamut than sRGB and is natively supported by all modern browsers and most Mac/iOS displays.\n\n```ts\nimport p3 from '@colordx/core/plugins/p3';\n\nextend([p3]);\n\ncolordx('#ff0000').toP3(); // { r: 0.9175, g: 0.2003, b: 0.1386, alpha: 1, colorSpace: 'display-p3' }\ncolordx('#ff0000').toP3String(); // 'color(display-p3 0.9175 0.2003 0.1386)'\n\n// Parse Display-P3 strings (alpha optional)\ncolordx('color(display-p3 0.9175 0.2003 0.1386)').toHex(); // '#ff0000'\ncolordx('color(display-p3 0.9175 0.2003 0.1386 / 0.5)').toHex(); // '#ff000080'\n```\n\nThe plugin also exports standalone gamut utilities and low-level channel functions. `inGamutP3` and the channel helpers need no `extend()`. Gamut mapping is available as `Colordx.toGamutP3` after `extend([p3])`:\n\n```ts\nimport { Colordx, extend } from '@colordx/core';\nimport p3, { inGamutP3, linearToP3Channels, oklchToP3Channels } from '@colordx/core/plugins/p3';\n\nextend([p3]);\n\ninGamutP3('oklch(0.64 0.27 29)');        // true — inside P3 but outside sRGB\nColordx.toGamutP3('oklch(0.5 0.4 180)'); // → Colordx at the P3 boundary\n\noklchToP3Channels(0.5, 0.2, 240); // [r, g, b] gamma-encoded P3 in [0, 1]\n```\n\nObject parsing is also supported using the `colorSpace` discriminant:\n\n```ts\ncolordx({ r: 0.9505, g: 0.2856, b: 0.0459, alpha: 1, colorSpace: 'display-p3' }).toHex();\n```\n\n### rec2020 plugin\n\nAdds Rec.2020 (BT.2020) color space support. Rec.2020 has the widest gamut of the three — it covers most of the visible spectrum.\n\n```ts\nimport rec2020 from '@colordx/core/plugins/rec2020';\n\nextend([rec2020]);\n\ncolordx('#ff0000').toRec2020(); // { r: 0.792, g: 0.231, b: 0.0738, alpha: 1, colorSpace: 'rec2020' }\ncolordx('#ff0000').toRec2020String(); // 'color(rec2020 0.792 0.231 0.0738)'\n\n// Parse Rec.2020 strings (alpha optional)\ncolordx('color(rec2020 0.792 0.231 0.0738)').toHex(); // '#ff0000'\ncolordx('color(rec2020 0.792 0.231 0.0738 / 0.5)').toHex(); // '#ff000080'\n```\n\nThe plugin also exports standalone gamut utilities and low-level channel functions. `inGamutRec2020` and the channel helpers need no `extend()`. Gamut mapping is available as `Colordx.toGamutRec2020` after `extend([rec2020])`:\n\n```ts\nimport { Colordx, extend } from '@colordx/core';\nimport rec2020, { inGamutRec2020, linearToRec2020Channels, oklchToRec2020Channels } from '@colordx/core/plugins/rec2020';\n\nextend([rec2020]);\n\ninGamutRec2020('oklch(0.5 0.4 180)');        // false — outside Rec.2020\nColordx.toGamutRec2020('oklch(0.5 0.4 180)'); // → Colordx at the Rec.2020 boundary\n\noklchToRec2020Channels(0.5, 0.2, 240); // [r, g, b] gamma-encoded Rec.2020 in [0, 1]\n```\n\nObject parsing is also supported using the `colorSpace` discriminant:\n\n```ts\ncolordx({ r: 0.7919, g: 0.2307, b: 0.0739, alpha: 1, colorSpace: 'rec2020' }).toHex();\n```\n\n### a98rgb plugin\n\nAdds A98 (Adobe RGB 1998) color space support. A98 shares sRGB's red and blue primaries but a wider green primary, so it extends mainly into the greens/cyans. It is a CSS Color 4 predefined space and a long-standing photography/print working space.\n\n```ts\nimport a98rgb from '@colordx/core/plugins/a98rgb';\n\nextend([a98rgb]);\n\ncolordx('#ff0000').toA98(); // { r: 0.8586, g: 0, b: 0, alpha: 1, colorSpace: 'a98-rgb' }\ncolordx('#ff0000').toA98String(); // 'color(a98-rgb 0.8586 0 0)'\n\n// Parse A98 strings (alpha optional)\ncolordx('color(a98-rgb 0.8586 0 0)').toHex(); // '#ff0000'\ncolordx('color(a98-rgb 0.8586 0 0 / 0.5)').toHex(); // '#ff000080'\n```\n\nThe plugin also exports standalone gamut utilities and low-level channel functions. `inGamutA98` and the channel helpers need no `extend()`. Gamut mapping is available as `Colordx.toGamutA98` after `extend([a98rgb])`:\n\n```ts\nimport { Colordx, extend } from '@colordx/core';\nimport a98rgb, { inGamutA98, linearToA98Channels, oklchToA98Channels } from '@colordx/core/plugins/a98rgb';\n\nextend([a98rgb]);\n\ninGamutA98('oklch(0.7 0.25 150)');        // true — inside A98 but outside sRGB\nColordx.toGamutA98('oklch(0.5 0.4 180)'); // → Colordx at the A98 boundary\n\noklchToA98Channels(0.5, 0.2, 240); // [r, g, b] gamma-encoded A98 in [0, 1]\n```\n\nObject parsing is also supported using the `colorSpace` discriminant:\n\n```ts\ncolordx({ r: 0.8586, g: 0, b: 0, alpha: 1, colorSpace: 'a98-rgb' }).toHex();\n```\n\n### prophoto plugin\n\nAdds ProPhoto (ROMM RGB) color space support. ProPhoto has an extremely wide gamut — wider than Rec.2020, covering most of the visible spectrum — and uses a D50 white point with a gamma-1.8 transfer curve. It is the standard working space for high-end RAW photo and print pipelines.\n\n```ts\nimport prophoto from '@colordx/core/plugins/prophoto';\n\nextend([prophoto]);\n\ncolordx('#ff0000').toProphoto(); // { r: 0.7022, g: 0.2757, b: 0.1035, alpha: 1, colorSpace: 'prophoto-rgb' }\ncolordx('#ff0000').toProphotoString(); // 'color(prophoto-rgb 0.7022 0.2757 0.1035)'\n\n// Parse ProPhoto strings (alpha optional)\ncolordx('color(prophoto-rgb 0.7022 0.2757 0.1035)').toHex(); // '#ff0000'\ncolordx('color(prophoto-rgb 0.7022 0.2757 0.1035 / 0.5)').toHex(); // '#ff000080'\n```\n\nThe plugin also exports standalone gamut utilities and low-level channel functions. `inGamutProphoto` and the channel helpers need no `extend()`. Gamut mapping is available as `Colordx.toGamutProphoto` after `extend([prophoto])`:\n\n```ts\nimport { Colordx, extend } from '@colordx/core';\nimport prophoto, { inGamutProphoto, linearToProphotoChannels, oklchToProphotoChannels } from '@colordx/core/plugins/prophoto';\n\nextend([prophoto]);\n\ninGamutProphoto('oklch(0.5 0.4 180)');       // false — outside even ProPhoto\nColordx.toGamutProphoto('oklch(0.5 0.4 180)'); // → Colordx at the ProPhoto boundary\n\noklchToProphotoChannels(0.5, 0.2, 240); // [r, g, b] gamma-encoded ProPhoto in [0, 1]\n```\n\nObject parsing is also supported using the `colorSpace` discriminant:\n\n```ts\ncolordx({ r: 0.7022, g: 0.2757, b: 0.1035, alpha: 1, colorSpace: 'prophoto-rgb' }).toHex();\n```\n\n### srgb-linear plugin\n\nAdds the CSS Color 4 `srgb-linear` space: sRGB with the transfer curve removed. Same primaries and gamut as sRGB, so `inGamutSrgb()` and `Colordx.toGamutSrgb()` already apply. Values outside `[0, 1]` are kept as-is and only clip on sRGB output.\n\n```ts\nimport srgbLinear from '@colordx/core/plugins/srgb-linear';\n\nextend([srgbLinear]);\n\ncolordx('#3b82f6').toSrgbLinear(); // { r: 0.04374, g: 0.22323, b: 0.92158, alpha: 1, colorSpace: 'srgb-linear' }\ncolordx('#3b82f6').toSrgbLinearString(); // 'color(srgb-linear 0.04374 0.22323 0.92158)'\ncolordx('#3b82f6').toSrgbLinearString(4); // 'color(srgb-linear 0.0437 0.2232 0.9216)'\n\n// Parse srgb-linear strings (alpha optional)\ncolordx('color(srgb-linear 1 0 0)').toHex(); // '#ff0000'\ncolordx('color(srgb-linear 100% none 0 / 0.5)').toHex(); // '#ff000080'\n\n// Out-of-gamut values survive until sRGB output\ncolordx('oklch(0.7 0.3 150)').toSrgbLinearString(); // 'color(srgb-linear -0.1728 0.59844 -0.00771)'\ninGamutSrgb('color(srgb-linear 1.2 0 0)'); // false\n```\n\nObject parsing is also supported using the `colorSpace` discriminant:\n\n```ts\ncolordx({ r: 0.5, g: 0, b: 0, colorSpace: 'srgb-linear' }).toHex(); // '#bc0000'\n```\n\nFor per-pixel work use the core channel helpers instead: `rgbToLinear()` and `oklchToLinear()` return the same linear channels without a `Colordx` instance.\n\n## Migrating from tinycolor2\n\n`@colordx/core/tinycolor` is a drop-in replacement for [tinycolor2](https://github.com/bgrins/TinyColor). Swap the import (or the `require`) and keep the code:\n\n```ts\nimport tinycolor from '@colordx/core/tinycolor'; // was: from 'tinycolor2'\n\ntinycolor('#f00').lighten(20).toHexString();     // '#ff6666'\ntinycolor('rgb 255 0 0').toHslString();          // 'hsl(0, 100%, 50%)'\ntinycolor.mix('#f00', '#00f', 50).toHexString(); // '#800080'\ntinycolor.isReadable('#777', '#fff');            // false\ntinycolor('#f00').toColordx().toOklchString();   // 'oklch(0.62796 0.25768 29.23389)'\n```\n\nSame API, same input quirks (`f00`, `rgb 255 0 0`, `{ h: 0, s: 1, l: 0.5 }` read as fractions), same output strings, mutable instances (`lighten()` changes the instance and returns it). Types come along: `tinycolor.Instance`, `tinycolor.ColorInput`.\n\nWhat is different:\n\n- **Precision.** tinycolor2 truncates percentages to two decimals when it re-parses HSL between operations. colordx keeps full precision, so `lighten()`, `darken()`, `saturate()`, `desaturate()`, `spin()` and the harmony helpers can differ from tinycolor2 by 1/255 on a channel. The test suite checks every operation against tinycolor2 within that bound.\n- **`toName()`** returns `'rebeccapurple'` for `#663399`; tinycolor2 returns `false` there (a lookup bug).\n- **`tinycolor.names`** holds full hex (`ff0000`); tinycolor2 holds shortened hex (`f00`).\n- **CSS angle units** on a hue (`hsl(0.5turn 100% 50%)`, `200grad`, `3.14rad`) convert to degrees. tinycolor2 rejects them as invalid.\n- **`analogous()` / `monochromatic()`** always terminate. tinycolor2 loops forever on a negative or fractional count ([#280](https://github.com/bgrins/TinyColor/issues/280)); here it is floored, with anything below 1 treated as 1.\n- **`.toColordx()`** is new: the immutable `Colordx` underneath, for oklch, gamut mapping and plugins.\n\n## Notes\n\n### `mix()` uses sRGB; use `mixLab()` or `mixOklab()` for perceptual blending\n\n`mix()` interpolates in **sRGB**, matching CSS `color-mix(in srgb, ...)` and how browsers composite layers. Use `mixOklab()` for perceptually uniform blending, or `mixLab()` (lab plugin) for CIE Lab.\n\n```ts\ncolordx('#000000').mix('#ffffff').toHex();       // '#808080' — sRGB (CSS spec)\ncolordx('#000000').mixOklab('#ffffff').toHex();  // '#636363' — Oklab (perceptually uniform)\n\nimport lab from '@colordx/core/plugins/lab';\nextend([lab]);\ncolordx('#000000').mixLab('#ffffff').toHex();    // '#777777' — CIE Lab\n```\n\nThe same applies to `tints()`, `shades()`, and `tones()` from the mix plugin, which all call `.mix()` internally.\n\n### Precision\n\nEvery `toX()` / `toXString()` method accepts an optional `precision` (decimal places), applied uniformly to every channel of that format. Alpha is fixed at 3 dp globally. Hues are wrapped after rounding, so a hue never prints as `360` at any precision. Format-specific defaults (scale-appropriate):\n\n| format | default |\n|---|---|\n| `toHsl`, `toHsv`, `toCmyk`, `toLab`, `toLch`, `toXyz`, `toXyzD65` | `2` |\n| `toHwb` | `0` |\n| `toP3`, `toRec2020`, `toA98`, `toProphoto`, `toXyzString`, `toXyzD65String` | `4` |\n| `toOklab`, `toOklch`, `toSrgbLinear` | `5` |\n\n```ts\ncolordx('#3d7a9f').toHsl();      // { h: 202.65, s: 44.55, l: 43.14, alpha: 1 }\ncolordx('#3d7a9f').toHsl(4);     // { h: 202.6531, s: 44.5455, l: 43.1373, alpha: 1 }\ncolordx('#3d7a9f').toHsl(0);     // { h: 203, s: 45, l: 43, alpha: 1 }\n\ncolordx('#ff0000').toOklchString();   // 'oklch(0.62796 0.25768 29.23389)'\ncolordx('#ff0000').toOklchString(2);  // 'oklch(0.63 0.26 29.23)'\n```\n\nThe `minify()` plugin preserves full HSL precision when building candidates, so minification is lossless — it only picks HSL when the string is genuinely shorter than hex/rgb.\n\n## Relative lighten/darken\n\nBy default, `.lighten(0.1)` shifts lightness by an **absolute** 10 percentage points. Pass `{ relative: true }` to shift by a fraction of the **current** value instead — useful when migrating from Qix's `color` library or when you want proportional adjustments:\n\n```ts\n// Color with l=10%\ncolordx('#1a0000').lighten(0.1); // l = 10 + 10 = 20%  (absolute)\ncolordx('#1a0000').lighten(0.1, { relative: true }); // l = 10 * 1.1 = 11% (relative)\n\n// Color with s=40%\ncolordx('#a35050').saturate(0.1); // s = 40 + 10 = 50%  (absolute)\ncolordx('#a35050').saturate(0.1, { relative: true }); // s = 40 * 1.1 = 44% (relative)\n```\n\nThe same flag works on `.darken()` and `.desaturate()`.\n\n## Roadmap\n\n### CSS Color 4/5 completeness\n\n- **`color-mix()`** — parse and evaluate `color-mix(in oklch, red 30%, blue)` strings, with support for all interpolation spaces and polar hue methods (`shorter`, `longer`, `increasing`, `decreasing`)\n- **Relative color syntax** — `oklch(from red l c h)` and channel arithmetic like `oklch(from red l calc(c + 0.1) h)`\n\n### Internals\n\n- Deduplicate the sRGB→XYZ D65 matrix shared between `xyz.ts` and `lab.ts`\n\n## Ecosystem\n\n- [**@colordx/cli**](https://github.com/dkryaklin/colordx-a11y) — color accessibility on the command line, and a skill for coding agents: check a pair, audit a token file, build a scale, solve a theme. WCAG 2.2 and APCA gates, one JSON record per pair, a fix on every fail.\n- [**@colordx/gpu**](https://github.com/dkryaklin/colordx-gpu) — colordx's color math on the GPU: conversions and gamut tests as generated GLSL, parity-tested against this library. For per-pixel workloads — picker charts, gamut visualizations, gradients — where you convert millions of colors per frame instead of one at a time.\n\n## Sponsors\n\nSupported by [Ruslo.app](https://ruslo.app).\n\n## License\n\nMIT\n","publish_time":1774291743042,"_cnpm_publish_time":1774291743042,"dist-tags":{"latest":"7.3.0"}}