Set up comprehensive frontend testing infrastructure

- Install Jest for unit testing with React Testing Library
- Install Playwright for end-to-end testing
- Configure Jest with proper TypeScript support and module mapping
- Create test setup files and utilities for both unit and e2e tests

Components:
* Jest configuration with coverage thresholds
* Playwright configuration with browser automation
* Unit tests for LoginForm, AuthContext, and useSocketIO hook
* E2E tests for authentication, dashboard, and agents workflows
* GitHub Actions workflow for automated testing
* Mock data and API utilities for consistent testing
* Test documentation with best practices

Testing features:
- Unit tests with 70% coverage threshold
- E2E tests with API mocking and user journey testing
- CI/CD integration for automated test runs
- Cross-browser testing support with Playwright
- Authentication system testing end-to-end

🚀 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
anthonyrawlins
2025-07-11 14:06:34 +10:00
parent c6d69695a8
commit aacb45156b
6109 changed files with 777927 additions and 1 deletions

View File

@@ -0,0 +1,9 @@
# Changes to Color Helpers
### 5.0.2
_February 23, 2025_
- Use `Number.isNaN` instead of `NaN` for consistency.
[Full CHANGELOG](https://github.com/csstools/postcss-plugins/tree/main/packages/color-helpers/CHANGELOG.md)

View File

@@ -0,0 +1,18 @@
MIT No Attribution (MIT-0)
Copyright © CSSTools Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,32 @@
# Color Helpers <img src="https://cssdb.org/images/css.svg" alt="for CSS" width="90" height="90" align="right">
[<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/color-helpers.svg" height="20">][npm-url]
[<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url]
[<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
## Usage
Add [Color Helpers] to your project:
```bash
npm install @csstools/color-helpers --save-dev
```
This package exists to join all the different color functions scattered among the Colors 4 and Colors 5 plugins we maintain such as:
* [PostCSS Color Function]
* [PostCSS Lab Function]
* [PostCSS OKLab Function]
## Copyright
This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/tree/main/css-color-4. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
[discord]: https://discord.gg/bUadyRwkJS
[npm-url]: https://www.npmjs.com/package/@csstools/color-helpers
[Color Helpers]: https://github.com/csstools/postcss-plugins/tree/main/packages/color-helpers
[PostCSS Color Function]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-color-function
[PostCSS Lab Function]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-lab-functionw
[PostCSS OKLab Function]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-oklab-function

View File

@@ -0,0 +1,263 @@
"use strict";function multiplyMatrices(t,n){return[t[0]*n[0]+t[1]*n[1]+t[2]*n[2],t[3]*n[0]+t[4]*n[1]+t[5]*n[2],t[6]*n[0]+t[7]*n[1]+t[8]*n[2]]}const t=[.955473421488075,-.02309845494876471,.06325924320057072,-.0283697093338637,1.0099953980813041,.021041441191917323,.012314014864481998,-.020507649298898964,1.330365926242124];
/**
* Bradford chromatic adaptation from D50 to D65
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function D50_to_D65(n){return multiplyMatrices(t,n)}const n=[1.0479297925449969,.022946870601609652,-.05019226628920524,.02962780877005599,.9904344267538799,-.017073799063418826,-.009243040646204504,.015055191490298152,.7518742814281371];
/**
* Bradford chromatic adaptation from D65 to D50
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
*/function D65_to_D50(t){return multiplyMatrices(n,t)}
/**
* @param {number} hue - Hue as degrees 0..360
* @param {number} sat - Saturation as percentage 0..100
* @param {number} light - Lightness as percentage 0..100
* @return {number[]} Array of sRGB components; in-gamut colors in range [0..1]
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/hslToRgb.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/hslToRgb.js
*/function HSL_to_sRGB(t){let n=t[0]%360;const _=t[1]/100,o=t[2]/100;return n<0&&(n+=360),[HSL_to_sRGB_channel(0,n,_,o),HSL_to_sRGB_channel(8,n,_,o),HSL_to_sRGB_channel(4,n,_,o)]}function HSL_to_sRGB_channel(t,n,_,o){const e=(t+n/30)%12;return o-_*Math.min(o,1-o)*Math.max(-1,Math.min(e-3,9-e,1))}
/**
* @param {number} hue - Hue as degrees 0..360
* @param {number} white - Whiteness as percentage 0..100
* @param {number} black - Blackness as percentage 0..100
* @return {number[]} Array of RGB components 0..1
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/hwbToRgb.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/hwbToRgb.js
*/function HWB_to_sRGB(t){const n=t[0],_=t[1]/100,o=t[2]/100;if(_+o>=1){const t=_/(_+o);return[t,t,t]}const e=HSL_to_sRGB([n,100,50]),r=1-_-o;return[e[0]*r+_,e[1]*r+_,e[2]*r+_]}
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function LCH_to_Lab(t){const n=t[2]*Math.PI/180;return[t[0],t[1]*Math.cos(n),t[1]*Math.sin(n)]}
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function Lab_to_LCH(t){const n=180*Math.atan2(t[2],t[1])/Math.PI;return[t[0],Math.sqrt(Math.pow(t[1],2)+Math.pow(t[2],2)),n>=0?n:n+360]}const _=[.3457/.3585,1,.2958/.3585];
/**
* Convert Lab to D50-adapted XYZ
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/function Lab_to_XYZ(t){const n=24389/27,o=216/24389,e=(t[0]+16)/116,r=t[1]/500+e,a=e-t[2]/200;return[(Math.pow(r,3)>o?Math.pow(r,3):(116*r-16)/n)*_[0],(t[0]>8?Math.pow((t[0]+16)/116,3):t[0]/n)*_[1],(Math.pow(a,3)>o?Math.pow(a,3):(116*a-16)/n)*_[2]]}
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js
*/function OKLCH_to_OKLab(t){const n=t[2]*Math.PI/180;return[t[0],t[1]*Math.cos(n),t[1]*Math.sin(n)]}
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js
*/function OKLab_to_OKLCH(t){const n=180*Math.atan2(t[2],t[1])/Math.PI;return[t[0],Math.sqrt(t[1]**2+t[2]**2),n>=0?n:n+360]}const o=[1.2268798758459243,-.5578149944602171,.2813910456659647,-.0405757452148008,1.112286803280317,-.0717110580655164,-.0763729366746601,-.4214933324022432,1.5869240198367816],e=[1,.3963377773761749,.2158037573099136,1,-.1055613458156586,-.0638541728258133,1,-.0894841775298119,-1.2914855480194092];
/**
* Given OKLab, convert to XYZ relative to D65
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js
*/
function OKLab_to_XYZ(t){const n=multiplyMatrices(e,t);return multiplyMatrices(o,[n[0]**3,n[1]**3,n[2]**3])}
/**
* Assuming XYZ is relative to D50, convert to CIE Lab
* from CIE standard, which now defines these as a rational fraction
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function XYZ_to_Lab(t){const n=compute_f(t[0]/_[0]),o=compute_f(t[1]/_[1]);return[116*o-16,500*(n-o),200*(o-compute_f(t[2]/_[2]))]}const r=216/24389,a=24389/27;function compute_f(t){return t>r?Math.cbrt(t):(a*t+16)/116}const i=[.819022437996703,.3619062600528904,-.1288737815209879,.0329836539323885,.9292868615863434,.0361446663506424,.0481771893596242,.2642395317527308,.6335478284694309],l=[.210454268309314,.7936177747023054,-.0040720430116193,1.9779985324311684,-2.42859224204858,.450593709617411,.0259040424655478,.7827717124575296,-.8086757549230774];
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*
* XYZ <-> LMS matrices recalculated for consistent reference white
* @see https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-943521484
*/
function XYZ_to_OKLab(t){const n=multiplyMatrices(i,t);return multiplyMatrices(l,[Math.cbrt(n[0]),Math.cbrt(n[1]),Math.cbrt(n[2])])}const s=[30757411/17917100,-6372589/17917100,-4539589/17917100,-.666684351832489,1.616481236634939,467509/29648200,792561/44930125,-1921689/44930125,.942103121235474];
/**
* Convert XYZ to linear-light rec2020
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/const c=[446124/178915,-333277/357830,-72051/178915,-14852/17905,63121/35810,423/17905,11844/330415,-50337/660830,316169/330415];
/**
* Convert XYZ to linear-light P3
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function XYZ_to_lin_P3(t){return multiplyMatrices(c,t)}const u=[1.3457868816471583,-.25557208737979464,-.05110186497554526,-.5446307051249019,1.5082477428451468,.02052744743642139,0,0,1.2119675456389452];
/**
* Convert D50 XYZ to linear-light prophoto-rgb
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/const h=[1829569/896150,-506331/896150,-308931/896150,-851781/878810,1648619/878810,36519/878810,16779/1248040,-147721/1248040,1266979/1248040];
/**
* Convert XYZ to linear-light a98-rgb
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/const m=[12831/3959,-329/214,-1974/3959,-851781/878810,1648619/878810,36519/878810,705/12673,-2585/12673,705/667];
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function XYZ_to_lin_sRGB(t){return multiplyMatrices(m,t)}
/**
* Convert an array of linear-light rec2020 RGB in the range 0.0-1.0
* to gamma corrected form ITU-R BT.2020-2 p.4
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/const p=1.09929682680944,D=.018053968510807;function gam_2020_channel(t){const n=t<0?-1:1,_=Math.abs(t);return _>D?n*(p*Math.pow(_,.45)-(p-1)):4.5*t}
/**
* Convert an array of linear-light sRGB values in the range 0.0-1.0 to gamma corrected form
* Extended transfer function:
* For negative values, linear portion extends on reflection
* of axis, then uses reflected pow below that
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://en.wikipedia.org/wiki/SRGB
*/function gam_sRGB(t){return[gam_sRGB_channel(t[0]),gam_sRGB_channel(t[1]),gam_sRGB_channel(t[2])]}function gam_sRGB_channel(t){const n=t<0?-1:1,_=Math.abs(t);return _>.0031308?n*(1.055*Math.pow(_,1/2.4)-.055):12.92*t}
/**
* Convert an array of linear-light display-p3 RGB in the range 0.0-1.0
* to gamma corrected form
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function gam_P3(t){return gam_sRGB(t)}
/**
* Convert an array of linear-light prophoto-rgb in the range 0.0-1.0
* to gamma corrected form.
* Transfer curve is gamma 1.8 with a small linear portion.
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/const b=1/512;function gam_ProPhoto_channel(t){const n=t<0?-1:1,_=Math.abs(t);return _>=b?n*Math.pow(_,1/1.8):16*t}
/**
* Convert an array of linear-light a98-rgb in the range 0.0-1.0
* to gamma corrected form. Negative values are also now accepted
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function gam_a98rgb_channel(t){const n=t<0?-1:1,_=Math.abs(t);return n*Math.pow(_,256/563)}
/**
* Convert an array of rec2020 RGB values in the range 0.0 - 1.0
* to linear light (un-companded) form.
* ITU-R BT.2020-2 p.4
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/const g=1.09929682680944,X=.018053968510807;function lin_2020_channel(t){const n=t<0?-1:1,_=Math.abs(t);return _<4.5*X?t/4.5:n*Math.pow((_+g-1)/g,1/.45)}const Y=[63426534/99577255,20160776/139408157,47086771/278816314,26158966/99577255,.677998071518871,8267143/139408157,0,19567812/697040785,1.0609850577107909];
/**
* Convert an array of linear-light rec2020 values to CIE XYZ
* using D65 (no chromatic adaptation)
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/
/**
* Convert an array of of sRGB values where in-gamut values are in the range
* [0 - 1] to linear light (un-companded) form.
* Extended transfer function:
* For negative values, linear portion is extended on reflection of axis,
* then reflected power function is used.
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://en.wikipedia.org/wiki/SRGB
*/
function lin_sRGB(t){return[lin_sRGB_channel(t[0]),lin_sRGB_channel(t[1]),lin_sRGB_channel(t[2])]}function lin_sRGB_channel(t){const n=t<0?-1:1,_=Math.abs(t);return _<=.04045?t/12.92:n*Math.pow((_+.055)/1.055,2.4)}
/**
* Convert an array of display-p3 RGB values in the range 0.0 - 1.0
* to linear light (un-companded) form.
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function lin_P3(t){return lin_sRGB(t)}const Z=[608311/1250200,189793/714400,198249/1000160,35783/156275,247089/357200,198249/2500400,0,32229/714400,5220557/5000800];
/**
* Convert an array of linear-light display-p3 values to CIE XYZ
* using D65 (no chromatic adaptation)
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/function lin_P3_to_XYZ(t){return multiplyMatrices(Z,t)}
/**
* Convert an array of prophoto-rgb values where in-gamut Colors are in the
* range [0.0 - 1.0] to linear light (un-companded) form. Transfer curve is
* gamma 1.8 with a small linear portion. Extended transfer function
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/const f=16/512;function lin_ProPhoto_channel(t){const n=t<0?-1:1,_=Math.abs(t);return _<=f?t/16:n*Math.pow(_,1.8)}const L=[.7977666449006423,.13518129740053308,.0313477341283922,.2880748288194013,.711835234241873,8993693872564e-17,0,0,.8251046025104602];
/**
* Convert an array of linear-light prophoto-rgb values to CIE D50 XYZ.
* Matrix cannot be expressed in rational form, but is calculated to 64 bit accuracy.
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see see https://github.com/w3c/csswg-drafts/issues/7675
*/function lin_a98rgb_channel(t){const n=t<0?-1:1,_=Math.abs(t);return n*Math.pow(_,563/256)}const M=[573536/994567,263643/1420810,187206/994567,591459/1989134,6239551/9945670,374412/4972835,53769/1989134,351524/4972835,4929758/4972835];
/**
* Convert an array of linear-light a98-rgb values to CIE XYZ
* http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
* has greater numerical precision than section 4.3.5.3 of
* https://www.adobe.com/digitalimag/pdfs/AdobeRGB1998.pdf
* but the values below were calculated from first principles
* from the chromaticity coordinates of R G B W
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
* @see https://www.adobe.com/digitalimag/pdfs/AdobeRGB1998.pdf
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/matrixmaker.html
*/const d=[506752/1228815,87881/245763,12673/70218,87098/409605,175762/245763,12673/175545,7918/409605,87881/737289,1001167/1053270];
/**
* Convert an array of linear-light sRGB values to CIE XYZ
* using sRGB's own white, D65 (no chromatic adaptation)
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function lin_sRGB_to_XYZ(t){return multiplyMatrices(d,t)}
/**
* Convert an array of gamma-corrected sRGB values in the 0.0 to 1.0 range to HSL.
*
* @param {Color} RGB [r, g, b]
* - Red component 0..1
* - Green component 0..1
* - Blue component 0..1
* @return {number[]} Array of HSL values: Hue as degrees 0..360, Saturation and Lightness as percentages 0..100
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/utilities.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/better-rgbToHsl.js
*/function sRGB_to_HSL(t){const n=t[0],_=t[1],o=t[2],e=Math.max(n,_,o),r=Math.min(n,_,o),a=(r+e)/2,i=e-r;let l=Number.NaN,s=0;if(0!==Math.round(1e5*i)){const t=Math.round(1e5*a);switch(s=0===t||1e5===t?0:(e-a)/Math.min(a,1-a),e){case n:l=(_-o)/i+(_<o?6:0);break;case _:l=(o-n)/i+2;break;case o:l=(n-_)/i+4}l*=60}return s<0&&(l+=180,s=Math.abs(s)),l>=360&&(l-=360),[l,100*s,100*a]}function sRGB_to_Hue(t){const n=t[0],_=t[1],o=t[2],e=Math.max(n,_,o),r=Math.min(n,_,o);let a=Number.NaN;const i=e-r;if(0!==i){switch(e){case n:a=(_-o)/i+(_<o?6:0);break;case _:a=(o-n)/i+2;break;case o:a=(n-_)/i+4}a*=60}return a>=360&&(a-=360),a}function inGamut(t){return t[0]>=-1e-4&&t[0]<=1.0001&&t[1]>=-1e-4&&t[1]<=1.0001&&t[2]>=-1e-4&&t[2]<=1.0001}function clip(t){return[t[0]<0?0:t[0]>1?1:t[0],t[1]<0?0:t[1]>1?1:t[1],t[2]<0?0:t[2]>1?1:t[2]]}
/**
* @description Calculate deltaE OK which is the simple root sum of squares
* @param {number[]} reference - Array of OKLab values: L as 0..1, a and b as -1..1
* @param {number[]} sample - Array of OKLab values: L as 0..1, a and b as -1..1
* @return {number} How different a color sample is from reference
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/deltaEOK.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/deltaEOK.js
*/function deltaEOK(t,n){const[_,o,e]=t,[r,a,i]=n,l=_-r,s=o-a,c=e-i;return Math.sqrt(l**2+s**2+c**2)}const B=.02,G=1e-4;function rayTraceBox(t,n){let _=1/0,o=-1/0;const e=[0,0,0];for(let r=0;r<3;r++){const a=t[r],i=n[r]-a;e[r]=i;const l=0,s=1;if(i){const t=1/i,n=(l-a)*t,e=(s-a)*t;o=Math.max(Math.min(n,e),o),_=Math.min(Math.max(n,e),_)}else if(a<l||a>s)return!1}return!(o>_||_<0)&&(o<0&&(o=_),!!isFinite(o)&&[t[0]+e[0]*o,t[1]+e[1]*o,t[2]+e[2]*o])}function luminance(t){const[n,_,o]=t.map((t=>t<=.03928?t/12.92:Math.pow((t+.055)/1.055,2.4)));return.2126*n+.7152*_+.0722*o}exports.HSL_to_XYZ_D50=function HSL_to_XYZ_D50(t){let n=t;return n=HSL_to_sRGB(n),n=lin_sRGB(n),n=lin_sRGB_to_XYZ(n),n=D65_to_D50(n),n},exports.HWB_to_XYZ_D50=function HWB_to_XYZ_D50(t){let n=t;return n=HWB_to_sRGB(n),n=lin_sRGB(n),n=lin_sRGB_to_XYZ(n),n=D65_to_D50(n),n},exports.LCH_to_XYZ_D50=function LCH_to_XYZ_D50(t){let n=t;return n=LCH_to_Lab(n),n=Lab_to_XYZ(n),n},exports.Lab_to_XYZ_D50=function Lab_to_XYZ_D50(t){let n=t;return n=Lab_to_XYZ(n),n},exports.OKLCH_to_OKLab=OKLCH_to_OKLab,exports.OKLCH_to_XYZ_D50=function OKLCH_to_XYZ_D50(t){let n=t;return n=OKLCH_to_OKLab(n),n=OKLab_to_XYZ(n),n=D65_to_D50(n),n},exports.OKLab_to_OKLCH=OKLab_to_OKLCH,exports.OKLab_to_XYZ=OKLab_to_XYZ,exports.OKLab_to_XYZ_D50=function OKLab_to_XYZ_D50(t){let n=t;return n=OKLab_to_XYZ(n),n=D65_to_D50(n),n},exports.P3_to_XYZ_D50=function P3_to_XYZ_D50(t){let n=t;return n=lin_P3(n),n=lin_P3_to_XYZ(n),n=D65_to_D50(n),n},exports.ProPhoto_RGB_to_XYZ_D50=function ProPhoto_RGB_to_XYZ_D50(t){let n=t;var _;return n=[lin_ProPhoto_channel((_=n)[0]),lin_ProPhoto_channel(_[1]),lin_ProPhoto_channel(_[2])],n=multiplyMatrices(L,n),n},exports.XYZ_D50_to_HSL=function XYZ_D50_to_HSL(t){let n=t;return n=D50_to_D65(n),n=XYZ_to_lin_sRGB(n),n=gam_sRGB(n),n=sRGB_to_HSL(n),n},exports.XYZ_D50_to_HWB=function XYZ_D50_to_HWB(t){let n=t;n=D50_to_D65(n),n=XYZ_to_lin_sRGB(n);const _=gam_sRGB(n),o=Math.min(_[0],_[1],_[2]),e=1-Math.max(_[0],_[1],_[2]);return[sRGB_to_Hue(_),100*o,100*e]},exports.XYZ_D50_to_LCH=function XYZ_D50_to_LCH(t){let n=t;return n=XYZ_to_Lab(n),n=Lab_to_LCH(n),n},exports.XYZ_D50_to_Lab=function XYZ_D50_to_Lab(t){let n=t;return n=XYZ_to_Lab(n),n},exports.XYZ_D50_to_OKLCH=function XYZ_D50_to_OKLCH(t){let n=t;return n=D50_to_D65(n),n=XYZ_to_OKLab(n),n=OKLab_to_OKLCH(n),n},exports.XYZ_D50_to_OKLab=function XYZ_D50_to_OKLab(t){let n=t;return n=D50_to_D65(n),n=XYZ_to_OKLab(n),n},exports.XYZ_D50_to_P3=function XYZ_D50_to_P3(t){let n=t;return n=D50_to_D65(n),n=XYZ_to_lin_P3(n),n=gam_P3(n),n},exports.XYZ_D50_to_ProPhoto=function XYZ_D50_to_ProPhoto(t){let n=t;var _;return n=multiplyMatrices(u,n),n=[gam_ProPhoto_channel((_=n)[0]),gam_ProPhoto_channel(_[1]),gam_ProPhoto_channel(_[2])],n},exports.XYZ_D50_to_XYZ_D50=function XYZ_D50_to_XYZ_D50(t){return t},exports.XYZ_D50_to_XYZ_D65=function XYZ_D50_to_XYZ_D65(t){let n=t;return n=D50_to_D65(n),n},exports.XYZ_D50_to_a98_RGB=function XYZ_D50_to_a98_RGB(t){let n=t;var _;return n=D50_to_D65(n),n=multiplyMatrices(h,n),n=[gam_a98rgb_channel((_=n)[0]),gam_a98rgb_channel(_[1]),gam_a98rgb_channel(_[2])],n},exports.XYZ_D50_to_lin_sRGB=function XYZ_D50_to_lin_sRGB(t){let n=t;return n=D50_to_D65(n),n=XYZ_to_lin_sRGB(n),n},exports.XYZ_D50_to_rec_2020=function XYZ_D50_to_rec_2020(t){let n=t;var _;return n=D50_to_D65(n),n=multiplyMatrices(s,n),n=[gam_2020_channel((_=n)[0]),gam_2020_channel(_[1]),gam_2020_channel(_[2])],n},exports.XYZ_D50_to_sRGB=function XYZ_D50_to_sRGB(t){let n=t;return n=D50_to_D65(n),n=XYZ_to_lin_sRGB(n),n=gam_sRGB(n),n},exports.XYZ_D65_to_XYZ_D50=function XYZ_D65_to_XYZ_D50(t){let n=t;return n=D65_to_D50(n),n},exports.XYZ_to_OKLab=XYZ_to_OKLab,exports.XYZ_to_lin_P3=XYZ_to_lin_P3,exports.XYZ_to_lin_sRGB=XYZ_to_lin_sRGB,exports.a98_RGB_to_XYZ_D50=function a98_RGB_to_XYZ_D50(t){let n=t;
/**
* Convert an array of a98-rgb values in the range 0.0 - 1.0
* to linear light (un-companded) form. Negative values are also now accepted
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/
var _;return n=[lin_a98rgb_channel((_=n)[0]),lin_a98rgb_channel(_[1]),lin_a98rgb_channel(_[2])],n=multiplyMatrices(M,n),n=D65_to_D50(n),n},exports.clip=clip,exports.contrast_ratio_wcag_2_1=function contrast_ratio_wcag_2_1(t,n){const _=luminance(t),o=luminance(n);return(Math.max(_,o)+.05)/(Math.min(_,o)+.05)},exports.gam_P3=gam_P3,exports.gam_sRGB=gam_sRGB,exports.inGamut=inGamut,exports.lin_P3=lin_P3,exports.lin_P3_to_XYZ=lin_P3_to_XYZ,exports.lin_sRGB=lin_sRGB,exports.lin_sRGB_to_XYZ=lin_sRGB_to_XYZ,exports.lin_sRGB_to_XYZ_D50=function lin_sRGB_to_XYZ_D50(t){let n=t;return n=lin_sRGB_to_XYZ(n),n=D65_to_D50(n),n},exports.mapGamut=function mapGamut(t,n,_){const o=t;let e=clip(n(o)),r=deltaEOK(OKLCH_to_OKLab(_(e)),OKLCH_to_OKLab(o));if(r<B)return e;let a=0,i=o[1],l=!0;for(;i-a>G;){const t=(a+i)/2;if(o[1]=t,l&&inGamut(n(o)))a=t;else if(e=clip(n(o)),r=deltaEOK(OKLCH_to_OKLab(_(e)),OKLCH_to_OKLab(o)),r<B){if(B-r<G)return e;l=!1,a=t}else i=t}return clip(n([...o]))}
/**
* @license MIT https://github.com/facelessuser/coloraide/blob/main/LICENSE.md
*/,exports.mapGamutRayTrace=function mapGamutRayTrace(t,n,_){const o=t[0],e=t[2];let r=n(t);const a=n([o,0,e]);for(let t=0;t<4;t++){if(t>0){const t=_(r);t[0]=o,t[2]=e,r=n(t)}const i=rayTraceBox(a,r);if(!i)break;r=i}return clip(r)},exports.namedColors={aliceblue:[240,248,255],antiquewhite:[250,235,215],aqua:[0,255,255],aquamarine:[127,255,212],azure:[240,255,255],beige:[245,245,220],bisque:[255,228,196],black:[0,0,0],blanchedalmond:[255,235,205],blue:[0,0,255],blueviolet:[138,43,226],brown:[165,42,42],burlywood:[222,184,135],cadetblue:[95,158,160],chartreuse:[127,255,0],chocolate:[210,105,30],coral:[255,127,80],cornflowerblue:[100,149,237],cornsilk:[255,248,220],crimson:[220,20,60],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgoldenrod:[184,134,11],darkgray:[169,169,169],darkgreen:[0,100,0],darkgrey:[169,169,169],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkseagreen:[143,188,143],darkslateblue:[72,61,139],darkslategray:[47,79,79],darkslategrey:[47,79,79],darkturquoise:[0,206,209],darkviolet:[148,0,211],deeppink:[255,20,147],deepskyblue:[0,191,255],dimgray:[105,105,105],dimgrey:[105,105,105],dodgerblue:[30,144,255],firebrick:[178,34,34],floralwhite:[255,250,240],forestgreen:[34,139,34],fuchsia:[255,0,255],gainsboro:[220,220,220],ghostwhite:[248,248,255],gold:[255,215,0],goldenrod:[218,165,32],gray:[128,128,128],green:[0,128,0],greenyellow:[173,255,47],grey:[128,128,128],honeydew:[240,255,240],hotpink:[255,105,180],indianred:[205,92,92],indigo:[75,0,130],ivory:[255,255,240],khaki:[240,230,140],lavender:[230,230,250],lavenderblush:[255,240,245],lawngreen:[124,252,0],lemonchiffon:[255,250,205],lightblue:[173,216,230],lightcoral:[240,128,128],lightcyan:[224,255,255],lightgoldenrodyellow:[250,250,210],lightgray:[211,211,211],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightsalmon:[255,160,122],lightseagreen:[32,178,170],lightskyblue:[135,206,250],lightslategray:[119,136,153],lightslategrey:[119,136,153],lightsteelblue:[176,196,222],lightyellow:[255,255,224],lime:[0,255,0],limegreen:[50,205,50],linen:[250,240,230],magenta:[255,0,255],maroon:[128,0,0],mediumaquamarine:[102,205,170],mediumblue:[0,0,205],mediumorchid:[186,85,211],mediumpurple:[147,112,219],mediumseagreen:[60,179,113],mediumslateblue:[123,104,238],mediumspringgreen:[0,250,154],mediumturquoise:[72,209,204],mediumvioletred:[199,21,133],midnightblue:[25,25,112],mintcream:[245,255,250],mistyrose:[255,228,225],moccasin:[255,228,181],navajowhite:[255,222,173],navy:[0,0,128],oldlace:[253,245,230],olive:[128,128,0],olivedrab:[107,142,35],orange:[255,165,0],orangered:[255,69,0],orchid:[218,112,214],palegoldenrod:[238,232,170],palegreen:[152,251,152],paleturquoise:[175,238,238],palevioletred:[219,112,147],papayawhip:[255,239,213],peachpuff:[255,218,185],peru:[205,133,63],pink:[255,192,203],plum:[221,160,221],powderblue:[176,224,230],purple:[128,0,128],rebeccapurple:[102,51,153],red:[255,0,0],rosybrown:[188,143,143],royalblue:[65,105,225],saddlebrown:[139,69,19],salmon:[250,128,114],sandybrown:[244,164,96],seagreen:[46,139,87],seashell:[255,245,238],sienna:[160,82,45],silver:[192,192,192],skyblue:[135,206,235],slateblue:[106,90,205],slategray:[112,128,144],slategrey:[112,128,144],snow:[255,250,250],springgreen:[0,255,127],steelblue:[70,130,180],tan:[210,180,140],teal:[0,128,128],thistle:[216,191,216],tomato:[255,99,71],turquoise:[64,224,208],violet:[238,130,238],wheat:[245,222,179],white:[255,255,255],whitesmoke:[245,245,245],yellow:[255,255,0],yellowgreen:[154,205,50]},exports.rec_2020_to_XYZ_D50=function rec_2020_to_XYZ_D50(t){let n=t;var _;return n=[lin_2020_channel((_=n)[0]),lin_2020_channel(_[1]),lin_2020_channel(_[2])],n=multiplyMatrices(Y,n),n=D65_to_D50(n),n},exports.sRGB_to_XYZ_D50=function sRGB_to_XYZ_D50(t){let n=t;return n=lin_sRGB(n),n=lin_sRGB_to_XYZ(n),n=D65_to_D50(n),n};

View File

@@ -0,0 +1,408 @@
/**
* @param {Color} color [r, g, b]
* - Red as number 0..1;
* - Green as number 0..1;
* - Blue as number 0..1;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function a98_RGB_to_XYZ_D50(x: Color): Color;
export declare function clip(color: Color): Color;
export declare type Color = [number, number, number];
/**
* WCAG 2.1 contrast ratio
*/
export declare function contrast_ratio_wcag_2_1(color1: Color, color2: Color): number;
/**
* Convert an array of linear-light display-p3 RGB in the range 0.0-1.0
* to gamma corrected form
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/
export declare function gam_P3(RGB: Color): Color;
/**
* Convert an array of linear-light sRGB values in the range 0.0-1.0 to gamma corrected form
* Extended transfer function:
* For negative values, linear portion extends on reflection
* of axis, then uses reflected pow below that
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://en.wikipedia.org/wiki/SRGB
*/
export declare function gam_sRGB(RGB: Color): Color;
/**
* @param {Color} color [h, s, l]
* - Hue as degrees 0..360;
* - Saturation as number 0..100;
* - Lightness as number 0..100;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function HSL_to_XYZ_D50(x: Color): Color;
/**
* @param {Color} color [h, w, b]
* - Hue as degrees 0..360;
* - Whiteness as number 0..100;
* - Blackness as number 0..100;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function HWB_to_XYZ_D50(x: Color): Color;
export declare function inGamut(x: Color): boolean;
/**
* @param {Color} color [l, a, b]
* - Lightness as number 0..100;
* - a as number -160..160;
* - b as number -160..160;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function Lab_to_XYZ_D50(x: Color): Color;
/**
* @param {Color} color [l, c, h]
* - Lightness as number 0..100;
* - Chroma as number 0..230;
* - Hue as degrees 0..360;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function LCH_to_XYZ_D50(x: Color): Color;
/**
* Convert an array of display-p3 RGB values in the range 0.0 - 1.0
* to linear light (un-companded) form.
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/
export declare function lin_P3(RGB: Color): Color;
/**
* Convert an array of linear-light display-p3 values to CIE XYZ
* using D65 (no chromatic adaptation)
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/
export declare function lin_P3_to_XYZ(rgb: Color): Color;
/**
* Convert an array of of sRGB values where in-gamut values are in the range
* [0 - 1] to linear light (un-companded) form.
* Extended transfer function:
* For negative values, linear portion is extended on reflection of axis,
* then reflected power function is used.
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://en.wikipedia.org/wiki/SRGB
*/
export declare function lin_sRGB(RGB: Color): Color;
/**
* Convert an array of linear-light sRGB values to CIE XYZ
* using sRGB's own white, D65 (no chromatic adaptation)
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/
export declare function lin_sRGB_to_XYZ(rgb: Color): Color;
/**
* @param {Color} color [r, g, b]
* - Red as number 0..1;
* - Green as number 0..1;
* - Blue as number 0..1;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function lin_sRGB_to_XYZ_D50(x: Color): Color;
export declare function mapGamut(startOKLCH: Color, toDestination: (x: Color) => Color, fromDestination: (x: Color) => Color): Color;
/**
* @license MIT https://github.com/facelessuser/coloraide/blob/main/LICENSE.md
*/
export declare function mapGamutRayTrace(startOKLCH: Color, toLinear: (x: Color) => Color, fromLinear: (x: Color) => Color): Color;
export declare const namedColors: Record<string, Color>;
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js
*/
export declare function OKLab_to_OKLCH(OKLab: Color): Color;
/**
* Given OKLab, convert to XYZ relative to D65
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js
*/
export declare function OKLab_to_XYZ(OKLab: Color): Color;
/**
* @param {Color} color [l, a, b]
* - Lightness as number 0..1;
* - a as number 0..0.5;
* - b as number 0..0.5;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function OKLab_to_XYZ_D50(x: Color): Color;
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js
*/
export declare function OKLCH_to_OKLab(OKLCH: Color): Color;
/**
* @param {Color} color [l, c, h]
* - Lightness as number 0..1;
* - Chroma as number 0..0.5;
* - Hue as degrees 0..360;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function OKLCH_to_XYZ_D50(x: Color): Color;
/**
* @param {Color} color [r, g, b]
* - Red as number 0..1;
* - Green as number 0..1;
* - Blue as number 0..1;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function P3_to_XYZ_D50(x: Color): Color;
/**
* @param {Color} color [r, g, b]
* - Red as number 0..1;
* - Green as number 0..1;
* - Blue as number 0..1;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function ProPhoto_RGB_to_XYZ_D50(x: Color): Color;
/**
* @param {Color} color [r, g, b]
* - Red as number 0..1;
* - Green as number 0..1;
* - Blue as number 0..1;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function rec_2020_to_XYZ_D50(x: Color): Color;
/**
* @param {Color} color [r, g, b]
* - Red as number 0..1;
* - Green as number 0..1;
* - Blue as number 0..1;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function sRGB_to_XYZ_D50(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} a98 sRGB [r, g, b]
* - Red as number 0..1;
* - Green as number 0..1;
* - Blue as number 0..1;
*/
export declare function XYZ_D50_to_a98_RGB(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} HSL [r, g, b]
* - Hue as degrees 0..360;
* - Saturation as number 0..100;
* - Lightness as number 0..100;
*/
export declare function XYZ_D50_to_HSL(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} HWB [r, g, b]
* - Hue as degrees 0..360;
* - Whiteness as number 0..100;
* - Blackness as number 0..100;
*/
export declare function XYZ_D50_to_HWB(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} Lab [r, g, b]
* - Lightness as number 0..100;
* - a as number -160..160;
* - b as number -160..160;
*/
export declare function XYZ_D50_to_Lab(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} LCH [r, g, b]
* - Lightness as number 0..100;
* - Chroma as number 0..230;
* - Hue as degrees 0..360;
*/
export declare function XYZ_D50_to_LCH(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} linear sRGB [r, g, b]
* - Red as number 0..1;
* - Green as number 0..1;
* - Blue as number 0..1;
*/
export declare function XYZ_D50_to_lin_sRGB(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} OKLab [r, g, b]
* - Lightness as number 0..1;
* - a as number 0..0.5;
* - b as number 0..0.5;
*/
export declare function XYZ_D50_to_OKLab(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} OKLCH [r, g, b]
* - Lightness as number 0..1;
* - Chroma as number 0..0.5;
* - Hue as degrees 0..360;
*/
export declare function XYZ_D50_to_OKLCH(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} P3 [r, g, b]
* - R as number 0..1;
* - G as number 0..1;
* - B as number 0..1;
*/
export declare function XYZ_D50_to_P3(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} ProPhoto [r, g, b]
* - Red as number 0..1;
* - Green as number 0..1;
* - Blue as number 0..1;
*/
export declare function XYZ_D50_to_ProPhoto(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} rec 2020 [r, g, b]
* - Red as number 0..1;
* - Green as number 0..1;
* - Blue as number 0..1;
*/
export declare function XYZ_D50_to_rec_2020(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} sRGB [r, g, b]
* - Red as number 0..1;
* - Green as number 0..1;
* - Blue as number 0..1;
*/
export declare function XYZ_D50_to_sRGB(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function XYZ_D50_to_XYZ_D50(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} D65 XYZ [x, y, z]
*/
export declare function XYZ_D50_to_XYZ_D65(x: Color): Color;
/**
* @param {Color} color [x, y, z]
* - X as number 0..1;
* - Y as number 0..1;
* - Z as number 0..1;
* @return {Color} D50 XYZ [x, y, z]
*/
export declare function XYZ_D65_to_XYZ_D50(x: Color): Color;
/**
* Convert XYZ to linear-light P3
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/
export declare function XYZ_to_lin_P3(XYZ: Color): Color;
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/
export declare function XYZ_to_lin_sRGB(XYZ: Color): Color;
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*
* XYZ <-> LMS matrices recalculated for consistent reference white
* @see https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-943521484
*/
export declare function XYZ_to_OKLab(XYZ: Color): Color;
export { }

View File

@@ -0,0 +1,263 @@
function multiplyMatrices(t,n){return[t[0]*n[0]+t[1]*n[1]+t[2]*n[2],t[3]*n[0]+t[4]*n[1]+t[5]*n[2],t[6]*n[0]+t[7]*n[1]+t[8]*n[2]]}const t=[.955473421488075,-.02309845494876471,.06325924320057072,-.0283697093338637,1.0099953980813041,.021041441191917323,.012314014864481998,-.020507649298898964,1.330365926242124];
/**
* Bradford chromatic adaptation from D50 to D65
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function D50_to_D65(n){return multiplyMatrices(t,n)}const n=[1.0479297925449969,.022946870601609652,-.05019226628920524,.02962780877005599,.9904344267538799,-.017073799063418826,-.009243040646204504,.015055191490298152,.7518742814281371];
/**
* Bradford chromatic adaptation from D65 to D50
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
*/function D65_to_D50(t){return multiplyMatrices(n,t)}
/**
* @param {number} hue - Hue as degrees 0..360
* @param {number} sat - Saturation as percentage 0..100
* @param {number} light - Lightness as percentage 0..100
* @return {number[]} Array of sRGB components; in-gamut colors in range [0..1]
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/hslToRgb.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/hslToRgb.js
*/function HSL_to_sRGB(t){let n=t[0]%360;const _=t[1]/100,o=t[2]/100;return n<0&&(n+=360),[HSL_to_sRGB_channel(0,n,_,o),HSL_to_sRGB_channel(8,n,_,o),HSL_to_sRGB_channel(4,n,_,o)]}function HSL_to_sRGB_channel(t,n,_,o){const e=(t+n/30)%12;return o-_*Math.min(o,1-o)*Math.max(-1,Math.min(e-3,9-e,1))}
/**
* @param {number} hue - Hue as degrees 0..360
* @param {number} white - Whiteness as percentage 0..100
* @param {number} black - Blackness as percentage 0..100
* @return {number[]} Array of RGB components 0..1
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/hwbToRgb.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/hwbToRgb.js
*/function HWB_to_sRGB(t){const n=t[0],_=t[1]/100,o=t[2]/100;if(_+o>=1){const t=_/(_+o);return[t,t,t]}const e=HSL_to_sRGB([n,100,50]),a=1-_-o;return[e[0]*a+_,e[1]*a+_,e[2]*a+_]}
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function LCH_to_Lab(t){const n=t[2]*Math.PI/180;return[t[0],t[1]*Math.cos(n),t[1]*Math.sin(n)]}
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function Lab_to_LCH(t){const n=180*Math.atan2(t[2],t[1])/Math.PI;return[t[0],Math.sqrt(Math.pow(t[1],2)+Math.pow(t[2],2)),n>=0?n:n+360]}const _=[.3457/.3585,1,.2958/.3585];
/**
* Convert Lab to D50-adapted XYZ
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/function Lab_to_XYZ(t){const n=24389/27,o=216/24389,e=(t[0]+16)/116,a=t[1]/500+e,r=e-t[2]/200;return[(Math.pow(a,3)>o?Math.pow(a,3):(116*a-16)/n)*_[0],(t[0]>8?Math.pow((t[0]+16)/116,3):t[0]/n)*_[1],(Math.pow(r,3)>o?Math.pow(r,3):(116*r-16)/n)*_[2]]}
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js
*/function OKLCH_to_OKLab(t){const n=t[2]*Math.PI/180;return[t[0],t[1]*Math.cos(n),t[1]*Math.sin(n)]}
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js
*/function OKLab_to_OKLCH(t){const n=180*Math.atan2(t[2],t[1])/Math.PI;return[t[0],Math.sqrt(t[1]**2+t[2]**2),n>=0?n:n+360]}const o=[1.2268798758459243,-.5578149944602171,.2813910456659647,-.0405757452148008,1.112286803280317,-.0717110580655164,-.0763729366746601,-.4214933324022432,1.5869240198367816],e=[1,.3963377773761749,.2158037573099136,1,-.1055613458156586,-.0638541728258133,1,-.0894841775298119,-1.2914855480194092];
/**
* Given OKLab, convert to XYZ relative to D65
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js
*/
function OKLab_to_XYZ(t){const n=multiplyMatrices(e,t);return multiplyMatrices(o,[n[0]**3,n[1]**3,n[2]**3])}
/**
* Assuming XYZ is relative to D50, convert to CIE Lab
* from CIE standard, which now defines these as a rational fraction
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function XYZ_to_Lab(t){const n=compute_f(t[0]/_[0]),o=compute_f(t[1]/_[1]);return[116*o-16,500*(n-o),200*(o-compute_f(t[2]/_[2]))]}const a=216/24389,r=24389/27;function compute_f(t){return t>a?Math.cbrt(t):(r*t+16)/116}const l=[.819022437996703,.3619062600528904,-.1288737815209879,.0329836539323885,.9292868615863434,.0361446663506424,.0481771893596242,.2642395317527308,.6335478284694309],i=[.210454268309314,.7936177747023054,-.0040720430116193,1.9779985324311684,-2.42859224204858,.450593709617411,.0259040424655478,.7827717124575296,-.8086757549230774];
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*
* XYZ <-> LMS matrices recalculated for consistent reference white
* @see https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-943521484
*/
function XYZ_to_OKLab(t){const n=multiplyMatrices(l,t);return multiplyMatrices(i,[Math.cbrt(n[0]),Math.cbrt(n[1]),Math.cbrt(n[2])])}const c=[30757411/17917100,-6372589/17917100,-4539589/17917100,-.666684351832489,1.616481236634939,467509/29648200,792561/44930125,-1921689/44930125,.942103121235474];
/**
* Convert XYZ to linear-light rec2020
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/const u=[446124/178915,-333277/357830,-72051/178915,-14852/17905,63121/35810,423/17905,11844/330415,-50337/660830,316169/330415];
/**
* Convert XYZ to linear-light P3
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function XYZ_to_lin_P3(t){return multiplyMatrices(u,t)}const s=[1.3457868816471583,-.25557208737979464,-.05110186497554526,-.5446307051249019,1.5082477428451468,.02052744743642139,0,0,1.2119675456389452];
/**
* Convert D50 XYZ to linear-light prophoto-rgb
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/const h=[1829569/896150,-506331/896150,-308931/896150,-851781/878810,1648619/878810,36519/878810,16779/1248040,-147721/1248040,1266979/1248040];
/**
* Convert XYZ to linear-light a98-rgb
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/const m=[12831/3959,-329/214,-1974/3959,-851781/878810,1648619/878810,36519/878810,705/12673,-2585/12673,705/667];
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function XYZ_to_lin_sRGB(t){return multiplyMatrices(m,t)}
/**
* Convert an array of linear-light rec2020 RGB in the range 0.0-1.0
* to gamma corrected form ITU-R BT.2020-2 p.4
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/const D=1.09929682680944,b=.018053968510807;function gam_2020_channel(t){const n=t<0?-1:1,_=Math.abs(t);return _>b?n*(D*Math.pow(_,.45)-(D-1)):4.5*t}
/**
* Convert an array of linear-light sRGB values in the range 0.0-1.0 to gamma corrected form
* Extended transfer function:
* For negative values, linear portion extends on reflection
* of axis, then uses reflected pow below that
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://en.wikipedia.org/wiki/SRGB
*/function gam_sRGB(t){return[gam_sRGB_channel(t[0]),gam_sRGB_channel(t[1]),gam_sRGB_channel(t[2])]}function gam_sRGB_channel(t){const n=t<0?-1:1,_=Math.abs(t);return _>.0031308?n*(1.055*Math.pow(_,1/2.4)-.055):12.92*t}
/**
* Convert an array of linear-light display-p3 RGB in the range 0.0-1.0
* to gamma corrected form
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function gam_P3(t){return gam_sRGB(t)}
/**
* Convert an array of linear-light prophoto-rgb in the range 0.0-1.0
* to gamma corrected form.
* Transfer curve is gamma 1.8 with a small linear portion.
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/const g=1/512;function gam_ProPhoto_channel(t){const n=t<0?-1:1,_=Math.abs(t);return _>=g?n*Math.pow(_,1/1.8):16*t}
/**
* Convert an array of linear-light a98-rgb in the range 0.0-1.0
* to gamma corrected form. Negative values are also now accepted
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function gam_a98rgb_channel(t){const n=t<0?-1:1,_=Math.abs(t);return n*Math.pow(_,256/563)}
/**
* Convert an array of rec2020 RGB values in the range 0.0 - 1.0
* to linear light (un-companded) form.
* ITU-R BT.2020-2 p.4
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/const f=1.09929682680944,X=.018053968510807;function lin_2020_channel(t){const n=t<0?-1:1,_=Math.abs(t);return _<4.5*X?t/4.5:n*Math.pow((_+f-1)/f,1/.45)}const Y=[63426534/99577255,20160776/139408157,47086771/278816314,26158966/99577255,.677998071518871,8267143/139408157,0,19567812/697040785,1.0609850577107909];
/**
* Convert an array of linear-light rec2020 values to CIE XYZ
* using D65 (no chromatic adaptation)
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/
/**
* Convert an array of of sRGB values where in-gamut values are in the range
* [0 - 1] to linear light (un-companded) form.
* Extended transfer function:
* For negative values, linear portion is extended on reflection of axis,
* then reflected power function is used.
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://en.wikipedia.org/wiki/SRGB
*/
function lin_sRGB(t){return[lin_sRGB_channel(t[0]),lin_sRGB_channel(t[1]),lin_sRGB_channel(t[2])]}function lin_sRGB_channel(t){const n=t<0?-1:1,_=Math.abs(t);return _<=.04045?t/12.92:n*Math.pow((_+.055)/1.055,2.4)}
/**
* Convert an array of display-p3 RGB values in the range 0.0 - 1.0
* to linear light (un-companded) form.
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function lin_P3(t){return lin_sRGB(t)}const Z=[608311/1250200,189793/714400,198249/1000160,35783/156275,247089/357200,198249/2500400,0,32229/714400,5220557/5000800];
/**
* Convert an array of linear-light display-p3 values to CIE XYZ
* using D65 (no chromatic adaptation)
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/function lin_P3_to_XYZ(t){return multiplyMatrices(Z,t)}
/**
* Convert an array of prophoto-rgb values where in-gamut Colors are in the
* range [0.0 - 1.0] to linear light (un-companded) form. Transfer curve is
* gamma 1.8 with a small linear portion. Extended transfer function
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/const M=16/512;function lin_ProPhoto_channel(t){const n=t<0?-1:1,_=Math.abs(t);return _<=M?t/16:n*Math.pow(_,1.8)}const p=[.7977666449006423,.13518129740053308,.0313477341283922,.2880748288194013,.711835234241873,8993693872564e-17,0,0,.8251046025104602];
/**
* Convert an array of linear-light prophoto-rgb values to CIE D50 XYZ.
* Matrix cannot be expressed in rational form, but is calculated to 64 bit accuracy.
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see see https://github.com/w3c/csswg-drafts/issues/7675
*/function lin_a98rgb_channel(t){const n=t<0?-1:1,_=Math.abs(t);return n*Math.pow(_,563/256)}const d=[573536/994567,263643/1420810,187206/994567,591459/1989134,6239551/9945670,374412/4972835,53769/1989134,351524/4972835,4929758/4972835];
/**
* Convert an array of linear-light a98-rgb values to CIE XYZ
* http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
* has greater numerical precision than section 4.3.5.3 of
* https://www.adobe.com/digitalimag/pdfs/AdobeRGB1998.pdf
* but the values below were calculated from first principles
* from the chromaticity coordinates of R G B W
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
* @see https://www.adobe.com/digitalimag/pdfs/AdobeRGB1998.pdf
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/matrixmaker.html
*/const L=[506752/1228815,87881/245763,12673/70218,87098/409605,175762/245763,12673/175545,7918/409605,87881/737289,1001167/1053270];
/**
* Convert an array of linear-light sRGB values to CIE XYZ
* using sRGB's own white, D65 (no chromatic adaptation)
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function lin_sRGB_to_XYZ(t){return multiplyMatrices(L,t)}
/**
* Convert an array of gamma-corrected sRGB values in the 0.0 to 1.0 range to HSL.
*
* @param {Color} RGB [r, g, b]
* - Red component 0..1
* - Green component 0..1
* - Blue component 0..1
* @return {number[]} Array of HSL values: Hue as degrees 0..360, Saturation and Lightness as percentages 0..100
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/utilities.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/better-rgbToHsl.js
*/function sRGB_to_HSL(t){const n=t[0],_=t[1],o=t[2],e=Math.max(n,_,o),a=Math.min(n,_,o),r=(a+e)/2,l=e-a;let i=Number.NaN,c=0;if(0!==Math.round(1e5*l)){const t=Math.round(1e5*r);switch(c=0===t||1e5===t?0:(e-r)/Math.min(r,1-r),e){case n:i=(_-o)/l+(_<o?6:0);break;case _:i=(o-n)/l+2;break;case o:i=(n-_)/l+4}i*=60}return c<0&&(i+=180,c=Math.abs(c)),i>=360&&(i-=360),[i,100*c,100*r]}function sRGB_to_Hue(t){const n=t[0],_=t[1],o=t[2],e=Math.max(n,_,o),a=Math.min(n,_,o);let r=Number.NaN;const l=e-a;if(0!==l){switch(e){case n:r=(_-o)/l+(_<o?6:0);break;case _:r=(o-n)/l+2;break;case o:r=(n-_)/l+4}r*=60}return r>=360&&(r-=360),r}function sRGB_to_XYZ_D50(t){let n=t;return n=lin_sRGB(n),n=lin_sRGB_to_XYZ(n),n=D65_to_D50(n),n}function XYZ_D50_to_sRGB(t){let n=t;return n=D50_to_D65(n),n=XYZ_to_lin_sRGB(n),n=gam_sRGB(n),n}function HSL_to_XYZ_D50(t){let n=t;return n=HSL_to_sRGB(n),n=lin_sRGB(n),n=lin_sRGB_to_XYZ(n),n=D65_to_D50(n),n}function XYZ_D50_to_HSL(t){let n=t;return n=D50_to_D65(n),n=XYZ_to_lin_sRGB(n),n=gam_sRGB(n),n=sRGB_to_HSL(n),n}function HWB_to_XYZ_D50(t){let n=t;return n=HWB_to_sRGB(n),n=lin_sRGB(n),n=lin_sRGB_to_XYZ(n),n=D65_to_D50(n),n}function XYZ_D50_to_HWB(t){let n=t;n=D50_to_D65(n),n=XYZ_to_lin_sRGB(n);const _=gam_sRGB(n),o=Math.min(_[0],_[1],_[2]),e=1-Math.max(_[0],_[1],_[2]);return[sRGB_to_Hue(_),100*o,100*e]}function Lab_to_XYZ_D50(t){let n=t;return n=Lab_to_XYZ(n),n}function XYZ_D50_to_Lab(t){let n=t;return n=XYZ_to_Lab(n),n}function LCH_to_XYZ_D50(t){let n=t;return n=LCH_to_Lab(n),n=Lab_to_XYZ(n),n}function XYZ_D50_to_LCH(t){let n=t;return n=XYZ_to_Lab(n),n=Lab_to_LCH(n),n}function OKLab_to_XYZ_D50(t){let n=t;return n=OKLab_to_XYZ(n),n=D65_to_D50(n),n}function XYZ_D50_to_OKLab(t){let n=t;return n=D50_to_D65(n),n=XYZ_to_OKLab(n),n}function OKLCH_to_XYZ_D50(t){let n=t;return n=OKLCH_to_OKLab(n),n=OKLab_to_XYZ(n),n=D65_to_D50(n),n}function XYZ_D50_to_OKLCH(t){let n=t;return n=D50_to_D65(n),n=XYZ_to_OKLab(n),n=OKLab_to_OKLCH(n),n}function lin_sRGB_to_XYZ_D50(t){let n=t;return n=lin_sRGB_to_XYZ(n),n=D65_to_D50(n),n}function XYZ_D50_to_lin_sRGB(t){let n=t;return n=D50_to_D65(n),n=XYZ_to_lin_sRGB(n),n}function a98_RGB_to_XYZ_D50(t){let n=t;
/**
* Convert an array of a98-rgb values in the range 0.0 - 1.0
* to linear light (un-companded) form. Negative values are also now accepted
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/
var _;return n=[lin_a98rgb_channel((_=n)[0]),lin_a98rgb_channel(_[1]),lin_a98rgb_channel(_[2])],n=multiplyMatrices(d,n),n=D65_to_D50(n),n}function XYZ_D50_to_a98_RGB(t){let n=t;var _;return n=D50_to_D65(n),n=multiplyMatrices(h,n),n=[gam_a98rgb_channel((_=n)[0]),gam_a98rgb_channel(_[1]),gam_a98rgb_channel(_[2])],n}function P3_to_XYZ_D50(t){let n=t;return n=lin_P3(n),n=lin_P3_to_XYZ(n),n=D65_to_D50(n),n}function XYZ_D50_to_P3(t){let n=t;return n=D50_to_D65(n),n=XYZ_to_lin_P3(n),n=gam_P3(n),n}function rec_2020_to_XYZ_D50(t){let n=t;var _;return n=[lin_2020_channel((_=n)[0]),lin_2020_channel(_[1]),lin_2020_channel(_[2])],n=multiplyMatrices(Y,n),n=D65_to_D50(n),n}function XYZ_D50_to_rec_2020(t){let n=t;var _;return n=D50_to_D65(n),n=multiplyMatrices(c,n),n=[gam_2020_channel((_=n)[0]),gam_2020_channel(_[1]),gam_2020_channel(_[2])],n}function ProPhoto_RGB_to_XYZ_D50(t){let n=t;var _;return n=[lin_ProPhoto_channel((_=n)[0]),lin_ProPhoto_channel(_[1]),lin_ProPhoto_channel(_[2])],n=multiplyMatrices(p,n),n}function XYZ_D50_to_ProPhoto(t){let n=t;var _;return n=multiplyMatrices(s,n),n=[gam_ProPhoto_channel((_=n)[0]),gam_ProPhoto_channel(_[1]),gam_ProPhoto_channel(_[2])],n}function XYZ_D65_to_XYZ_D50(t){let n=t;return n=D65_to_D50(n),n}function XYZ_D50_to_XYZ_D65(t){let n=t;return n=D50_to_D65(n),n}function XYZ_D50_to_XYZ_D50(t){return t}function inGamut(t){return t[0]>=-1e-4&&t[0]<=1.0001&&t[1]>=-1e-4&&t[1]<=1.0001&&t[2]>=-1e-4&&t[2]<=1.0001}function clip(t){return[t[0]<0?0:t[0]>1?1:t[0],t[1]<0?0:t[1]>1?1:t[1],t[2]<0?0:t[2]>1?1:t[2]]}
/**
* @description Calculate deltaE OK which is the simple root sum of squares
* @param {number[]} reference - Array of OKLab values: L as 0..1, a and b as -1..1
* @param {number[]} sample - Array of OKLab values: L as 0..1, a and b as -1..1
* @return {number} How different a color sample is from reference
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/deltaEOK.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/deltaEOK.js
*/function deltaEOK(t,n){const[_,o,e]=t,[a,r,l]=n,i=_-a,c=o-r,u=e-l;return Math.sqrt(i**2+c**2+u**2)}const B=.02,G=1e-4;function mapGamut(t,n,_){const o=t;let e=clip(n(o)),a=deltaEOK(OKLCH_to_OKLab(_(e)),OKLCH_to_OKLab(o));if(a<B)return e;let r=0,l=o[1],i=!0;for(;l-r>G;){const t=(r+l)/2;if(o[1]=t,i&&inGamut(n(o)))r=t;else if(e=clip(n(o)),a=deltaEOK(OKLCH_to_OKLab(_(e)),OKLCH_to_OKLab(o)),a<B){if(B-a<G)return e;i=!1,r=t}else l=t}return clip(n([...o]))}
/**
* @license MIT https://github.com/facelessuser/coloraide/blob/main/LICENSE.md
*/function mapGamutRayTrace(t,n,_){const o=t[0],e=t[2];let a=n(t);const r=n([o,0,e]);for(let t=0;t<4;t++){if(t>0){const t=_(a);t[0]=o,t[2]=e,a=n(t)}const l=rayTraceBox(r,a);if(!l)break;a=l}return clip(a)}function rayTraceBox(t,n){let _=1/0,o=-1/0;const e=[0,0,0];for(let a=0;a<3;a++){const r=t[a],l=n[a]-r;e[a]=l;const i=0,c=1;if(l){const t=1/l,n=(i-r)*t,e=(c-r)*t;o=Math.max(Math.min(n,e),o),_=Math.min(Math.max(n,e),_)}else if(r<i||r>c)return!1}return!(o>_||_<0)&&(o<0&&(o=_),!!isFinite(o)&&[t[0]+e[0]*o,t[1]+e[1]*o,t[2]+e[2]*o])}const R={aliceblue:[240,248,255],antiquewhite:[250,235,215],aqua:[0,255,255],aquamarine:[127,255,212],azure:[240,255,255],beige:[245,245,220],bisque:[255,228,196],black:[0,0,0],blanchedalmond:[255,235,205],blue:[0,0,255],blueviolet:[138,43,226],brown:[165,42,42],burlywood:[222,184,135],cadetblue:[95,158,160],chartreuse:[127,255,0],chocolate:[210,105,30],coral:[255,127,80],cornflowerblue:[100,149,237],cornsilk:[255,248,220],crimson:[220,20,60],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgoldenrod:[184,134,11],darkgray:[169,169,169],darkgreen:[0,100,0],darkgrey:[169,169,169],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkseagreen:[143,188,143],darkslateblue:[72,61,139],darkslategray:[47,79,79],darkslategrey:[47,79,79],darkturquoise:[0,206,209],darkviolet:[148,0,211],deeppink:[255,20,147],deepskyblue:[0,191,255],dimgray:[105,105,105],dimgrey:[105,105,105],dodgerblue:[30,144,255],firebrick:[178,34,34],floralwhite:[255,250,240],forestgreen:[34,139,34],fuchsia:[255,0,255],gainsboro:[220,220,220],ghostwhite:[248,248,255],gold:[255,215,0],goldenrod:[218,165,32],gray:[128,128,128],green:[0,128,0],greenyellow:[173,255,47],grey:[128,128,128],honeydew:[240,255,240],hotpink:[255,105,180],indianred:[205,92,92],indigo:[75,0,130],ivory:[255,255,240],khaki:[240,230,140],lavender:[230,230,250],lavenderblush:[255,240,245],lawngreen:[124,252,0],lemonchiffon:[255,250,205],lightblue:[173,216,230],lightcoral:[240,128,128],lightcyan:[224,255,255],lightgoldenrodyellow:[250,250,210],lightgray:[211,211,211],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightsalmon:[255,160,122],lightseagreen:[32,178,170],lightskyblue:[135,206,250],lightslategray:[119,136,153],lightslategrey:[119,136,153],lightsteelblue:[176,196,222],lightyellow:[255,255,224],lime:[0,255,0],limegreen:[50,205,50],linen:[250,240,230],magenta:[255,0,255],maroon:[128,0,0],mediumaquamarine:[102,205,170],mediumblue:[0,0,205],mediumorchid:[186,85,211],mediumpurple:[147,112,219],mediumseagreen:[60,179,113],mediumslateblue:[123,104,238],mediumspringgreen:[0,250,154],mediumturquoise:[72,209,204],mediumvioletred:[199,21,133],midnightblue:[25,25,112],mintcream:[245,255,250],mistyrose:[255,228,225],moccasin:[255,228,181],navajowhite:[255,222,173],navy:[0,0,128],oldlace:[253,245,230],olive:[128,128,0],olivedrab:[107,142,35],orange:[255,165,0],orangered:[255,69,0],orchid:[218,112,214],palegoldenrod:[238,232,170],palegreen:[152,251,152],paleturquoise:[175,238,238],palevioletred:[219,112,147],papayawhip:[255,239,213],peachpuff:[255,218,185],peru:[205,133,63],pink:[255,192,203],plum:[221,160,221],powderblue:[176,224,230],purple:[128,0,128],rebeccapurple:[102,51,153],red:[255,0,0],rosybrown:[188,143,143],royalblue:[65,105,225],saddlebrown:[139,69,19],salmon:[250,128,114],sandybrown:[244,164,96],seagreen:[46,139,87],seashell:[255,245,238],sienna:[160,82,45],silver:[192,192,192],skyblue:[135,206,235],slateblue:[106,90,205],slategray:[112,128,144],slategrey:[112,128,144],snow:[255,250,250],springgreen:[0,255,127],steelblue:[70,130,180],tan:[210,180,140],teal:[0,128,128],thistle:[216,191,216],tomato:[255,99,71],turquoise:[64,224,208],violet:[238,130,238],wheat:[245,222,179],white:[255,255,255],whitesmoke:[245,245,245],yellow:[255,255,0],yellowgreen:[154,205,50]};function luminance(t){const[n,_,o]=t.map((t=>t<=.03928?t/12.92:Math.pow((t+.055)/1.055,2.4)));return.2126*n+.7152*_+.0722*o}function contrast_ratio_wcag_2_1(t,n){const _=luminance(t),o=luminance(n);return(Math.max(_,o)+.05)/(Math.min(_,o)+.05)}export{HSL_to_XYZ_D50,HWB_to_XYZ_D50,LCH_to_XYZ_D50,Lab_to_XYZ_D50,OKLCH_to_OKLab,OKLCH_to_XYZ_D50,OKLab_to_OKLCH,OKLab_to_XYZ,OKLab_to_XYZ_D50,P3_to_XYZ_D50,ProPhoto_RGB_to_XYZ_D50,XYZ_D50_to_HSL,XYZ_D50_to_HWB,XYZ_D50_to_LCH,XYZ_D50_to_Lab,XYZ_D50_to_OKLCH,XYZ_D50_to_OKLab,XYZ_D50_to_P3,XYZ_D50_to_ProPhoto,XYZ_D50_to_XYZ_D50,XYZ_D50_to_XYZ_D65,XYZ_D50_to_a98_RGB,XYZ_D50_to_lin_sRGB,XYZ_D50_to_rec_2020,XYZ_D50_to_sRGB,XYZ_D65_to_XYZ_D50,XYZ_to_OKLab,XYZ_to_lin_P3,XYZ_to_lin_sRGB,a98_RGB_to_XYZ_D50,clip,contrast_ratio_wcag_2_1,gam_P3,gam_sRGB,inGamut,lin_P3,lin_P3_to_XYZ,lin_sRGB,lin_sRGB_to_XYZ,lin_sRGB_to_XYZ_D50,mapGamut,mapGamutRayTrace,R as namedColors,rec_2020_to_XYZ_D50,sRGB_to_XYZ_D50};

View File

@@ -0,0 +1,62 @@
{
"name": "@csstools/color-helpers",
"description": "Color helpers to ease transformation between formats, gamut, etc",
"version": "5.0.2",
"contributors": [
{
"name": "Antonio Laguna",
"email": "antonio@laguna.es",
"url": "https://antonio.laguna.es"
},
{
"name": "Romain Menke",
"email": "romainmenke@gmail.com"
}
],
"license": "MIT-0",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/csstools"
},
{
"type": "opencollective",
"url": "https://opencollective.com/csstools"
}
],
"engines": {
"node": ">=18"
},
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"require": {
"default": "./dist/index.cjs"
}
}
},
"files": [
"CHANGELOG.md",
"LICENSE.md",
"README.md",
"dist"
],
"scripts": {},
"homepage": "https://github.com/csstools/postcss-plugins/tree/main/packages/color-helpers#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/csstools/postcss-plugins.git",
"directory": "packages/color-helpers"
},
"bugs": "https://github.com/csstools/postcss-plugins/issues",
"keywords": [
"colors",
"css"
]
}

10
frontend/node_modules/@csstools/css-calc/CHANGELOG.md generated vendored Normal file
View File

@@ -0,0 +1,10 @@
# Changes to CSS Calc
### 2.1.4
_May 27, 2025_
- Updated [`@csstools/css-tokenizer`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-tokenizer) to [`3.0.4`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-tokenizer/CHANGELOG.md#304) (patch)
- Updated [`@csstools/css-parser-algorithms`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-parser-algorithms) to [`3.0.5`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-parser-algorithms/CHANGELOG.md#305) (patch)
[Full CHANGELOG](https://github.com/csstools/postcss-plugins/tree/main/packages/css-calc/CHANGELOG.md)

20
frontend/node_modules/@csstools/css-calc/LICENSE.md generated vendored Normal file
View File

@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright 2022 Romain Menke, Antonio Laguna <antonio@laguna.es>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

132
frontend/node_modules/@csstools/css-calc/README.md generated vendored Normal file
View File

@@ -0,0 +1,132 @@
# CSS Calc <img src="https://cssdb.org/images/css.svg" alt="for CSS" width="90" height="90" align="right">
[<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/css-calc.svg" height="20">][npm-url]
[<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/actions/workflows/test.yml/badge.svg?branch=main" height="20">][cli-url]
[<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
Implemented from : https://drafts.csswg.org/css-values-4/ on 2023-02-17
## Usage
Add [CSS calc] to your project:
```bash
npm install @csstools/css-calc @csstools/css-parser-algorithms @csstools/css-tokenizer --save-dev
```
### With string values :
```mjs
import { calc } from '@csstools/css-calc';
// '20'
console.log(calc('calc(10 * 2)'));
```
### With component values :
```mjs
import { stringify, tokenizer } from '@csstools/css-tokenizer';
import { parseCommaSeparatedListOfComponentValues } from '@csstools/css-parser-algorithms';
import { calcFromComponentValues } from '@csstools/css-calc';
const t = tokenizer({
css: 'calc(10 * 2)',
});
const tokens = [];
{
while (!t.endOfFile()) {
tokens.push(t.nextToken());
}
tokens.push(t.nextToken()); // EOF-token
}
const result = parseCommaSeparatedListOfComponentValues(tokens, {});
// filter or mutate the component values
const calcResult = calcFromComponentValues(result, { precision: 5, toCanonicalUnits: true });
// filter or mutate the component values even further
const calcResultStr = calcResult.map((componentValues) => {
return componentValues.map((x) => stringify(...x.tokens())).join('');
}).join(',');
// '20'
console.log(calcResultStr);
```
### Options
#### `precision` :
The default precision is fairly high.
It aims to be high enough to make rounding unnoticeable in the browser.
You can set it to a lower number to suit your needs.
```mjs
import { calc } from '@csstools/css-calc';
// '0.3'
console.log(calc('calc(1 / 3)', { precision: 1 }));
// '0.33'
console.log(calc('calc(1 / 3)', { precision: 2 }));
```
#### `globals` :
Pass global values as a map of key value pairs.
> Example : Relative color syntax (`lch(from pink calc(l / 2) c h)`) exposes color channel information as ident tokens.
> By passing globals for `l`, `c` and `h` it is possible to solve nested `calc()`'s.
```mjs
import { calc } from '@csstools/css-calc';
const globals = new Map([
['a', '10px'],
['b', '2rem'],
]);
// '20px'
console.log(calc('calc(a * 2)', { globals: globals }));
// '6rem'
console.log(calc('calc(b * 3)', { globals: globals }));
```
#### `toCanonicalUnits` :
By default this package will try to preserve units.
The heuristic to do this is very simplistic.
We take the first unit we encounter and try to convert other dimensions to that unit.
This better matches what users expect from a CSS dev tool.
If you want to have outputs that are closes to CSS serialized values you can pass `toCanonicalUnits: true`.
```mjs
import { calc } from '@csstools/css-calc';
// '20hz'
console.log(calc('calc(0.01khz + 10hz)', { toCanonicalUnits: true }));
// '20hz'
console.log(calc('calc(10hz + 0.01khz)', { toCanonicalUnits: true }));
// '0.02khz' !!!
console.log(calc('calc(0.01khz + 10hz)', { toCanonicalUnits: false }));
// '20hz'
console.log(calc('calc(10hz + 0.01khz)', { toCanonicalUnits: false }));
```
[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
[discord]: https://discord.gg/bUadyRwkJS
[npm-url]: https://www.npmjs.com/package/@csstools/css-calc
[CSS calc]: https://github.com/csstools/postcss-plugins/tree/main/packages/css-calc

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,71 @@
import type { ComponentValue } from '@csstools/css-parser-algorithms';
import type { TokenDimension } from '@csstools/css-tokenizer';
import type { TokenNumber } from '@csstools/css-tokenizer';
import type { TokenPercentage } from '@csstools/css-tokenizer';
export declare function calc(css: string, options?: conversionOptions): string;
export declare function calcFromComponentValues(componentValuesList: Array<Array<ComponentValue>>, options?: conversionOptions): Array<Array<ComponentValue>>;
export declare type conversionOptions = {
/**
* Pass global values as a map of key value pairs.
*/
globals?: GlobalsWithStrings;
/**
* The default precision is fairly high.
* It aims to be high enough to make rounding unnoticeable in the browser.
* You can set it to a lower number to suite your needs.
*/
precision?: number;
/**
* By default this package will try to preserve units.
* The heuristic to do this is very simplistic.
* We take the first unit we encounter and try to convert other dimensions to that unit.
*
* This better matches what users expect from a CSS dev tool.
*
* If you want to have outputs that are closes to CSS serialized values you can set `true`.
*/
toCanonicalUnits?: boolean;
/**
* Convert NaN, Infinity, ... into standard representable values.
*/
censorIntoStandardRepresentableValues?: boolean;
/**
* Some percentages resolve against other values and might be negative or positive depending on context.
* Raw percentages are more likely to be safe to simplify outside of a browser context
*
* @see https://drafts.csswg.org/css-values-4/#calc-simplification
*/
rawPercentages?: boolean;
/**
* The values used to generate random value cache keys.
*/
randomCaching?: {
/**
* The name of the property the random function is used in.
*/
propertyName: string;
/**
* N is the index of the random function among other random functions in the same property value.
*/
propertyN: number;
/**
* An element ID identifying the element the style is being applied to.
* When omitted any `random()` call will not be computed.
*/
elementID: string;
/**
* A document ID identifying the Document the styles are from.
* When omitted any `random()` call will not be computed.
*/
documentID: string;
};
};
export declare type GlobalsWithStrings = Map<string, TokenDimension | TokenNumber | TokenPercentage | string>;
export declare const mathFunctionNames: Set<string>;
export { }

File diff suppressed because one or more lines are too long

66
frontend/node_modules/@csstools/css-calc/package.json generated vendored Normal file
View File

@@ -0,0 +1,66 @@
{
"name": "@csstools/css-calc",
"description": "Solve CSS math expressions",
"version": "2.1.4",
"contributors": [
{
"name": "Antonio Laguna",
"email": "antonio@laguna.es",
"url": "https://antonio.laguna.es"
},
{
"name": "Romain Menke",
"email": "romainmenke@gmail.com"
}
],
"license": "MIT",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/csstools"
},
{
"type": "opencollective",
"url": "https://opencollective.com/csstools"
}
],
"engines": {
"node": ">=18"
},
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"require": {
"default": "./dist/index.cjs"
}
}
},
"files": [
"CHANGELOG.md",
"LICENSE.md",
"README.md",
"dist"
],
"peerDependencies": {
"@csstools/css-parser-algorithms": "^3.0.5",
"@csstools/css-tokenizer": "^3.0.4"
},
"scripts": {},
"homepage": "https://github.com/csstools/postcss-plugins/tree/main/packages/css-calc#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/csstools/postcss-plugins.git",
"directory": "packages/css-calc"
},
"bugs": "https://github.com/csstools/postcss-plugins/issues",
"keywords": [
"calc",
"css"
]
}

View File

@@ -0,0 +1,11 @@
# Changes to CSS Color Parser
### 3.0.10
_May 27, 2025_
- Updated [`@csstools/css-tokenizer`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-tokenizer) to [`3.0.4`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-tokenizer/CHANGELOG.md#304) (patch)
- Updated [`@csstools/css-parser-algorithms`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-parser-algorithms) to [`3.0.5`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-parser-algorithms/CHANGELOG.md#305) (patch)
- Updated [`@csstools/css-calc`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-calc) to [`2.1.4`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-calc/CHANGELOG.md#214) (patch)
[Full CHANGELOG](https://github.com/csstools/postcss-plugins/tree/main/packages/css-color-parser/CHANGELOG.md)

View File

@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright 2022 Romain Menke, Antonio Laguna <antonio@laguna.es>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,37 @@
# CSS Color Parser <img src="https://cssdb.org/images/css.svg" alt="for CSS" width="90" height="90" align="right">
[<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/css-color-parser.svg" height="20">][npm-url]
[<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/actions/workflows/test.yml/badge.svg?branch=main" height="20">][cli-url]
[<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
## Usage
Add [CSS Color Parser] to your project:
```bash
npm install @csstools/css-color-parser @csstools/css-parser-algorithms @csstools/css-tokenizer --save-dev
```
```ts
import { color } from '@csstools/css-color-parser';
import { isFunctionNode, parseComponentValue } from '@csstools/css-parser-algorithms';
import { serializeRGB } from '@csstools/css-color-parser';
import { tokenize } from '@csstools/css-tokenizer';
// color() expects a parsed component value.
const hwbComponentValue = parseComponentValue(tokenize({ css: 'hwb(10deg 10% 20%)' }));
const colorData = color(hwbComponentValue);
if (colorData) {
console.log(colorData);
// serializeRGB() returns a component value.
const rgbComponentValue = serializeRGB(colorData);
console.log(rgbComponentValue.toString());
}
```
[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
[discord]: https://discord.gg/bUadyRwkJS
[npm-url]: https://www.npmjs.com/package/@csstools/css-color-parser
[CSS Color Parser]: https://github.com/csstools/postcss-plugins/tree/main/packages/css-color-parser

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,164 @@
import type { Color } from '@csstools/color-helpers';
import type { ComponentValue } from '@csstools/css-parser-algorithms';
import { FunctionNode } from '@csstools/css-parser-algorithms';
/**
* Convert a color function to a `ColorData` object.
*
* @param {ComponentValue} colorNode - The color function to be converted.
* @returns {ColorData|false} The color function as a `ColorData` object or `false` if it could not be converted.
*/
export declare function color(colorNode: ComponentValue): ColorData | false;
/**
* A color data object.
* It contains as much information as possible about the color and the original parsed syntax.
*/
export declare interface ColorData {
/**
* The color notation of the color data.
*
* We use "color notation" and not "color space" because these represent the original notation and not the actual color space.
* The actual color space is however always implied by the color notation.
*/
colorNotation: ColorNotation;
/**
* The color channels.
* This is always an array of three numbers
* but the channels can only be interpreted by looking at the color notation.
*/
channels: Color;
/**
* The alpha channel.
* This is either a number between `0` and `1` or a `ComponentValue` object.
*
* Since most computations are not dependent on the alpha channel,
* we allow things like `var(--some-alpha)` as an alpha channel value for most inputs.
*/
alpha: number | ComponentValue;
/**
* Information about the original syntax.
*/
syntaxFlags: Set<SyntaxFlag>;
}
/**
* Check if a color data object fits the `display-p3` gamut.
*
* @param {ColorData} x - The color data to be checked.
* @returns {boolean} Whether the color data fits the `display-p3` gamut.
*/
export declare function colorDataFitsDisplayP3_Gamut(x: ColorData): boolean;
/**
* Check if a color data object fits the `sRGB` gamut.
*
* @param {ColorData} x - The color data to be checked.
* @returns {boolean} Whether the color data fits the `sRGB` gamut.
*/
export declare function colorDataFitsRGB_Gamut(x: ColorData): boolean;
export declare enum ColorNotation {
/** Adobe 1999, expressed through `color(a98-rgb 0 0 0)` */
A98_RGB = "a98-rgb",
/** Display P3, expressed through `color(display-p3 0 0 0)` */
Display_P3 = "display-p3",
/** Hex, expressed through `#000` */
HEX = "hex",
/** HSL, expressed through `hsl(0 0% 0%)` */
HSL = "hsl",
/** HWB, expressed through `hwb(0 0% 0%)` */
HWB = "hwb",
/** LCH, expressed through `lch(0 0% 0deg)` */
LCH = "lch",
/** Lab, expressed through `lab(0 0 0)` */
Lab = "lab",
/** Linear sRGB, expressed through `color(linear-srgb 0 0 0)` */
Linear_sRGB = "srgb-linear",
/** Oklch, expressed through `oklch(0 0% 0deg)` */
OKLCH = "oklch",
/** Oklab, expressed through `oklab(0 0 0)` */
OKLab = "oklab",
/** ProPhoto RGB, expressed through `color(prophoto-rgb 0 0 0)` */
ProPhoto_RGB = "prophoto-rgb",
/** RGB, expressed through `rgb(0 0 0)` */
RGB = "rgb",
/** sRGB, expressed through `color(srgb 0 0 0)` */
sRGB = "srgb",
/** Rec. 2020, expressed through `color(rec2020 0 0 0)` */
Rec2020 = "rec2020",
/** XYZ, expressed through `color(xyz-d50 0 0 0)` */
XYZ_D50 = "xyz-d50",
/** XYZ, expressed through `color(xyz-d65 0 0 0)` */
XYZ_D65 = "xyz-d65"
}
export declare function serializeHSL(color: ColorData, gamutMapping?: boolean): FunctionNode;
/**
* Convert color data to component values in the OKLCH color space.
* The return value can be converted to a string by calling `toString()` on it.
*
* @param {ColorData} color - The color data to be serialized.
* @returns {FunctionNode} The serialized color data as a FunctionNode object.
*/
export declare function serializeOKLCH(color: ColorData): FunctionNode;
/**
* Convert color data to component values in the display-p3 color space.
* The return value can be converted to a string by calling `toString()` on it.
*
* @param {ColorData} color - The color data to be serialized.
* @param {boolean} gamutMapping - Whether to perform gamut mapping, defaults to `true`.
* @returns {FunctionNode} The serialized color data as a FunctionNode object.
*/
export declare function serializeP3(color: ColorData, gamutMapping?: boolean): FunctionNode;
/**
* Convert color data to component values in the srgb color space.
* The return value can be converted to a string by calling `toString()` on it.
*
* @param {ColorData} color - The color data to be serialized.
* @param {boolean} gamutMapping - Whether to perform gamut mapping, defaults to `true`.
* @returns {FunctionNode} The serialized color data as a FunctionNode object.
*/
export declare function serializeRGB(color: ColorData, gamutMapping?: boolean): FunctionNode;
export declare enum SyntaxFlag {
/** Is a color keyword, e.g. `transparent`, `currentColor`, ... */
ColorKeyword = "color-keyword",
/** Has an explicit alpha channel */
HasAlpha = "has-alpha",
/** Has a channel with a dimension value, e.g. `50deg` */
HasDimensionValues = "has-dimension-values",
/** Has a channel with the `none` keyword */
HasNoneKeywords = "has-none-keywords",
/** Has a channel with a number value */
HasNumberValues = "has-number-values",
/** Has an alpha channel with a percentage value */
HasPercentageAlpha = "has-percentage-alpha",
/** Has a channel with a percentage value */
HasPercentageValues = "has-percentage-values",
/** Has an alpha channel with a `var()` function value */
HasVariableAlpha = "has-variable-alpha",
/** Is Hex notation */
Hex = "hex",
/** Is legacy HSL, e.g. `hsl(50deg, 0%, 0%)` */
LegacyHSL = "legacy-hsl",
/** Is legacy RGB, e.g. `rgb(0, 0, 0)` */
LegacyRGB = "legacy-rgb",
/** Is a named color, e.g. `red`, `blue` */
NamedColor = "named-color",
/** Is a relative color syntax, e.g. `rgb(from purple r g b)` */
RelativeColorSyntax = "relative-color-syntax",
/** Is a mixed color, e.g. `color-mix(in oklch, red, blue)` */
ColorMix = "color-mix",
/** Is a variadic mixed color, e.g. `color-mix(in oklch, red)` `color-mix(in oklch, red, blue, green)` */
ColorMixVariadic = "color-mix-variadic",
/** Is a contrasting color, e.g. `contrast-color()` */
ContrastColor = "contrast-color",
/** Is an experimental color syntax */
Experimental = "experimental"
}
export { }

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,71 @@
{
"name": "@csstools/css-color-parser",
"description": "Parse CSS color values",
"version": "3.0.10",
"contributors": [
{
"name": "Antonio Laguna",
"email": "antonio@laguna.es",
"url": "https://antonio.laguna.es"
},
{
"name": "Romain Menke",
"email": "romainmenke@gmail.com"
}
],
"license": "MIT",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/csstools"
},
{
"type": "opencollective",
"url": "https://opencollective.com/csstools"
}
],
"engines": {
"node": ">=18"
},
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"require": {
"default": "./dist/index.cjs"
}
}
},
"files": [
"CHANGELOG.md",
"LICENSE.md",
"README.md",
"dist"
],
"dependencies": {
"@csstools/color-helpers": "^5.0.2",
"@csstools/css-calc": "^2.1.4"
},
"peerDependencies": {
"@csstools/css-parser-algorithms": "^3.0.5",
"@csstools/css-tokenizer": "^3.0.4"
},
"scripts": {},
"homepage": "https://github.com/csstools/postcss-plugins/tree/main/packages/css-color-parser#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/csstools/postcss-plugins.git",
"directory": "packages/css-color-parser"
},
"bugs": "https://github.com/csstools/postcss-plugins/issues",
"keywords": [
"color",
"css",
"parser"
]
}

View File

@@ -0,0 +1,9 @@
# Changes to CSS Parser Algorithms
### 3.0.5
_May 27, 2025_
- Updated [`@csstools/css-tokenizer`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-tokenizer) to [`3.0.4`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-tokenizer/CHANGELOG.md#304) (patch)
[Full CHANGELOG](https://github.com/csstools/postcss-plugins/tree/main/packages/css-parser-algorithms/CHANGELOG.md)

View File

@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright 2022 Romain Menke, Antonio Laguna <antonio@laguna.es>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,119 @@
# CSS Parser Algorithms <img src="https://cssdb.org/images/css.svg" alt="for CSS" width="90" height="90" align="right">
[<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/css-parser-algorithms.svg" height="20">][npm-url]
[<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/actions/workflows/test.yml/badge.svg?branch=main" height="20">][cli-url]
[<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
Implemented from : https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/
## API
[Read the API docs](./docs/css-parser-algorithms.md)
## Usage
Add [CSS Parser Algorithms] to your project:
```bash
npm install @csstools/css-parser-algorithms @csstools/css-tokenizer --save-dev
```
[CSS Parser Algorithms] only accepts tokenized CSS.
It must be used together with `@csstools/css-tokenizer`.
```js
import { tokenizer, TokenType } from '@csstools/css-tokenizer';
import { parseComponentValue } from '@csstools/css-parser-algorithms';
const myCSS = `@media only screen and (min-width: 768rem) {
.foo {
content: 'Some content!' !important;
}
}
`;
const t = tokenizer({
css: myCSS,
});
const tokens = [];
{
while (!t.endOfFile()) {
tokens.push(t.nextToken());
}
tokens.push(t.nextToken()); // EOF-token
}
const options = {
onParseError: ((err) => {
throw err;
}),
};
const result = parseComponentValue(tokens, options);
console.log(result);
```
### Available functions
- [`parseComponentValue`](https://www.w3.org/TR/css-syntax-3/#parse-component-value)
- [`parseListOfComponentValues`](https://www.w3.org/TR/css-syntax-3/#parse-list-of-component-values)
- [`parseCommaSeparatedListOfComponentValues`](https://www.w3.org/TR/css-syntax-3/#parse-comma-separated-list-of-component-values)
### Utilities
#### `gatherNodeAncestry`
The AST does not expose the entire ancestry of each node.
The walker methods do provide access to the current parent, but also not the entire ancestry.
To gather the entire ancestry for a a given sub tree of the AST you can use `gatherNodeAncestry`.
The result is a `Map` with the child nodes as keys and the parents as values.
This allows you to lookup any ancestor of any node.
```js
import { parseComponentValue } from '@csstools/css-parser-algorithms';
const result = parseComponentValue(tokens, options);
const ancestry = gatherNodeAncestry(result);
```
### Options
```ts
{
onParseError?: (error: ParseError) => void
}
```
#### `onParseError`
The parser algorithms are forgiving and won't stop when a parse error is encountered.
Parse errors also aren't tokens.
To receive parsing error information you can set a callback.
Parser errors will try to inform you about the point in the parsing logic the error happened.
This tells you the kind of error.
## Goals and non-goals
Things this package aims to be:
- specification compliant CSS parser
- a reliable low level package to be used in CSS sub-grammars
What it is not:
- opinionated
- fast
- small
- a replacement for PostCSS (PostCSS is fast and also an ecosystem)
[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
[discord]: https://discord.gg/bUadyRwkJS
[npm-url]: https://www.npmjs.com/package/@csstools/css-parser-algorithms
[CSS Parser Algorithms]: https://github.com/csstools/postcss-plugins/tree/main/packages/css-parser-algorithms

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,604 @@
/**
* Parse CSS following the {@link https://drafts.csswg.org/css-syntax/#parsing | CSS Syntax Level 3 specification}.
*
* @remarks
* The tokenizing and parsing tools provided by CSS Tools are designed to be low level and generic with strong ties to their respective specifications.
*
* Any analysis or mutation of CSS source code should be done with the least powerful tool that can accomplish the task.
* For many applications it is sufficient to work with tokens.
* For others you might need to use {@link https://github.com/csstools/postcss-plugins/tree/main/packages/css-parser-algorithms | @csstools/css-parser-algorithms} or a more specific parser.
*
* The implementation of the AST nodes is kept lightweight and simple.
* Do not expect magic methods, instead assume that arrays and class instances behave like any other JavaScript.
*
* @example
* Parse a string of CSS into a component value:
* ```js
* import { tokenize } from '@csstools/css-tokenizer';
* import { parseComponentValue } from '@csstools/css-parser-algorithms';
*
* const myCSS = `calc(1px * 2)`;
*
* const componentValue = parseComponentValue(tokenize({
* css: myCSS,
* }));
*
* console.log(componentValue);
* ```
*
* @example
* Use the right algorithm for the job.
*
* Algorithms that can parse larger structures (comma-separated lists, ...) can also parse smaller structures.
* However, the opposite is not true.
*
* If your context allows a list of component values, use {@link parseListOfComponentValues}:
* ```js
* import { tokenize } from '@csstools/css-tokenizer';
* import { parseListOfComponentValues } from '@csstools/css-parser-algorithms';
*
* parseListOfComponentValues(tokenize({ css: `10x 20px` }));
* ```
*
* If your context allows a comma-separated list of component values, use {@link parseCommaSeparatedListOfComponentValues}:
* ```js
* import { tokenize } from '@csstools/css-tokenizer';
* import { parseCommaSeparatedListOfComponentValues } from '@csstools/css-parser-algorithms';
*
* parseCommaSeparatedListOfComponentValues(tokenize({ css: `20deg, 50%, 30%` }));
* ```
*
* @example
* Use the stateful walkers to keep track of the context of a given component value.
*
* ```js
* import { tokenize } from '@csstools/css-tokenizer';
* import { parseComponentValue, isSimpleBlockNode } from '@csstools/css-parser-algorithms';
*
* const myCSS = `calc(1px * (5 / 2))`;
*
* const componentValue = parseComponentValue(tokenize({ css: myCSS }));
*
* let state = { inSimpleBlock: false };
* componentValue.walk((entry) => {
* if (isSimpleBlockNode(entry)) {
* entry.state.inSimpleBlock = true;
* return;
* }
*
* if (entry.state.inSimpleBlock) {
* console.log(entry.node.toString()); // `5`, ...
* }
* }, state);
* ```
*
* @packageDocumentation
*/
import type { CSSToken } from '@csstools/css-tokenizer';
import { ParseError } from '@csstools/css-tokenizer';
import type { TokenFunction } from '@csstools/css-tokenizer';
export declare class CommentNode {
/**
* The node type, always `ComponentValueType.Comment`
*/
type: ComponentValueType;
/**
* The comment token.
*/
value: CSSToken;
constructor(value: CSSToken);
/**
* Retrieve the tokens for the current comment.
* This is the inverse of parsing from a list of tokens.
*/
tokens(): Array<CSSToken>;
/**
* Convert the current comment to a string.
* This is not a true serialization.
* It is purely a concatenation of the string representation of the tokens.
*/
toString(): string;
/**
* @internal
*
* A debug helper to convert the current object to a JSON representation.
* This is useful in asserts and to store large ASTs in files.
*/
toJSON(): Record<string, unknown>;
/**
* @internal
*/
isCommentNode(): this is CommentNode;
/**
* @internal
*/
static isCommentNode(x: unknown): x is CommentNode;
}
export declare type ComponentValue = FunctionNode | SimpleBlockNode | WhitespaceNode | CommentNode | TokenNode;
export declare enum ComponentValueType {
Function = "function",
SimpleBlock = "simple-block",
Whitespace = "whitespace",
Comment = "comment",
Token = "token"
}
export declare type ContainerNode = FunctionNode | SimpleBlockNode;
export declare abstract class ContainerNodeBaseClass {
/**
* The contents of the `Function` or `Simple Block`.
* This is a list of component values.
*/
value: Array<ComponentValue>;
/**
* Retrieve the index of the given item in the current node.
* For most node types this will be trivially implemented as `this.value.indexOf(item)`.
*/
indexOf(item: ComponentValue): number | string;
/**
* Retrieve the item at the given index in the current node.
* For most node types this will be trivially implemented as `this.value[index]`.
*/
at(index: number | string): ComponentValue | undefined;
/**
* Iterates over each item in the `value` array of the current node.
*
* @param cb - The callback function to execute for each item.
* The function receives an object containing the current node (`node`), its parent (`parent`),
* and an optional `state` object.
* A second parameter is the index of the current node.
* The function can return `false` to stop the iteration.
*
* @param state - An optional state object that can be used to pass additional information to the callback function.
* The state object is cloned for each iteration. This means that changes to the state object are not reflected in the next iteration.
*
* @returns `false` if the iteration was halted, `undefined` otherwise.
*/
forEach<T extends Record<string, unknown>, U extends ContainerNode>(this: U, cb: (entry: {
node: ComponentValue;
parent: ContainerNode;
state?: T;
}, index: number | string) => boolean | void, state?: T): false | undefined;
/**
* Walks the current node and all its children.
*
* @param cb - The callback function to execute for each item.
* The function receives an object containing the current node (`node`), its parent (`parent`),
* and an optional `state` object.
* A second parameter is the index of the current node.
* The function can return `false` to stop the iteration.
*
* @param state - An optional state object that can be used to pass additional information to the callback function.
* The state object is cloned for each iteration. This means that changes to the state object are not reflected in the next iteration.
* However changes are passed down to child node iterations.
*
* @returns `false` if the iteration was halted, `undefined` otherwise.
*/
walk<T extends Record<string, unknown>, U extends ContainerNode>(this: U, cb: (entry: {
node: ComponentValue;
parent: ContainerNode;
state?: T;
}, index: number | string) => boolean | void, state?: T): false | undefined;
}
/**
* Iterates over each item in a list of component values.
*
* @param cb - The callback function to execute for each item.
* The function receives an object containing the current node (`node`), its parent (`parent`),
* and an optional `state` object.
* A second parameter is the index of the current node.
* The function can return `false` to stop the iteration.
*
* @param state - An optional state object that can be used to pass additional information to the callback function.
* The state object is cloned for each iteration. This means that changes to the state object are not reflected in the next iteration.
*
* @returns `false` if the iteration was halted, `undefined` otherwise.
*/
export declare function forEach<T extends Record<string, unknown>>(componentValues: Array<ComponentValue>, cb: (entry: {
node: ComponentValue;
parent: ContainerNode | {
value: Array<ComponentValue>;
};
state?: T;
}, index: number | string) => boolean | void, state?: T): false | undefined;
/**
* A function node.
*
* @example
* ```js
* const node = parseComponentValue(tokenize('calc(1 + 1)'));
*
* isFunctionNode(node); // true
* node.getName(); // 'calc'
* ```
*/
export declare class FunctionNode extends ContainerNodeBaseClass {
/**
* The node type, always `ComponentValueType.Function`
*/
type: ComponentValueType;
/**
* The token for the name of the function.
*/
name: TokenFunction;
/**
* The token for the closing parenthesis of the function.
* If the function is unclosed, this will be an EOF token.
*/
endToken: CSSToken;
constructor(name: TokenFunction, endToken: CSSToken, value: Array<ComponentValue>);
/**
* Retrieve the name of the current function.
* This is the parsed and unescaped name of the function.
*/
getName(): string;
/**
* Normalize the current function:
* 1. if the "endToken" is EOF, replace with a ")-token"
*/
normalize(): void;
/**
* Retrieve the tokens for the current function.
* This is the inverse of parsing from a list of tokens.
*/
tokens(): Array<CSSToken>;
/**
* Convert the current function to a string.
* This is not a true serialization.
* It is purely a concatenation of the string representation of the tokens.
*/
toString(): string;
/**
* @internal
*
* A debug helper to convert the current object to a JSON representation.
* This is useful in asserts and to store large ASTs in files.
*/
toJSON(): unknown;
/**
* @internal
*/
isFunctionNode(): this is FunctionNode;
/**
* @internal
*/
static isFunctionNode(x: unknown): x is FunctionNode;
}
/**
* AST nodes do not have a `parent` property or method.
* This makes it harder to traverse the AST upwards.
* This function builds a `Map<Child, Parent>` that can be used to lookup ancestors of a node.
*
* @remarks
* There is no magic behind this or the map it returns.
* Mutating the AST will not update the map.
*
* Types are erased and any content of the map has type `unknown`.
* If someone knows a clever way to type this, please let us know.
*
* @example
* ```js
* const ancestry = gatherNodeAncestry(mediaQuery);
* mediaQuery.walk((entry) => {
* const node = entry.node; // directly exposed
* const parent = entry.parent; // directly exposed
* const grandParent: unknown = ancestry.get(parent); // lookup
*
* console.log('node', node);
* console.log('parent', parent);
* console.log('grandParent', grandParent);
* });
* ```
*/
export declare function gatherNodeAncestry(node: {
walk(cb: (entry: {
node: unknown;
parent: unknown;
}, index: number | string) => boolean | void): false | undefined;
}): Map<unknown, unknown>;
/**
* Check if the current object is a `CommentNode`.
* This is a type guard.
*/
export declare function isCommentNode(x: unknown): x is CommentNode;
/**
* Check if the current object is a `FunctionNode`.
* This is a type guard.
*/
export declare function isFunctionNode(x: unknown): x is FunctionNode;
/**
* Check if the current object is a `SimpleBlockNode`.
* This is a type guard.
*/
export declare function isSimpleBlockNode(x: unknown): x is SimpleBlockNode;
/**
* Check if the current object is a `TokenNode`.
* This is a type guard.
*/
export declare function isTokenNode(x: unknown): x is TokenNode;
/**
* Check if the current object is a `WhitespaceNode`.
* This is a type guard.
*/
export declare function isWhitespaceNode(x: unknown): x is WhitespaceNode;
/**
* Check if the current object is a `WhiteSpaceNode` or a `CommentNode`.
* This is a type guard.
*/
export declare function isWhiteSpaceOrCommentNode(x: unknown): x is WhitespaceNode | CommentNode;
/**
* Parse a comma-separated list of component values.
*
* @example
* ```js
* import { tokenize } from '@csstools/css-tokenizer';
* import { parseCommaSeparatedListOfComponentValues } from '@csstools/css-parser-algorithms';
*
* parseCommaSeparatedListOfComponentValues(tokenize({ css: `20deg, 50%, 30%` }));
* ```
*/
export declare function parseCommaSeparatedListOfComponentValues(tokens: Array<CSSToken>, options?: {
onParseError?: (error: ParseError) => void;
}): Array<Array<ComponentValue>>;
/**
* Parse a single component value.
*
* @example
* ```js
* import { tokenize } from '@csstools/css-tokenizer';
* import { parseCommaSeparatedListOfComponentValues } from '@csstools/css-parser-algorithms';
*
* parseCommaSeparatedListOfComponentValues(tokenize({ css: `10px` }));
* parseCommaSeparatedListOfComponentValues(tokenize({ css: `calc((10px + 1x) * 4)` }));
* ```
*/
export declare function parseComponentValue(tokens: Array<CSSToken>, options?: {
onParseError?: (error: ParseError) => void;
}): ComponentValue | undefined;
/**
* Parse a list of component values.
*
* @example
* ```js
* import { tokenize } from '@csstools/css-tokenizer';
* import { parseListOfComponentValues } from '@csstools/css-parser-algorithms';
*
* parseListOfComponentValues(tokenize({ css: `20deg 30%` }));
* ```
*/
export declare function parseListOfComponentValues(tokens: Array<CSSToken>, options?: {
onParseError?: (error: ParseError) => void;
}): Array<ComponentValue>;
/**
* Replace specific component values in a list of component values.
* A helper for the most common and simplistic cases when mutating an AST.
*/
export declare function replaceComponentValues(componentValuesList: Array<Array<ComponentValue>>, replaceWith: (componentValue: ComponentValue) => Array<ComponentValue> | ComponentValue | void): Array<Array<ComponentValue>>;
/**
* A simple block node.
*
* @example
* ```js
* const node = parseComponentValue(tokenize('[foo=bar]'));
*
* isSimpleBlockNode(node); // true
* node.startToken; // [TokenType.OpenSquare, '[', 0, 0, undefined]
* ```
*/
export declare class SimpleBlockNode extends ContainerNodeBaseClass {
/**
* The node type, always `ComponentValueType.SimpleBlock`
*/
type: ComponentValueType;
/**
* The token for the opening token of the block.
*/
startToken: CSSToken;
/**
* The token for the closing token of the block.
* If the block is closed it will be the mirror variant of the `startToken`.
* If the block is unclosed, this will be an EOF token.
*/
endToken: CSSToken;
constructor(startToken: CSSToken, endToken: CSSToken, value: Array<ComponentValue>);
/**
* Normalize the current simple block
* 1. if the "endToken" is EOF, replace with the mirror token of the "startToken"
*/
normalize(): void;
/**
* Retrieve the tokens for the current simple block.
* This is the inverse of parsing from a list of tokens.
*/
tokens(): Array<CSSToken>;
/**
* Convert the current simple block to a string.
* This is not a true serialization.
* It is purely a concatenation of the string representation of the tokens.
*/
toString(): string;
/**
* @internal
*
* A debug helper to convert the current object to a JSON representation.
* This is useful in asserts and to store large ASTs in files.
*/
toJSON(): unknown;
/**
* @internal
*/
isSimpleBlockNode(): this is SimpleBlockNode;
/**
* @internal
*/
static isSimpleBlockNode(x: unknown): x is SimpleBlockNode;
}
/**
* Returns the start and end index of a node in the CSS source string.
*/
export declare function sourceIndices(x: {
tokens(): Array<CSSToken>;
} | Array<{
tokens(): Array<CSSToken>;
}>): [number, number];
/**
* Concatenate the string representation of a collection of component values.
* This is not a proper serializer that will handle escaping and whitespace.
* It only produces valid CSS for token lists that are also valid.
*/
export declare function stringify(componentValueLists: Array<Array<ComponentValue>>): string;
export declare class TokenNode {
/**
* The node type, always `ComponentValueType.Token`
*/
type: ComponentValueType;
/**
* The token.
*/
value: CSSToken;
constructor(value: CSSToken);
/**
* This is the inverse of parsing from a list of tokens.
*/
tokens(): [CSSToken];
/**
* Convert the current token to a string.
* This is not a true serialization.
* It is purely the string representation of token.
*/
toString(): string;
/**
* @internal
*
* A debug helper to convert the current object to a JSON representation.
* This is useful in asserts and to store large ASTs in files.
*/
toJSON(): Record<string, unknown>;
/**
* @internal
*/
isTokenNode(): this is TokenNode;
/**
* @internal
*/
static isTokenNode(x: unknown): x is TokenNode;
}
/**
* Walks each item in a list of component values all of their children.
*
* @param cb - The callback function to execute for each item.
* The function receives an object containing the current node (`node`), its parent (`parent`),
* and an optional `state` object.
* A second parameter is the index of the current node.
* The function can return `false` to stop the iteration.
*
* @param state - An optional state object that can be used to pass additional information to the callback function.
* The state object is cloned for each iteration. This means that changes to the state object are not reflected in the next iteration.
* However changes are passed down to child node iterations.
*
* @returns `false` if the iteration was halted, `undefined` otherwise.
*
* @example
* ```js
* import { tokenize } from '@csstools/css-tokenizer';
* import { parseListOfComponentValues, isSimpleBlockNode } from '@csstools/css-parser-algorithms';
*
* const myCSS = `calc(1px * (5 / 2)) 10px`;
*
* const componentValues = parseListOfComponentValues(tokenize({ css: myCSS }));
*
* let state = { inSimpleBlock: false };
* walk(componentValues, (entry) => {
* if (isSimpleBlockNode(entry)) {
* entry.state.inSimpleBlock = true;
* return;
* }
*
* if (entry.state.inSimpleBlock) {
* console.log(entry.node.toString()); // `5`, ...
* }
* }, state);
* ```
*/
export declare function walk<T extends Record<string, unknown>>(componentValues: Array<ComponentValue>, cb: (entry: {
node: ComponentValue;
parent: ContainerNode | {
value: Array<ComponentValue>;
};
state?: T;
}, index: number | string) => boolean | void, state?: T): false | undefined;
/**
* Generate a function that finds the next element that should be visited when walking an AST.
* Rules :
* 1. the previous iteration is used as a reference, so any checks are relative to the start of the current iteration.
* 2. the next element always appears after the current index.
* 3. the next element always exists in the list.
* 4. replacing an element does not cause the replaced element to be visited.
* 5. removing an element does not cause elements to be skipped.
* 6. an element added later in the list will be visited.
*/
export declare function walkerIndexGenerator<T>(initialList: Array<T>): (list: Array<T>, child: T, index: number) => number;
export declare class WhitespaceNode {
/**
* The node type, always `ComponentValueType.WhiteSpace`
*/
type: ComponentValueType;
/**
* The list of consecutive whitespace tokens.
*/
value: Array<CSSToken>;
constructor(value: Array<CSSToken>);
/**
* Retrieve the tokens for the current whitespace.
* This is the inverse of parsing from a list of tokens.
*/
tokens(): Array<CSSToken>;
/**
* Convert the current whitespace to a string.
* This is not a true serialization.
* It is purely a concatenation of the string representation of the tokens.
*/
toString(): string;
/**
* @internal
*
* A debug helper to convert the current object to a JSON representation.
* This is useful in asserts and to store large ASTs in files.
*/
toJSON(): Record<string, unknown>;
/**
* @internal
*/
isWhitespaceNode(): this is WhitespaceNode;
/**
* @internal
*/
static isWhitespaceNode(x: unknown): x is WhitespaceNode;
}
export { }

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,65 @@
{
"name": "@csstools/css-parser-algorithms",
"description": "Algorithms to help you parse CSS from an array of tokens.",
"version": "3.0.5",
"contributors": [
{
"name": "Antonio Laguna",
"email": "antonio@laguna.es",
"url": "https://antonio.laguna.es"
},
{
"name": "Romain Menke",
"email": "romainmenke@gmail.com"
}
],
"license": "MIT",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/csstools"
},
{
"type": "opencollective",
"url": "https://opencollective.com/csstools"
}
],
"engines": {
"node": ">=18"
},
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"require": {
"default": "./dist/index.cjs"
}
}
},
"files": [
"CHANGELOG.md",
"LICENSE.md",
"README.md",
"dist"
],
"peerDependencies": {
"@csstools/css-tokenizer": "^3.0.4"
},
"scripts": {},
"homepage": "https://github.com/csstools/postcss-plugins/tree/main/packages/css-parser-algorithms#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/csstools/postcss-plugins.git",
"directory": "packages/css-parser-algorithms"
},
"bugs": "https://github.com/csstools/postcss-plugins/issues",
"keywords": [
"css",
"parser"
]
}

View File

@@ -0,0 +1,9 @@
# Changes to CSS Tokenizer
### 3.0.4
_May 27, 2025_
- align serializers with CSSOM
[Full CHANGELOG](https://github.com/csstools/postcss-plugins/tree/main/packages/css-tokenizer/CHANGELOG.md)

View File

@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright 2022 Romain Menke, Antonio Laguna <antonio@laguna.es>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

111
frontend/node_modules/@csstools/css-tokenizer/README.md generated vendored Normal file
View File

@@ -0,0 +1,111 @@
# CSS Tokenizer <img src="https://cssdb.org/images/css.svg" alt="for CSS" width="90" height="90" align="right">
[<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/css-tokenizer.svg" height="20">][npm-url]
[<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/actions/workflows/test.yml/badge.svg?branch=main" height="20">][cli-url]
[<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
Implemented from : https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/
## API
[Read the API docs](./docs/css-tokenizer.md)
## Usage
Add [CSS Tokenizer] to your project:
```bash
npm install @csstools/css-tokenizer --save-dev
```
```js
import { tokenizer, TokenType } from '@csstools/css-tokenizer';
const myCSS = `@media only screen and (min-width: 768rem) {
.foo {
content: 'Some content!' !important;
}
}
`;
const t = tokenizer({
css: myCSS,
});
while (true) {
const token = t.nextToken();
if (token[0] === TokenType.EOF) {
break;
}
console.log(token);
}
```
Or use the `tokenize` helper function:
```js
import { tokenize } from '@csstools/css-tokenizer';
const myCSS = `@media only screen and (min-width: 768rem) {
.foo {
content: 'Some content!' !important;
}
}
`;
const tokens = tokenize({
css: myCSS,
});
console.log(tokens);
```
### Options
```ts
{
onParseError?: (error: ParseError) => void
}
```
#### `onParseError`
The tokenizer is forgiving and won't stop when a parse error is encountered.
To receive parsing error information you can set a callback.
```js
import { tokenizer, TokenType } from '@csstools/css-tokenizer';
const t = tokenizer({
css: '\\',
}, { onParseError: (err) => console.warn(err) });
while (true) {
const token = t.nextToken();
if (token[0] === TokenType.EOF) {
break;
}
}
```
Parser errors will try to inform you where in the tokenizer logic the error happened.
This tells you what kind of error occurred.
## Goals and non-goals
Things this package aims to be:
- specification compliant CSS tokenizer
- a reliable low level package to be used in CSS parsers
What it is not:
- opinionated
- fast
- small
[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
[discord]: https://discord.gg/bUadyRwkJS
[npm-url]: https://www.npmjs.com/package/@csstools/css-tokenizer
[CSS Tokenizer]: https://github.com/csstools/postcss-plugins/tree/main/packages/css-tokenizer

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,593 @@
/**
* Tokenize CSS following the {@link https://drafts.csswg.org/css-syntax/#tokenization | CSS Syntax Level 3 specification}.
*
* @remarks
* The tokenizing and parsing tools provided by CSS Tools are designed to be low level and generic with strong ties to their respective specifications.
*
* Any analysis or mutation of CSS source code should be done with the least powerful tool that can accomplish the task.
* For many applications it is sufficient to work with tokens.
* For others you might need to use {@link https://github.com/csstools/postcss-plugins/tree/main/packages/css-parser-algorithms | @csstools/css-parser-algorithms} or a more specific parser.
*
* @example
* Tokenize a string of CSS into an array of tokens:
* ```js
* import { tokenize } from '@csstools/css-tokenizer';
*
* const myCSS = `@media only screen and (min-width: 768rem) {
* .foo {
* content: 'Some content!' !important;
* }
* }
* `;
*
* const tokens = tokenize({
* css: myCSS,
* });
*
* console.log(tokens);
* ```
*
* @packageDocumentation
*/
/**
* Deep clone a list of tokens.
* Useful for mutations without altering the original list.
*/
export declare function cloneTokens(tokens: Array<CSSToken>): Array<CSSToken>;
/**
* The union of all possible CSS tokens
*/
export declare type CSSToken = TokenAtKeyword | TokenBadString | TokenBadURL | TokenCDC | TokenCDO | TokenColon | TokenComma | TokenComment | TokenDelim | TokenDimension | TokenEOF | TokenFunction | TokenHash | TokenIdent | TokenNumber | TokenPercentage | TokenSemicolon | TokenString | TokenURL | TokenWhitespace | TokenOpenParen | TokenCloseParen | TokenOpenSquare | TokenCloseSquare | TokenOpenCurly | TokenCloseCurly | TokenUnicodeRange;
/**
* The type of hash token
*/
export declare enum HashType {
/**
* The hash token did not start with an ident sequence (e.g. `#-2`)
*/
Unrestricted = "unrestricted",
/**
* The hash token started with an ident sequence (e.g. `#foo`)
* Only hash tokens with the "id" type are valid ID selectors.
*/
ID = "id"
}
/**
* Assert that a given value has the general structure of a CSS token:
* 1. is an array.
* 2. has at least four items.
* 3. has a known token type.
* 4. has a string representation.
* 5. has a start position.
* 6. has an end position.
*/
export declare function isToken(x: any): x is CSSToken;
export declare function isTokenAtKeyword(x?: CSSToken | null): x is TokenAtKeyword;
export declare function isTokenBadString(x?: CSSToken | null): x is TokenBadString;
export declare function isTokenBadURL(x?: CSSToken | null): x is TokenBadURL;
export declare function isTokenCDC(x?: CSSToken | null): x is TokenCDC;
export declare function isTokenCDO(x?: CSSToken | null): x is TokenCDO;
export declare function isTokenCloseCurly(x?: CSSToken | null): x is TokenCloseCurly;
export declare function isTokenCloseParen(x?: CSSToken | null): x is TokenCloseParen;
export declare function isTokenCloseSquare(x?: CSSToken | null): x is TokenCloseSquare;
export declare function isTokenColon(x?: CSSToken | null): x is TokenColon;
export declare function isTokenComma(x?: CSSToken | null): x is TokenComma;
export declare function isTokenComment(x?: CSSToken | null): x is TokenComment;
export declare function isTokenDelim(x?: CSSToken | null): x is TokenDelim;
export declare function isTokenDimension(x?: CSSToken | null): x is TokenDimension;
export declare function isTokenEOF(x?: CSSToken | null): x is TokenEOF;
export declare function isTokenFunction(x?: CSSToken | null): x is TokenFunction;
export declare function isTokenHash(x?: CSSToken | null): x is TokenHash;
export declare function isTokenIdent(x?: CSSToken | null): x is TokenIdent;
export declare function isTokenNumber(x?: CSSToken | null): x is TokenNumber;
/**
* Assert that a token is a numeric token
*/
export declare function isTokenNumeric(x?: CSSToken | null): x is NumericToken;
export declare function isTokenOpenCurly(x?: CSSToken | null): x is TokenOpenCurly;
export declare function isTokenOpenParen(x?: CSSToken | null): x is TokenOpenParen;
export declare function isTokenOpenSquare(x?: CSSToken | null): x is TokenOpenSquare;
export declare function isTokenPercentage(x?: CSSToken | null): x is TokenPercentage;
export declare function isTokenSemicolon(x?: CSSToken | null): x is TokenSemicolon;
export declare function isTokenString(x?: CSSToken | null): x is TokenString;
export declare function isTokenUnicodeRange(x?: CSSToken | null): x is TokenUnicodeRange;
export declare function isTokenURL(x?: CSSToken | null): x is TokenURL;
export declare function isTokenWhitespace(x?: CSSToken | null): x is TokenWhitespace;
/**
* Assert that a token is a whitespace or comment token
*/
export declare function isTokenWhiteSpaceOrComment(x?: CSSToken | null): x is TokenWhitespace | TokenComment;
/**
* Get the mirror variant of a given token
*
* @example
*
* ```js
* const input = [TokenType.OpenParen, '(', 0, 1, undefined];
* const output = mirrorVariant(input);
*
* console.log(output); // [TokenType.CloseParen, ')', -1, -1, undefined]
* ```
*/
export declare function mirrorVariant(token: CSSToken): CSSToken | null;
/**
* Get the mirror variant type of a given token type
*
* @example
*
* ```js
* const input = TokenType.OpenParen;
* const output = mirrorVariantType(input);
*
* console.log(output); // TokenType.CloseParen
* ```
*/
export declare function mirrorVariantType(type: TokenType): TokenType | null;
/**
* Set the ident value and update the string representation.
* This handles escaping.
*/
export declare function mutateIdent(ident: TokenIdent, newValue: string): void;
/**
* Set the unit and update the string representation.
* This handles escaping.
*/
export declare function mutateUnit(ident: TokenDimension, newUnit: string): void;
/**
* The type of number token
* Either `integer` or `number`
*/
export declare enum NumberType {
Integer = "integer",
Number = "number"
}
/**
* The union of all possible CSS tokens that represent a numeric value
*/
export declare type NumericToken = TokenDimension | TokenNumber | TokenPercentage;
/**
* The CSS Tokenizer is forgiving and will never throw on invalid input.
* Any errors are reported through the `onParseError` callback.
*/
export declare class ParseError extends Error {
/** The index of the start character of the current token. */
sourceStart: number;
/** The index of the end character of the current token. */
sourceEnd: number;
/** The parser steps that preceded the error. */
parserState: Array<string>;
constructor(message: string, sourceStart: number, sourceEnd: number, parserState: Array<string>);
}
export declare const ParseErrorMessage: {
UnexpectedNewLineInString: string;
UnexpectedEOFInString: string;
UnexpectedEOFInComment: string;
UnexpectedEOFInURL: string;
UnexpectedEOFInEscapedCodePoint: string;
UnexpectedCharacterInURL: string;
InvalidEscapeSequenceInURL: string;
InvalidEscapeSequenceAfterBackslash: string;
};
export declare class ParseErrorWithToken extends ParseError {
/** The associated token. */
token: CSSToken;
constructor(message: string, sourceStart: number, sourceEnd: number, parserState: Array<string>, token: CSSToken);
}
/**
* Concatenate the string representation of a list of tokens.
* This is not a proper serializer that will handle escaping and whitespace.
* It only produces valid CSS for a token list that is also valid.
*/
export declare function stringify(...tokens: Array<CSSToken>): string;
/**
* The CSS Token interface
*
* @remarks
* CSS Tokens are fully typed and have a strict structure.
* This makes it easier to iterate and analyze a token stream.
*
* The string representation and the parsed value are stored separately for many token types.
* It is always assumed that the string representation will be used when stringifying, while the parsed value should be used when analyzing tokens.
*/
export declare interface Token<T extends TokenType, U> extends Array<T | string | number | U> {
/**
* The type of token
*/
0: T;
/**
* The token representation
*
* @remarks
* This field will be used when stringifying the token.
* Any stored value is assumed to be valid CSS.
*
* You should never use this field when analyzing the token when there is a parsed value available.
* But you must store mutated values here.
*/
1: string;
/**
* Start position of representation
*/
2: number;
/**
* End position of representation
*/
3: number;
/**
* Extra data
*
* @remarks
* This holds the parsed value of each token.
* These values are unescaped, unquoted, converted to numbers, etc.
*
* You should always use this field when analyzing the token.
* But you must not assume that mutating only this field will have any effect.
*/
4: U;
}
export declare interface TokenAtKeyword extends Token<TokenType.AtKeyword, {
/**
* The unescaped at-keyword name without the leading `@`.
*/
value: string;
}> {
}
export declare interface TokenBadString extends Token<TokenType.BadString, undefined> {
}
export declare interface TokenBadURL extends Token<TokenType.BadURL, undefined> {
}
export declare interface TokenCDC extends Token<TokenType.CDC, undefined> {
}
export declare interface TokenCDO extends Token<TokenType.CDO, undefined> {
}
export declare interface TokenCloseCurly extends Token<TokenType.CloseCurly, undefined> {
}
export declare interface TokenCloseParen extends Token<TokenType.CloseParen, undefined> {
}
export declare interface TokenCloseSquare extends Token<TokenType.CloseSquare, undefined> {
}
export declare interface TokenColon extends Token<TokenType.Colon, undefined> {
}
export declare interface TokenComma extends Token<TokenType.Comma, undefined> {
}
export declare interface TokenComment extends Token<TokenType.Comment, undefined> {
}
export declare interface TokenDelim extends Token<TokenType.Delim, {
/**
* The delim character.
*/
value: string;
}> {
}
export declare interface TokenDimension extends Token<TokenType.Dimension, {
/**
* The numeric value.
*/
value: number;
/**
* The unescaped unit name.
*/
unit: string;
/**
* `integer` or `number`
*/
type: NumberType;
/**
* The sign character as it appeared in the source.
* This is only useful if you need to determine if a value was written as "2px" or "+2px".
*/
signCharacter?: '+' | '-';
}> {
}
export declare interface TokenEOF extends Token<TokenType.EOF, undefined> {
}
export declare interface TokenFunction extends Token<TokenType.Function, {
/**
* The unescaped function name without the trailing `(`.
*/
value: string;
}> {
}
export declare interface TokenHash extends Token<TokenType.Hash, {
/**
* The unescaped hash value without the leading `#`.
*/
value: string;
/**
* The hash type.
*/
type: HashType;
}> {
}
export declare interface TokenIdent extends Token<TokenType.Ident, {
/**
* The unescaped ident value.
*/
value: string;
}> {
}
/**
* Tokenize a CSS string into a list of tokens.
*/
export declare function tokenize(input: {
css: {
valueOf(): string;
};
unicodeRangesAllowed?: boolean;
}, options?: {
onParseError?: (error: ParseError) => void;
}): Array<CSSToken>;
/**
* Create a tokenizer for a CSS string.
*/
export declare function tokenizer(input: {
css: {
valueOf(): string;
};
unicodeRangesAllowed?: boolean;
}, options?: {
onParseError?: (error: ParseError) => void;
}): {
nextToken: () => CSSToken;
endOfFile: () => boolean;
};
export declare interface TokenNumber extends Token<TokenType.Number, {
/**
* The numeric value.
*/
value: number;
/**
* `integer` or `number`
*/
type: NumberType;
/**
* The sign character as it appeared in the source.
* This is only useful if you need to determine if a value was written as "2" or "+2".
*/
signCharacter?: '+' | '-';
}> {
}
export declare interface TokenOpenCurly extends Token<TokenType.OpenCurly, undefined> {
}
export declare interface TokenOpenParen extends Token<TokenType.OpenParen, undefined> {
}
export declare interface TokenOpenSquare extends Token<TokenType.OpenSquare, undefined> {
}
export declare interface TokenPercentage extends Token<TokenType.Percentage, {
/**
* The numeric value.
*/
value: number;
/**
* The sign character as it appeared in the source.
* This is only useful if you need to determine if a value was written as "2%" or "+2%".
*/
signCharacter?: '+' | '-';
}> {
}
export declare interface TokenSemicolon extends Token<TokenType.Semicolon, undefined> {
}
export declare interface TokenString extends Token<TokenType.String, {
/**
* The unescaped string value without the leading and trailing quotes.
*/
value: string;
}> {
}
/**
* All possible CSS token types
*/
export declare enum TokenType {
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#comment-diagram}
*/
Comment = "comment",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-at-keyword-token}
*/
AtKeyword = "at-keyword-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-bad-string-token}
*/
BadString = "bad-string-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-bad-url-token}
*/
BadURL = "bad-url-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-cdc-token}
*/
CDC = "CDC-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-cdo-token}
*/
CDO = "CDO-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-colon-token}
*/
Colon = "colon-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-comma-token}
*/
Comma = "comma-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-delim-token}
*/
Delim = "delim-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-dimension-token}
*/
Dimension = "dimension-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-eof-token}
*/
EOF = "EOF-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-function-token}
*/
Function = "function-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-hash-token}
*/
Hash = "hash-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-ident-token}
*/
Ident = "ident-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-percentage-token}
*/
Number = "number-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-percentage-token}
*/
Percentage = "percentage-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-semicolon-token}
*/
Semicolon = "semicolon-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-string-token}
*/
String = "string-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-url-token}
*/
URL = "url-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-whitespace-token}
*/
Whitespace = "whitespace-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#tokendef-open-paren}
*/
OpenParen = "(-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#tokendef-close-paren}
*/
CloseParen = ")-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#tokendef-open-square}
*/
OpenSquare = "[-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#tokendef-close-square}
*/
CloseSquare = "]-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#tokendef-open-curly}
*/
OpenCurly = "{-token",
/**
* @see {@link https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#tokendef-close-curly}
*/
CloseCurly = "}-token",
/**
* Only appears in the token stream when the `unicodeRangesAllowed` option is set to true.
*
* @example
* ```js
* import { tokenize } from '@csstools/css-tokenizer';
*
* const tokens = tokenize({
* css: `U+0025-00FF, U+4??`,
* unicodeRangesAllowed: true,
* });
*
* console.log(tokens);
* ```
*
* @see {@link https://drafts.csswg.org/css-syntax/#typedef-unicode-range-token}
*/
UnicodeRange = "unicode-range-token"
}
export declare interface TokenUnicodeRange extends Token<TokenType.UnicodeRange, {
startOfRange: number;
endOfRange: number;
}> {
}
export declare interface TokenURL extends Token<TokenType.URL, {
/**
* The unescaped URL value without the leading `url(` and trailing `)`.
*/
value: string;
}> {
}
export declare interface TokenWhitespace extends Token<TokenType.Whitespace, undefined> {
}
export { }

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,62 @@
{
"name": "@csstools/css-tokenizer",
"description": "Tokenize CSS",
"version": "3.0.4",
"contributors": [
{
"name": "Antonio Laguna",
"email": "antonio@laguna.es",
"url": "https://antonio.laguna.es"
},
{
"name": "Romain Menke",
"email": "romainmenke@gmail.com"
}
],
"license": "MIT",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/csstools"
},
{
"type": "opencollective",
"url": "https://opencollective.com/csstools"
}
],
"engines": {
"node": ">=18"
},
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"require": {
"default": "./dist/index.cjs"
}
}
},
"files": [
"CHANGELOG.md",
"LICENSE.md",
"README.md",
"dist"
],
"scripts": {},
"homepage": "https://github.com/csstools/postcss-plugins/tree/main/packages/css-tokenizer#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/csstools/postcss-plugins.git",
"directory": "packages/css-tokenizer"
},
"bugs": "https://github.com/csstools/postcss-plugins/issues",
"keywords": [
"css",
"tokenizer"
]
}