@csstools/postcss-color-function 1.1.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changes to PostCSS Color Function
2
2
 
3
+ ### 2.0.0 (January 24, 2023)
4
+
5
+ - Updated: Support for Node v14+ (major).
6
+
3
7
  ### 1.1.1 (July 8, 2022)
4
8
 
5
9
  - Fix case insensitive matching.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # PostCSS Color Function [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
1
+ # PostCSS Color Function [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][PostCSS]
2
2
 
3
3
  [<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/postcss-color-function.svg" height="20">][npm-url] [<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/color-function.svg" height="20">][css-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]
4
4
 
@@ -47,8 +47,13 @@ postcss([
47
47
  [PostCSS Color Function] runs in all Node environments, with special
48
48
  instructions for:
49
49
 
50
- | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
51
- | --- | --- | --- | --- | --- | --- |
50
+ - [Node](INSTALL.md#node)
51
+ - [PostCSS CLI](INSTALL.md#postcss-cli)
52
+ - [PostCSS Load Config](INSTALL.md#postcss-load-config)
53
+ - [Webpack](INSTALL.md#webpack)
54
+ - [Next.js](INSTALL.md#nextjs)
55
+ - [Gulp](INSTALL.md#gulp)
56
+ - [Grunt](INSTALL.md#grunt)
52
57
 
53
58
  ## Options
54
59
 
@@ -162,9 +167,6 @@ This software or document includes material copied from or derived from https://
162
167
  [discord]: https://discord.gg/bUadyRwkJS
163
168
  [npm-url]: https://www.npmjs.com/package/@csstools/postcss-color-function
164
169
 
165
- [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
166
- [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
167
170
  [PostCSS]: https://github.com/postcss/postcss
168
- [PostCSS Loader]: https://github.com/postcss/postcss-loader
169
171
  [PostCSS Color Function]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-color-function
170
172
  [CSS Color]: https://www.w3.org/TR/css-color-4/#funcdef-color
@@ -1,3 +1,3 @@
1
- declare type color = [number, number, number];
1
+ type color = [number, number, number];
2
2
  export declare function a98RgbToSRgb(a98: color): color;
3
3
  export {};
@@ -1,3 +1,3 @@
1
- declare type color = [number, number, number];
1
+ type color = [number, number, number];
2
2
  export declare function cieXyz50ToSRgb(xyz: color): color;
3
3
  export {};
@@ -1,3 +1,3 @@
1
- declare type color = [number, number, number];
1
+ type color = [number, number, number];
2
2
  export declare function cieXyz65ToSRgb(xyz: color): color;
3
3
  export {};
@@ -1,3 +1,3 @@
1
- declare type color = [number, number, number];
1
+ type color = [number, number, number];
2
2
  export declare function displayP3ToSRgb(displayP3: color): color;
3
3
  export {};
@@ -1,3 +1,3 @@
1
- declare type color = [number, number, number];
1
+ type color = [number, number, number];
2
2
  export declare function prophotoRgbToSRgb(prophoto: color): color;
3
3
  export {};
@@ -1,3 +1,3 @@
1
- declare type color = [number, number, number];
1
+ type color = [number, number, number];
2
2
  export declare function rec2020ToSRgb(rec: color): color;
3
3
  export {};
@@ -1,3 +1,3 @@
1
- declare type color = [number, number, number];
1
+ type color = [number, number, number];
2
2
  export declare function sRgbLinearToSRgb(linearSRgb: color): color;
3
3
  export {};
@@ -1,3 +1,3 @@
1
- declare type color = [number, number, number];
1
+ type color = [number, number, number];
2
2
  export declare function sRgbToSRgb(sRgb: color): color;
3
3
  export {};
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js
8
8
  */
9
- declare type color = [number, number, number];
9
+ type color = [number, number, number];
10
10
  export declare const D50: number[];
11
11
  export declare const D65: number[];
12
12
  export declare function lin_sRGB(RGB: color): color;
@@ -6,6 +6,6 @@
6
6
  *
7
7
  * @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/deltaEOK.js
8
8
  */
9
- declare type color = [number, number, number];
9
+ type color = [number, number, number];
10
10
  export declare function deltaEOK(reference: color, sample: color): number;
11
11
  export {};
@@ -1,4 +1,4 @@
1
- declare type color = [number, number, number];
1
+ type color = [number, number, number];
2
2
  export declare function mapGamut(startOKLCH: color, toDestination: (x: color) => color, fromDestination: (x: color) => color): color;
3
3
  export declare function clip(color: color): color;
4
4
  export declare function inGamut(x: color): boolean;
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/utilities.js
8
8
  */
9
- declare type color = [number, number, number];
9
+ type color = [number, number, number];
10
10
  export declare function sRGB_to_luminance(RGB: color): number;
11
11
  export declare function contrast(RGB1: color, RGB2: color): number;
12
12
  export declare function sRGB_to_LCH(RGB: color): color;
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var e=require("@csstools/postcss-progressive-custom-properties"),r=require("postcss-value-parser");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=t(e),u=t(r);
1
+ "use strict";var t=require("@csstools/postcss-progressive-custom-properties"),e=require("postcss-value-parser");function hasFallback(t){const e=t.parent;if(!e)return!1;const r=t.prop.toLowerCase(),n=e.index(t);for(let t=0;t<n;t++){const n=e.nodes[t];if("decl"===n.type&&n.prop.toLowerCase()===r)return!0}return!1}function hasSupportsAtRuleAncestor(t){let e=t.parent;for(;e;)if("atrule"===e.type){if("supports"===e.name&&-1!==e.params.indexOf("color("))return!0;e=e.parent}else e=e.parent;return!1}
2
2
  /**
3
3
  * Simple matrix (and vector) multiplication
4
4
  * Warning: No error handling for incompatible dimensions!
@@ -10,8 +10,7 @@
10
10
  * @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/multiply-matrices.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
11
11
  *
12
12
  * @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/multiply-matrices.js
13
- */
14
- function o(e,r){const t=e.length;let n,u;n=Array.isArray(e[0])?e:[e],Array.isArray(r[0])||(u=r.map((e=>[e])));const o=u[0].length,a=u[0].map(((e,r)=>u.map((e=>e[r]))));let s=n.map((e=>a.map((r=>Array.isArray(e)?e.reduce(((e,t,n)=>e+t*(r[n]||0)),0):r.reduce(((r,t)=>r+t*e),0)))));return 1===t&&(s=s[0]),1===o?s.map((e=>e[0])):s}
13
+ */function multiplyMatrices(t,e){const r=t.length;let n,a;n=Array.isArray(t[0])?t:[t],Array.isArray(e[0])||(a=e.map((t=>[t])));const o=a[0].length,s=a[0].map(((t,e)=>a.map((t=>t[e]))));let i=n.map((t=>s.map((e=>Array.isArray(t)?t.reduce(((t,r,n)=>t+r*(e[n]||0)),0):e.reduce(((e,r)=>e+r*t),0)))));return 1===r&&(i=i[0]),1===o?i.map((t=>t[0])):i}
15
14
  /**
16
15
  * @license W3C
17
16
  * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
@@ -19,7 +18,7 @@ function o(e,r){const t=e.length;let n,u;n=Array.isArray(e[0])?e:[e],Array.isArr
19
18
  * @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).
20
19
  *
21
20
  * @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js
22
- */function a(e){return e.map((function(e){const r=e<0?-1:1,t=Math.abs(e);return t<.04045?e/12.92:r*Math.pow((t+.055)/1.055,2.4)}))}function s(e){return e.map((function(e){const r=e<0?-1:1,t=Math.abs(e);return t>.0031308?r*(1.055*Math.pow(t,1/2.4)-.055):12.92*e}))}function c(e){return o([[.41239079926595934,.357584339383878,.1804807884018343],[.21263900587151027,.715168678767756,.07219231536073371],[.01933081871559182,.11919477979462598,.9505321522496607]],e)}function i(e){return o([[3.2409699419045226,-1.537383177570094,-.4986107602930034],[-.9692436362808796,1.8759675015077202,.04155505740717559],[.05563007969699366,-.20397695888897652,1.0569715142428786]],e)}function l(e){return o([[.9554734527042182,-.023098536874261423,.0632593086610217],[-.028369706963208136,1.0099954580058226,.021041398966943008],[.012314001688319899,-.020507696433477912,1.3303659366080753]],e)}function p(e){const r=o([[.8190224432164319,.3619062562801221,-.12887378261216414],[.0329836671980271,.9292868468965546,.03614466816999844],[.048177199566046255,.26423952494422764,.6335478258136937]],e);return o([[.2104542553,.793617785,-.0040720468],[1.9779984951,-2.428592205,.4505937099],[.0259040371,.7827717662,-.808675766]],r.map((e=>Math.cbrt(e))))}function f(e){const r=o([[.9999999984505198,.39633779217376786,.2158037580607588],[1.0000000088817609,-.10556134232365635,-.06385417477170591],[1.0000000546724108,-.08948418209496575,-1.2914855378640917]],e);return o([[1.2268798733741557,-.5578149965554813,.28139105017721583],[-.04057576262431372,1.1122868293970594,-.07171106666151701],[-.07637294974672142,-.4214933239627914,1.5869240244272418]],r.map((e=>e**3)))}function d(e){const r=180*Math.atan2(e[2],e[1])/Math.PI;return[e[0],Math.sqrt(e[1]**2+e[2]**2),r>=0?r:r+360]}function v(e){return[e[0],e[1]*Math.cos(e[2]*Math.PI/180),e[1]*Math.sin(e[2]*Math.PI/180)]}
21
+ */function lin_sRGB(t){return t.map((function(t){const e=t<0?-1:1,r=Math.abs(t);return r<.04045?t/12.92:e*Math.pow((r+.055)/1.055,2.4)}))}function gam_sRGB(t){return t.map((function(t){const e=t<0?-1:1,r=Math.abs(t);return r>.0031308?e*(1.055*Math.pow(r,1/2.4)-.055):12.92*t}))}function lin_sRGB_to_XYZ(t){return multiplyMatrices([[.41239079926595934,.357584339383878,.1804807884018343],[.21263900587151027,.715168678767756,.07219231536073371],[.01933081871559182,.11919477979462598,.9505321522496607]],t)}function XYZ_to_lin_sRGB(t){return multiplyMatrices([[3.2409699419045226,-1.537383177570094,-.4986107602930034],[-.9692436362808796,1.8759675015077202,.04155505740717559],[.05563007969699366,-.20397695888897652,1.0569715142428786]],t)}function lin_2020(t){const e=1.09929682680944;return t.map((function(t){const r=t<0?-1:1,n=Math.abs(t);return n<.08124285829863151?t/4.5:r*Math.pow((n+e-1)/e,1/.45)}))}function D50_to_D65(t){return multiplyMatrices([[.9554734527042182,-.023098536874261423,.0632593086610217],[-.028369706963208136,1.0099954580058226,.021041398966943008],[.012314001688319899,-.020507696433477912,1.3303659366080753]],t)}function XYZ_to_OKLab(t){const e=multiplyMatrices([[.8190224432164319,.3619062562801221,-.12887378261216414],[.0329836671980271,.9292868468965546,.03614466816999844],[.048177199566046255,.26423952494422764,.6335478258136937]],t);return multiplyMatrices([[.2104542553,.793617785,-.0040720468],[1.9779984951,-2.428592205,.4505937099],[.0259040371,.7827717662,-.808675766]],e.map((t=>Math.cbrt(t))))}function OKLab_to_XYZ(t){const e=multiplyMatrices([[.9999999984505198,.39633779217376786,.2158037580607588],[1.0000000088817609,-.10556134232365635,-.06385417477170591],[1.0000000546724108,-.08948418209496575,-1.2914855378640917]],t);return multiplyMatrices([[1.2268798733741557,-.5578149965554813,.28139105017721583],[-.04057576262431372,1.1122868293970594,-.07171106666151701],[-.07637294974672142,-.4214933239627914,1.5869240244272418]],e.map((t=>t**3)))}function OKLab_to_OKLCH(t){const e=180*Math.atan2(t[2],t[1])/Math.PI;return[t[0],Math.sqrt(t[1]**2+t[2]**2),e>=0?e:e+360]}function OKLCH_to_OKLab(t){return[t[0],t[1]*Math.cos(t[2]*Math.PI/180),t[1]*Math.sin(t[2]*Math.PI/180)]}
23
22
  /**
24
23
  * @license W3C
25
24
  * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
@@ -27,4 +26,4 @@ function o(e,r){const t=e.length;let n,u;n=Array.isArray(e[0])?e:[e],Array.isArr
27
26
  * @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).
28
27
  *
29
28
  * @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/deltaEOK.js
30
- */function m(e,r){const[t,n,u]=e,[o,a,s]=r,c=t-o,i=n-a,l=u-s;return Math.sqrt(c**2+i**2+l**2)}function h(e,r,t){return function(e,r,t){let n=0,u=e[1];const o=e;for(;u-n>1e-4;){const e=b(r(o));m(v(o),v(t(e)))-.02<1e-4?n=o[1]:u=o[1],o[1]=(u+n)/2}return b(r([...o]))}(e,r,t)}function b(e){return e.map((e=>e<0?0:e>1?1:e))}function y(e){const[r,t,n]=e;return r>=-1e-4&&r<=1.0001&&t>=-1e-4&&t<=1.0001&&n>=-1e-4&&n<=1.0001}function g(e){let r=e.slice();r=r.map((function(e){const r=e<0?-1:1,t=Math.abs(e);return r*Math.pow(t,563/256)})),r=o([[.5766690429101305,.1855582379065463,.1882286462349947],[.29734497525053605,.6273635662554661,.07529145849399788],[.02703136138641234,.07068885253582723,.9913375368376388]],r);let t=r.slice();return t=p(t),t=d(t),t[0]<1e-6&&(t=[0,0,0]),t[0]>.999999&&(t=[1,0,0]),r=i(r),r=s(r),y(r)?b(r):h(t,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=a(e))))))}function w(e){let r=e.slice();r=l(r);let t=r.slice();return t=p(t),t=d(t),t[0]<1e-6&&(t=[0,0,0]),t[0]>.999999&&(t=[1,0,0]),r=i(r),r=s(r),y(r)?b(r):h(t,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=a(e))))))}function x(e){let r=e.slice(),t=r.slice();return t=p(t),t=d(t),t[0]<1e-6&&(t=[0,0,0]),t[0]>.999999&&(t=[1,0,0]),r=i(r),r=s(r),y(r)?b(r):h(t,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=a(e))))))}function M(e){let r=e.slice();r=a(r),r=o([[.4865709486482162,.26566769316909306,.1982172852343625],[.2289745640697488,.6917385218365064,.079286914093745],[0,.04511338185890264,1.043944368900976]],r);let t=r.slice();return t=p(t),t=d(t),t[0]<1e-6&&(t=[0,0,0]),t[0]>.999999&&(t=[1,0,0]),r=i(r),r=s(r),y(r)?b(r):h(t,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=a(e))))))}function I(e){let r=e.slice();r=r.map((function(e){const r=e<0?-1:1;return Math.abs(e)<=.03125?e/16:r*Math.pow(e,1.8)})),r=o([[.7977604896723027,.13518583717574031,.0313493495815248],[.2880711282292934,.7118432178101014,8565396060525902e-20],[0,0,.8251046025104601]],r),r=l(r);let t=r.slice();return t=p(t),t=d(t),t[0]<1e-6&&(t=[0,0,0]),t[0]>.999999&&(t=[1,0,0]),r=i(r),r=s(r),y(r)?b(r):h(t,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=a(e))))))}function S(e){let r=e.slice();r=function(e){const r=1.09929682680944;return e.map((function(e){const t=e<0?-1:1,n=Math.abs(e);return n<.08124285829863151?e/4.5:t*Math.pow((n+r-1)/r,1/.45)}))}(r),r=o([[.6369580483012914,.14461690358620832,.1688809751641721],[.2627002120112671,.6779980715188708,.05930171646986196],[0,.028072693049087428,1.060985057710791]],r);let t=r.slice();return t=p(t),t=d(t),t[0]<1e-6&&(t=[0,0,0]),t[0]>.999999&&(t=[1,0,0]),r=i(r),r=s(r),y(r)?b(r):h(t,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=a(e))))))}function C(e){let r=e.slice();r=c(r);let t=r.slice();return t=p(t),t=d(t),t[0]<1e-6&&(t=[0,0,0]),t[0]>.999999&&(t=[1,0,0]),r=i(r),r=s(r),y(r)?b(r):h(t,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=a(e))))))}function k(e){let r=e.slice();r=a(r),r=c(r);let t=r.slice();return t=p(t),t=d(t),t[0]<1e-6&&(t=[0,0,0]),t[0]>.999999&&(t=[1,0,0]),r=i(r),r=s(r),y(r)?b(r):h(t,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=a(e))))))}function L(e,r,t,n){const o=u.default.stringify(e),a=e.value,s=e.nodes.slice().filter((e=>"comment"!==e.type&&"space"!==e.type));let c,i=null;if("color"===a.toLowerCase()&&(i=function(e){if(!function(e){if(!e||"word"!==e.type)return!1;switch(e.value.toLowerCase()){case"srgb":case"srgb-linear":case"display-p3":case"a98-rgb":case"prophoto-rgb":case"rec2020":case"xyz-d50":case"xyz-d65":case"xyz":return!0;default:return!1}}(e[0]))return null;const r={colorSpace:e[0].value.toLowerCase(),colorSpaceNode:e[0],parameters:[]};for(let t=1;t<e.length;t++)if(A(e[t]))r.slash=e[t];else{if(r.slash&&(P(e[t])||E(e[t])||z(e[t]))){r.alpha=e[t];break}if(!P(e[t]))return null;{const n=u.default.unit(e[t].value);"%"===n.unit&&(n.number=String(parseFloat(n.number)/100),n.unit="",e[t].value=String(n.number)),r.parameters.push({value:n,node:e[t]})}}if(0===r.parameters.length)return r;r.parameters.length<3&&(r.parameters=[...r.parameters,{node:{sourceIndex:0,sourceEndIndex:1,value:"0",type:"word"},value:{number:"0",unit:""}},{node:{sourceIndex:0,sourceEndIndex:1,value:"0",type:"word"},value:{number:"0",unit:""}}]);r.parameters.length>3&&(r.parameters=r.parameters.slice(0,3));return r}(s)),!i)return;switch(e.value="rgb",function(e,r,t){if(!r||!t)return;if(e.value="rgba",r.value=",",r.before="",!function(e){if(!e||"word"!==e.type)return!1;if(!q(e))return!1;const r=u.default.unit(e.value);if(!r)return!1;return!!r.number}(t))return;const n=u.default.unit(t.value);if(!n)return;"%"===n.unit&&(n.number=String(parseFloat(n.number)/100),t.value=String(n.number))}(e,i.slash,i.alpha),i.colorSpace){case"srgb":c=k;break;case"srgb-linear":c=C;break;case"a98-rgb":c=g;break;case"prophoto-rgb":c=I;break;case"display-p3":c=M;break;case"rec2020":c=S;break;case"xyz-d50":c=w;break;case"xyz-d65":case"xyz":c=x;break;default:return}const l=(p=i,p.parameters.map((e=>e.value))).map((e=>parseFloat(e.number)));var p;const f=c(l);!y(l)&&n&&r.warn(t,`"${o}" is out of gamut for "${i.colorSpace}". Given "preserve: true" is set, this will lead to unexpected results in some browsers.`),e.nodes=[{sourceIndex:0,sourceEndIndex:1,value:String(Math.round(255*f[0])),type:"word"},{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""},{sourceIndex:0,sourceEndIndex:1,value:String(Math.round(255*f[1])),type:"word"},{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""},{sourceIndex:0,sourceEndIndex:1,value:String(Math.round(255*f[2])),type:"word"}],i.alpha&&(e.nodes.push({sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),e.nodes.push(i.alpha))}function P(e){if(!e||"word"!==e.type)return!1;if(!q(e))return!1;const r=u.default.unit(e.value);return!!r&&("%"===r.unit||""===r.unit)}function E(e){return e&&"function"===e.type&&"calc"===e.value.toLowerCase()}function z(e){return e&&"function"===e.type&&"var"===e.value.toLowerCase()}function A(e){return e&&"div"===e.type&&"/"===e.value}function q(e){if(!e||!e.value)return!1;try{return!1!==u.default.unit(e.value)}catch(e){return!1}}const F=e=>{const r="preserve"in Object(e)&&Boolean(e.preserve);return{postcssPlugin:"postcss-color-function",Declaration:(e,{result:t})=>{if(function(e){const r=e.parent;if(!r)return!1;const t=e.prop.toLowerCase(),n=r.index(e);for(let e=0;e<n;e++){const n=r.nodes[e];if("decl"===n.type&&n.prop.toLowerCase()===t)return!0}return!1}(e))return;if(function(e){let r=e.parent;for(;r;)if("atrule"===r.type){if("supports"===r.name&&-1!==r.params.indexOf("color("))return!0;r=r.parent}else r=r.parent;return!1}(e))return;const n=e.value;if(!n.toLowerCase().includes("color("))return;const o=function(e,r,t,n){let o;try{o=u.default(e)}catch(n){r.warn(t,`Failed to parse value '${e}' as a color function. Leaving the original value intact.`)}if(void 0===o)return;o.walk((e=>{e.type&&"function"===e.type&&"color"===e.value.toLowerCase()&&L(e,r,t,n)}));const a=String(o);return a!==e?a:void 0}(n,e,t,r);void 0!==o&&(e.cloneBefore({value:o}),r||e.remove())}}};F.postcss=!0;const j=e=>{const r=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},e);return r.enableProgressiveCustomProperties&&r.preserve?{postcssPlugin:"postcss-color-function",plugins:[n.default(),F(r)]}:F(r)};j.postcss=!0,module.exports=j;
29
+ */function deltaEOK(t,e){const[r,n,a]=t,[o,s,i]=e,u=r-o,c=n-s,l=a-i;return Math.sqrt(u**2+c**2+l**2)}function mapGamut(t,e,r){return binarySearchGamut(t,e,r)}function binarySearchGamut(t,e,r){let n=0,a=t[1];const o=t;for(;a-n>1e-4;){const t=clip(e(o));deltaEOK(OKLCH_to_OKLab(o),OKLCH_to_OKLab(r(t)))-.02<1e-4?n=o[1]:a=o[1],o[1]=(a+n)/2}return clip(e([...o]))}function clip(t){return t.map((t=>t<0?0:t>1?1:t))}function inGamut(t){const[e,r,n]=t;return e>=-1e-4&&e<=1.0001&&r>=-1e-4&&r<=1.0001&&n>=-1e-4&&n<=1.0001}function a98RgbToSRgb(t){let e=t.slice();e=e.map((function(t){const e=t<0?-1:1,r=Math.abs(t);return e*Math.pow(r,563/256)})),e=multiplyMatrices([[.5766690429101305,.1855582379065463,.1882286462349947],[.29734497525053605,.6273635662554661,.07529145849399788],[.02703136138641234,.07068885253582723,.9913375368376388]],e);let r=e.slice();return r=XYZ_to_OKLab(r),r=OKLab_to_OKLCH(r),r[0]<1e-6&&(r=[0,0,0]),r[0]>.999999&&(r=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(r,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function cieXyz50ToSRgb(t){let e=t.slice();e=D50_to_D65(e);let r=e.slice();return r=XYZ_to_OKLab(r),r=OKLab_to_OKLCH(r),r[0]<1e-6&&(r=[0,0,0]),r[0]>.999999&&(r=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(r,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function cieXyz65ToSRgb(t){let e=t.slice(),r=e.slice();return r=XYZ_to_OKLab(r),r=OKLab_to_OKLCH(r),r[0]<1e-6&&(r=[0,0,0]),r[0]>.999999&&(r=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(r,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function displayP3ToSRgb(t){let e=t.slice();e=lin_sRGB(e),e=multiplyMatrices([[.4865709486482162,.26566769316909306,.1982172852343625],[.2289745640697488,.6917385218365064,.079286914093745],[0,.04511338185890264,1.043944368900976]],e);let r=e.slice();return r=XYZ_to_OKLab(r),r=OKLab_to_OKLCH(r),r[0]<1e-6&&(r=[0,0,0]),r[0]>.999999&&(r=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(r,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function prophotoRgbToSRgb(t){let e=t.slice();e=e.map((function(t){const e=t<0?-1:1;return Math.abs(t)<=.03125?t/16:e*Math.pow(t,1.8)})),e=multiplyMatrices([[.7977604896723027,.13518583717574031,.0313493495815248],[.2880711282292934,.7118432178101014,8565396060525902e-20],[0,0,.8251046025104601]],e),e=D50_to_D65(e);let r=e.slice();return r=XYZ_to_OKLab(r),r=OKLab_to_OKLCH(r),r[0]<1e-6&&(r=[0,0,0]),r[0]>.999999&&(r=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(r,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function rec2020ToSRgb(t){let e=t.slice();e=lin_2020(e),e=multiplyMatrices([[.6369580483012914,.14461690358620832,.1688809751641721],[.2627002120112671,.6779980715188708,.05930171646986196],[0,.028072693049087428,1.060985057710791]],e);let r=e.slice();return r=XYZ_to_OKLab(r),r=OKLab_to_OKLCH(r),r[0]<1e-6&&(r=[0,0,0]),r[0]>.999999&&(r=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(r,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function sRgbLinearToSRgb(t){let e=t.slice();e=lin_sRGB_to_XYZ(e);let r=e.slice();return r=XYZ_to_OKLab(r),r=OKLab_to_OKLCH(r),r[0]<1e-6&&(r=[0,0,0]),r[0]>.999999&&(r=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(r,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function sRgbToSRgb(t){let e=t.slice();e=lin_sRGB(e),e=lin_sRGB_to_XYZ(e);let r=e.slice();return r=XYZ_to_OKLab(r),r=OKLab_to_OKLCH(r),r[0]<1e-6&&(r=[0,0,0]),r[0]>.999999&&(r=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(r,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function onCSSFunctionSRgb(t,r,n,a){const o=e.stringify(t),s=t.value,i=t.nodes.slice().filter((t=>"comment"!==t.type&&"space"!==t.type));let u,c=null;if("color"===s.toLowerCase()&&(c=colorFunctionContents(i)),!c)return;switch(t.value="rgb",transformAlpha(t,c.slash,c.alpha),c.colorSpace){case"srgb":u=sRgbToSRgb;break;case"srgb-linear":u=sRgbLinearToSRgb;break;case"a98-rgb":u=a98RgbToSRgb;break;case"prophoto-rgb":u=prophotoRgbToSRgb;break;case"display-p3":u=displayP3ToSRgb;break;case"rec2020":u=rec2020ToSRgb;break;case"xyz-d50":u=cieXyz50ToSRgb;break;case"xyz-d65":case"xyz":u=cieXyz65ToSRgb;break;default:return}const l=(_=c,_.parameters.map((t=>t.value))).map((t=>parseFloat(t.number)));var _;const p=u(l);!inGamut(l)&&a&&r.warn(n,`"${o}" is out of gamut for "${c.colorSpace}". Given "preserve: true" is set, this will lead to unexpected results in some browsers.`),t.nodes=[{sourceIndex:0,sourceEndIndex:1,value:String(Math.round(255*p[0])),type:"word"},{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""},{sourceIndex:0,sourceEndIndex:1,value:String(Math.round(255*p[1])),type:"word"},{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""},{sourceIndex:0,sourceEndIndex:1,value:String(Math.round(255*p[2])),type:"word"}],c.alpha&&(t.nodes.push({sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),t.nodes.push(c.alpha))}function isColorSpaceNode(t){if(!t||"word"!==t.type)return!1;switch(t.value.toLowerCase()){case"srgb":case"srgb-linear":case"display-p3":case"a98-rgb":case"prophoto-rgb":case"rec2020":case"xyz-d50":case"xyz-d65":case"xyz":return!0;default:return!1}}function isNumericNode(t){if(!t||"word"!==t.type)return!1;if(!canParseAsUnit(t))return!1;const r=e.unit(t.value);return!!r&&!!r.number}function isNumericNodePercentageOrNumber(t){if(!t||"word"!==t.type)return!1;if(!canParseAsUnit(t))return!1;const r=e.unit(t.value);return!!r&&("%"===r.unit||""===r.unit)}function isCalcNode(t){return t&&"function"===t.type&&"calc"===t.value.toLowerCase()}function isVarNode(t){return t&&"function"===t.type&&"var"===t.value.toLowerCase()}function colorFunctionContents(t){if(!isColorSpaceNode(t[0]))return null;const r={colorSpace:t[0].value.toLowerCase(),colorSpaceNode:t[0],parameters:[]};for(let a=1;a<t.length;a++)if((n=t[a])&&"div"===n.type&&"/"===n.value)r.slash=t[a];else{if(r.slash&&(isNumericNodePercentageOrNumber(t[a])||isCalcNode(t[a])||isVarNode(t[a]))){r.alpha=t[a];break}if(!isNumericNodePercentageOrNumber(t[a]))return null;{const n=e.unit(t[a].value);"%"===n.unit&&(n.number=String(parseFloat(n.number)/100),n.unit="",t[a].value=String(n.number)),r.parameters.push({value:n,node:t[a]})}}var n;return 0===r.parameters.length||(r.parameters.length<3&&(r.parameters=[...r.parameters,{node:{sourceIndex:0,sourceEndIndex:1,value:"0",type:"word"},value:{number:"0",unit:""}},{node:{sourceIndex:0,sourceEndIndex:1,value:"0",type:"word"},value:{number:"0",unit:""}}]),r.parameters.length>3&&(r.parameters=r.parameters.slice(0,3))),r}function transformAlpha(t,r,n){if(!r||!n)return;if(t.value="rgba",r.value=",",r.before="",!isNumericNode(n))return;const a=e.unit(n.value);a&&"%"===a.unit&&(a.number=String(parseFloat(a.number)/100),n.value=String(a.number))}function canParseAsUnit(t){if(!t||!t.value)return!1;try{return!1!==e.unit(t.value)}catch(t){return!1}}function modifiedValues(t,r,n,a){let o;try{o=e(t)}catch(e){r.warn(n,`Failed to parse value '${t}' as a color function. Leaving the original value intact.`)}if(void 0===o)return;o.walk((t=>{t.type&&"function"===t.type&&"color"===t.value.toLowerCase()&&onCSSFunctionSRgb(t,r,n,a)}));const s=String(o);return s!==t?s:void 0}const basePlugin=t=>{const e="preserve"in Object(t)&&Boolean(t.preserve);return{postcssPlugin:"postcss-color-function",Declaration:(t,{result:r})=>{if(hasFallback(t))return;if(hasSupportsAtRuleAncestor(t))return;const n=t.value;if(!n.toLowerCase().includes("color("))return;const a=modifiedValues(n,t,r,e);void 0!==a&&(t.cloneBefore({value:a}),e||t.remove())}}};basePlugin.postcss=!0;const postcssPlugin=e=>{const r=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},e);return r.enableProgressiveCustomProperties&&r.preserve?{postcssPlugin:"postcss-color-function",plugins:[t(),basePlugin(r)]}:basePlugin(r)};postcssPlugin.postcss=!0,module.exports=postcssPlugin;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import type { PluginCreator } from 'postcss';
2
- declare type pluginOptions = {
2
+ /** postcss-color-function plugin options */
3
+ export type pluginOptions = {
4
+ /** Preserve the original notation. default: false */
3
5
  preserve?: boolean;
6
+ /** Enable "@csstools/postcss-progressive-custom-properties". default: true */
4
7
  enableProgressiveCustomProperties?: boolean;
5
8
  };
6
9
  declare const postcssPlugin: PluginCreator<pluginOptions>;
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import e from"@csstools/postcss-progressive-custom-properties";import r from"postcss-value-parser";
1
+ import t from"@csstools/postcss-progressive-custom-properties";import e from"postcss-value-parser";function hasFallback(t){const e=t.parent;if(!e)return!1;const n=t.prop.toLowerCase(),r=e.index(t);for(let t=0;t<r;t++){const r=e.nodes[t];if("decl"===r.type&&r.prop.toLowerCase()===n)return!0}return!1}function hasSupportsAtRuleAncestor(t){let e=t.parent;for(;e;)if("atrule"===e.type){if("supports"===e.name&&-1!==e.params.indexOf("color("))return!0;e=e.parent}else e=e.parent;return!1}
2
2
  /**
3
3
  * Simple matrix (and vector) multiplication
4
4
  * Warning: No error handling for incompatible dimensions!
@@ -10,8 +10,7 @@ import e from"@csstools/postcss-progressive-custom-properties";import r from"pos
10
10
  * @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/multiply-matrices.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
11
11
  *
12
12
  * @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/multiply-matrices.js
13
- */
14
- function t(e,r){const t=e.length;let n,o;n=Array.isArray(e[0])?e:[e],Array.isArray(r[0])||(o=r.map((e=>[e])));const u=o[0].length,a=o[0].map(((e,r)=>o.map((e=>e[r]))));let s=n.map((e=>a.map((r=>Array.isArray(e)?e.reduce(((e,t,n)=>e+t*(r[n]||0)),0):r.reduce(((r,t)=>r+t*e),0)))));return 1===t&&(s=s[0]),1===u?s.map((e=>e[0])):s}
13
+ */function multiplyMatrices(t,e){const n=t.length;let r,o;r=Array.isArray(t[0])?t:[t],Array.isArray(e[0])||(o=e.map((t=>[t])));const a=o[0].length,s=o[0].map(((t,e)=>o.map((t=>t[e]))));let i=r.map((t=>s.map((e=>Array.isArray(t)?t.reduce(((t,n,r)=>t+n*(e[r]||0)),0):e.reduce(((e,n)=>e+n*t),0)))));return 1===n&&(i=i[0]),1===a?i.map((t=>t[0])):i}
15
14
  /**
16
15
  * @license W3C
17
16
  * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
@@ -19,7 +18,7 @@ function t(e,r){const t=e.length;let n,o;n=Array.isArray(e[0])?e:[e],Array.isArr
19
18
  * @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).
20
19
  *
21
20
  * @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js
22
- */function n(e){return e.map((function(e){const r=e<0?-1:1,t=Math.abs(e);return t<.04045?e/12.92:r*Math.pow((t+.055)/1.055,2.4)}))}function o(e){return e.map((function(e){const r=e<0?-1:1,t=Math.abs(e);return t>.0031308?r*(1.055*Math.pow(t,1/2.4)-.055):12.92*e}))}function u(e){return t([[.41239079926595934,.357584339383878,.1804807884018343],[.21263900587151027,.715168678767756,.07219231536073371],[.01933081871559182,.11919477979462598,.9505321522496607]],e)}function a(e){return t([[3.2409699419045226,-1.537383177570094,-.4986107602930034],[-.9692436362808796,1.8759675015077202,.04155505740717559],[.05563007969699366,-.20397695888897652,1.0569715142428786]],e)}function s(e){return t([[.9554734527042182,-.023098536874261423,.0632593086610217],[-.028369706963208136,1.0099954580058226,.021041398966943008],[.012314001688319899,-.020507696433477912,1.3303659366080753]],e)}function c(e){const r=t([[.8190224432164319,.3619062562801221,-.12887378261216414],[.0329836671980271,.9292868468965546,.03614466816999844],[.048177199566046255,.26423952494422764,.6335478258136937]],e);return t([[.2104542553,.793617785,-.0040720468],[1.9779984951,-2.428592205,.4505937099],[.0259040371,.7827717662,-.808675766]],r.map((e=>Math.cbrt(e))))}function i(e){const r=t([[.9999999984505198,.39633779217376786,.2158037580607588],[1.0000000088817609,-.10556134232365635,-.06385417477170591],[1.0000000546724108,-.08948418209496575,-1.2914855378640917]],e);return t([[1.2268798733741557,-.5578149965554813,.28139105017721583],[-.04057576262431372,1.1122868293970594,-.07171106666151701],[-.07637294974672142,-.4214933239627914,1.5869240244272418]],r.map((e=>e**3)))}function l(e){const r=180*Math.atan2(e[2],e[1])/Math.PI;return[e[0],Math.sqrt(e[1]**2+e[2]**2),r>=0?r:r+360]}function p(e){return[e[0],e[1]*Math.cos(e[2]*Math.PI/180),e[1]*Math.sin(e[2]*Math.PI/180)]}
21
+ */function lin_sRGB(t){return t.map((function(t){const e=t<0?-1:1,n=Math.abs(t);return n<.04045?t/12.92:e*Math.pow((n+.055)/1.055,2.4)}))}function gam_sRGB(t){return t.map((function(t){const e=t<0?-1:1,n=Math.abs(t);return n>.0031308?e*(1.055*Math.pow(n,1/2.4)-.055):12.92*t}))}function lin_sRGB_to_XYZ(t){return multiplyMatrices([[.41239079926595934,.357584339383878,.1804807884018343],[.21263900587151027,.715168678767756,.07219231536073371],[.01933081871559182,.11919477979462598,.9505321522496607]],t)}function XYZ_to_lin_sRGB(t){return multiplyMatrices([[3.2409699419045226,-1.537383177570094,-.4986107602930034],[-.9692436362808796,1.8759675015077202,.04155505740717559],[.05563007969699366,-.20397695888897652,1.0569715142428786]],t)}function lin_2020(t){const e=1.09929682680944;return t.map((function(t){const n=t<0?-1:1,r=Math.abs(t);return r<.08124285829863151?t/4.5:n*Math.pow((r+e-1)/e,1/.45)}))}function D50_to_D65(t){return multiplyMatrices([[.9554734527042182,-.023098536874261423,.0632593086610217],[-.028369706963208136,1.0099954580058226,.021041398966943008],[.012314001688319899,-.020507696433477912,1.3303659366080753]],t)}function XYZ_to_OKLab(t){const e=multiplyMatrices([[.8190224432164319,.3619062562801221,-.12887378261216414],[.0329836671980271,.9292868468965546,.03614466816999844],[.048177199566046255,.26423952494422764,.6335478258136937]],t);return multiplyMatrices([[.2104542553,.793617785,-.0040720468],[1.9779984951,-2.428592205,.4505937099],[.0259040371,.7827717662,-.808675766]],e.map((t=>Math.cbrt(t))))}function OKLab_to_XYZ(t){const e=multiplyMatrices([[.9999999984505198,.39633779217376786,.2158037580607588],[1.0000000088817609,-.10556134232365635,-.06385417477170591],[1.0000000546724108,-.08948418209496575,-1.2914855378640917]],t);return multiplyMatrices([[1.2268798733741557,-.5578149965554813,.28139105017721583],[-.04057576262431372,1.1122868293970594,-.07171106666151701],[-.07637294974672142,-.4214933239627914,1.5869240244272418]],e.map((t=>t**3)))}function OKLab_to_OKLCH(t){const e=180*Math.atan2(t[2],t[1])/Math.PI;return[t[0],Math.sqrt(t[1]**2+t[2]**2),e>=0?e:e+360]}function OKLCH_to_OKLab(t){return[t[0],t[1]*Math.cos(t[2]*Math.PI/180),t[1]*Math.sin(t[2]*Math.PI/180)]}
23
22
  /**
24
23
  * @license W3C
25
24
  * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
@@ -27,4 +26,4 @@ function t(e,r){const t=e.length;let n,o;n=Array.isArray(e[0])?e:[e],Array.isArr
27
26
  * @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).
28
27
  *
29
28
  * @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/deltaEOK.js
30
- */function f(e,r){const[t,n,o]=e,[u,a,s]=r,c=t-u,i=n-a,l=o-s;return Math.sqrt(c**2+i**2+l**2)}function d(e,r,t){return function(e,r,t){let n=0,o=e[1];const u=e;for(;o-n>1e-4;){const e=v(r(u));f(p(u),p(t(e)))-.02<1e-4?n=u[1]:o=u[1],u[1]=(o+n)/2}return v(r([...u]))}(e,r,t)}function v(e){return e.map((e=>e<0?0:e>1?1:e))}function m(e){const[r,t,n]=e;return r>=-1e-4&&r<=1.0001&&t>=-1e-4&&t<=1.0001&&n>=-1e-4&&n<=1.0001}function h(e){let r=e.slice();r=r.map((function(e){const r=e<0?-1:1,t=Math.abs(e);return r*Math.pow(t,563/256)})),r=t([[.5766690429101305,.1855582379065463,.1882286462349947],[.29734497525053605,.6273635662554661,.07529145849399788],[.02703136138641234,.07068885253582723,.9913375368376388]],r);let s=r.slice();return s=c(s),s=l(s),s[0]<1e-6&&(s=[0,0,0]),s[0]>.999999&&(s=[1,0,0]),r=a(r),r=o(r),m(r)?v(r):d(s,(e=>o(e=a(e=i(e=p(e))))),(e=>l(e=c(e=u(e=n(e))))))}function b(e){let r=e.slice();r=s(r);let t=r.slice();return t=c(t),t=l(t),t[0]<1e-6&&(t=[0,0,0]),t[0]>.999999&&(t=[1,0,0]),r=a(r),r=o(r),m(r)?v(r):d(t,(e=>o(e=a(e=i(e=p(e))))),(e=>l(e=c(e=u(e=n(e))))))}function y(e){let r=e.slice(),t=r.slice();return t=c(t),t=l(t),t[0]<1e-6&&(t=[0,0,0]),t[0]>.999999&&(t=[1,0,0]),r=a(r),r=o(r),m(r)?v(r):d(t,(e=>o(e=a(e=i(e=p(e))))),(e=>l(e=c(e=u(e=n(e))))))}function g(e){let r=e.slice();r=n(r),r=t([[.4865709486482162,.26566769316909306,.1982172852343625],[.2289745640697488,.6917385218365064,.079286914093745],[0,.04511338185890264,1.043944368900976]],r);let s=r.slice();return s=c(s),s=l(s),s[0]<1e-6&&(s=[0,0,0]),s[0]>.999999&&(s=[1,0,0]),r=a(r),r=o(r),m(r)?v(r):d(s,(e=>o(e=a(e=i(e=p(e))))),(e=>l(e=c(e=u(e=n(e))))))}function w(e){let r=e.slice();r=r.map((function(e){const r=e<0?-1:1;return Math.abs(e)<=.03125?e/16:r*Math.pow(e,1.8)})),r=t([[.7977604896723027,.13518583717574031,.0313493495815248],[.2880711282292934,.7118432178101014,8565396060525902e-20],[0,0,.8251046025104601]],r),r=s(r);let f=r.slice();return f=c(f),f=l(f),f[0]<1e-6&&(f=[0,0,0]),f[0]>.999999&&(f=[1,0,0]),r=a(r),r=o(r),m(r)?v(r):d(f,(e=>o(e=a(e=i(e=p(e))))),(e=>l(e=c(e=u(e=n(e))))))}function x(e){let r=e.slice();r=function(e){const r=1.09929682680944;return e.map((function(e){const t=e<0?-1:1,n=Math.abs(e);return n<.08124285829863151?e/4.5:t*Math.pow((n+r-1)/r,1/.45)}))}(r),r=t([[.6369580483012914,.14461690358620832,.1688809751641721],[.2627002120112671,.6779980715188708,.05930171646986196],[0,.028072693049087428,1.060985057710791]],r);let s=r.slice();return s=c(s),s=l(s),s[0]<1e-6&&(s=[0,0,0]),s[0]>.999999&&(s=[1,0,0]),r=a(r),r=o(r),m(r)?v(r):d(s,(e=>o(e=a(e=i(e=p(e))))),(e=>l(e=c(e=u(e=n(e))))))}function M(e){let r=e.slice();r=u(r);let t=r.slice();return t=c(t),t=l(t),t[0]<1e-6&&(t=[0,0,0]),t[0]>.999999&&(t=[1,0,0]),r=a(r),r=o(r),m(r)?v(r):d(t,(e=>o(e=a(e=i(e=p(e))))),(e=>l(e=c(e=u(e=n(e))))))}function I(e){let r=e.slice();r=n(r),r=u(r);let t=r.slice();return t=c(t),t=l(t),t[0]<1e-6&&(t=[0,0,0]),t[0]>.999999&&(t=[1,0,0]),r=a(r),r=o(r),m(r)?v(r):d(t,(e=>o(e=a(e=i(e=p(e))))),(e=>l(e=c(e=u(e=n(e))))))}function S(e,t,n,o){const u=r.stringify(e),a=e.value,s=e.nodes.slice().filter((e=>"comment"!==e.type&&"space"!==e.type));let c,i=null;if("color"===a.toLowerCase()&&(i=function(e){if(!function(e){if(!e||"word"!==e.type)return!1;switch(e.value.toLowerCase()){case"srgb":case"srgb-linear":case"display-p3":case"a98-rgb":case"prophoto-rgb":case"rec2020":case"xyz-d50":case"xyz-d65":case"xyz":return!0;default:return!1}}(e[0]))return null;const t={colorSpace:e[0].value.toLowerCase(),colorSpaceNode:e[0],parameters:[]};for(let n=1;n<e.length;n++)if(P(e[n]))t.slash=e[n];else{if(t.slash&&(C(e[n])||k(e[n])||L(e[n]))){t.alpha=e[n];break}if(!C(e[n]))return null;{const o=r.unit(e[n].value);"%"===o.unit&&(o.number=String(parseFloat(o.number)/100),o.unit="",e[n].value=String(o.number)),t.parameters.push({value:o,node:e[n]})}}if(0===t.parameters.length)return t;t.parameters.length<3&&(t.parameters=[...t.parameters,{node:{sourceIndex:0,sourceEndIndex:1,value:"0",type:"word"},value:{number:"0",unit:""}},{node:{sourceIndex:0,sourceEndIndex:1,value:"0",type:"word"},value:{number:"0",unit:""}}]);t.parameters.length>3&&(t.parameters=t.parameters.slice(0,3));return t}(s)),!i)return;switch(e.value="rgb",function(e,t,n){if(!t||!n)return;if(e.value="rgba",t.value=",",t.before="",!function(e){if(!e||"word"!==e.type)return!1;if(!E(e))return!1;const t=r.unit(e.value);if(!t)return!1;return!!t.number}(n))return;const o=r.unit(n.value);if(!o)return;"%"===o.unit&&(o.number=String(parseFloat(o.number)/100),n.value=String(o.number))}(e,i.slash,i.alpha),i.colorSpace){case"srgb":c=I;break;case"srgb-linear":c=M;break;case"a98-rgb":c=h;break;case"prophoto-rgb":c=w;break;case"display-p3":c=g;break;case"rec2020":c=x;break;case"xyz-d50":c=b;break;case"xyz-d65":case"xyz":c=y;break;default:return}const l=(p=i,p.parameters.map((e=>e.value))).map((e=>parseFloat(e.number)));var p;const f=c(l);!m(l)&&o&&t.warn(n,`"${u}" is out of gamut for "${i.colorSpace}". Given "preserve: true" is set, this will lead to unexpected results in some browsers.`),e.nodes=[{sourceIndex:0,sourceEndIndex:1,value:String(Math.round(255*f[0])),type:"word"},{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""},{sourceIndex:0,sourceEndIndex:1,value:String(Math.round(255*f[1])),type:"word"},{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""},{sourceIndex:0,sourceEndIndex:1,value:String(Math.round(255*f[2])),type:"word"}],i.alpha&&(e.nodes.push({sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),e.nodes.push(i.alpha))}function C(e){if(!e||"word"!==e.type)return!1;if(!E(e))return!1;const t=r.unit(e.value);return!!t&&("%"===t.unit||""===t.unit)}function k(e){return e&&"function"===e.type&&"calc"===e.value.toLowerCase()}function L(e){return e&&"function"===e.type&&"var"===e.value.toLowerCase()}function P(e){return e&&"div"===e.type&&"/"===e.value}function E(e){if(!e||!e.value)return!1;try{return!1!==r.unit(e.value)}catch(e){return!1}}const z=e=>{const t="preserve"in Object(e)&&Boolean(e.preserve);return{postcssPlugin:"postcss-color-function",Declaration:(e,{result:n})=>{if(function(e){const r=e.parent;if(!r)return!1;const t=e.prop.toLowerCase(),n=r.index(e);for(let e=0;e<n;e++){const n=r.nodes[e];if("decl"===n.type&&n.prop.toLowerCase()===t)return!0}return!1}(e))return;if(function(e){let r=e.parent;for(;r;)if("atrule"===r.type){if("supports"===r.name&&-1!==r.params.indexOf("color("))return!0;r=r.parent}else r=r.parent;return!1}(e))return;const o=e.value;if(!o.toLowerCase().includes("color("))return;const u=function(e,t,n,o){let u;try{u=r(e)}catch(r){t.warn(n,`Failed to parse value '${e}' as a color function. Leaving the original value intact.`)}if(void 0===u)return;u.walk((e=>{e.type&&"function"===e.type&&"color"===e.value.toLowerCase()&&S(e,t,n,o)}));const a=String(u);return a!==e?a:void 0}(o,e,n,t);void 0!==u&&(e.cloneBefore({value:u}),t||e.remove())}}};z.postcss=!0;const A=r=>{const t=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},r);return t.enableProgressiveCustomProperties&&t.preserve?{postcssPlugin:"postcss-color-function",plugins:[e(),z(t)]}:z(t)};A.postcss=!0;export{A as default};
29
+ */function deltaEOK(t,e){const[n,r,o]=t,[a,s,i]=e,u=n-a,c=r-s,l=o-i;return Math.sqrt(u**2+c**2+l**2)}function mapGamut(t,e,n){return binarySearchGamut(t,e,n)}function binarySearchGamut(t,e,n){let r=0,o=t[1];const a=t;for(;o-r>1e-4;){const t=clip(e(a));deltaEOK(OKLCH_to_OKLab(a),OKLCH_to_OKLab(n(t)))-.02<1e-4?r=a[1]:o=a[1],a[1]=(o+r)/2}return clip(e([...a]))}function clip(t){return t.map((t=>t<0?0:t>1?1:t))}function inGamut(t){const[e,n,r]=t;return e>=-1e-4&&e<=1.0001&&n>=-1e-4&&n<=1.0001&&r>=-1e-4&&r<=1.0001}function a98RgbToSRgb(t){let e=t.slice();e=e.map((function(t){const e=t<0?-1:1,n=Math.abs(t);return e*Math.pow(n,563/256)})),e=multiplyMatrices([[.5766690429101305,.1855582379065463,.1882286462349947],[.29734497525053605,.6273635662554661,.07529145849399788],[.02703136138641234,.07068885253582723,.9913375368376388]],e);let n=e.slice();return n=XYZ_to_OKLab(n),n=OKLab_to_OKLCH(n),n[0]<1e-6&&(n=[0,0,0]),n[0]>.999999&&(n=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(n,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function cieXyz50ToSRgb(t){let e=t.slice();e=D50_to_D65(e);let n=e.slice();return n=XYZ_to_OKLab(n),n=OKLab_to_OKLCH(n),n[0]<1e-6&&(n=[0,0,0]),n[0]>.999999&&(n=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(n,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function cieXyz65ToSRgb(t){let e=t.slice(),n=e.slice();return n=XYZ_to_OKLab(n),n=OKLab_to_OKLCH(n),n[0]<1e-6&&(n=[0,0,0]),n[0]>.999999&&(n=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(n,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function displayP3ToSRgb(t){let e=t.slice();e=lin_sRGB(e),e=multiplyMatrices([[.4865709486482162,.26566769316909306,.1982172852343625],[.2289745640697488,.6917385218365064,.079286914093745],[0,.04511338185890264,1.043944368900976]],e);let n=e.slice();return n=XYZ_to_OKLab(n),n=OKLab_to_OKLCH(n),n[0]<1e-6&&(n=[0,0,0]),n[0]>.999999&&(n=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(n,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function prophotoRgbToSRgb(t){let e=t.slice();e=e.map((function(t){const e=t<0?-1:1;return Math.abs(t)<=.03125?t/16:e*Math.pow(t,1.8)})),e=multiplyMatrices([[.7977604896723027,.13518583717574031,.0313493495815248],[.2880711282292934,.7118432178101014,8565396060525902e-20],[0,0,.8251046025104601]],e),e=D50_to_D65(e);let n=e.slice();return n=XYZ_to_OKLab(n),n=OKLab_to_OKLCH(n),n[0]<1e-6&&(n=[0,0,0]),n[0]>.999999&&(n=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(n,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function rec2020ToSRgb(t){let e=t.slice();e=lin_2020(e),e=multiplyMatrices([[.6369580483012914,.14461690358620832,.1688809751641721],[.2627002120112671,.6779980715188708,.05930171646986196],[0,.028072693049087428,1.060985057710791]],e);let n=e.slice();return n=XYZ_to_OKLab(n),n=OKLab_to_OKLCH(n),n[0]<1e-6&&(n=[0,0,0]),n[0]>.999999&&(n=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(n,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function sRgbLinearToSRgb(t){let e=t.slice();e=lin_sRGB_to_XYZ(e);let n=e.slice();return n=XYZ_to_OKLab(n),n=OKLab_to_OKLCH(n),n[0]<1e-6&&(n=[0,0,0]),n[0]>.999999&&(n=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(n,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function sRgbToSRgb(t){let e=t.slice();e=lin_sRGB(e),e=lin_sRGB_to_XYZ(e);let n=e.slice();return n=XYZ_to_OKLab(n),n=OKLab_to_OKLCH(n),n[0]<1e-6&&(n=[0,0,0]),n[0]>.999999&&(n=[1,0,0]),e=XYZ_to_lin_sRGB(e),e=gam_sRGB(e),inGamut(e)?clip(e):mapGamut(n,(t=>gam_sRGB(t=XYZ_to_lin_sRGB(t=OKLab_to_XYZ(t=OKLCH_to_OKLab(t))))),(t=>OKLab_to_OKLCH(t=XYZ_to_OKLab(t=lin_sRGB_to_XYZ(t=lin_sRGB(t))))))}function onCSSFunctionSRgb(t,n,r,o){const a=e.stringify(t),s=t.value,i=t.nodes.slice().filter((t=>"comment"!==t.type&&"space"!==t.type));let u,c=null;if("color"===s.toLowerCase()&&(c=colorFunctionContents(i)),!c)return;switch(t.value="rgb",transformAlpha(t,c.slash,c.alpha),c.colorSpace){case"srgb":u=sRgbToSRgb;break;case"srgb-linear":u=sRgbLinearToSRgb;break;case"a98-rgb":u=a98RgbToSRgb;break;case"prophoto-rgb":u=prophotoRgbToSRgb;break;case"display-p3":u=displayP3ToSRgb;break;case"rec2020":u=rec2020ToSRgb;break;case"xyz-d50":u=cieXyz50ToSRgb;break;case"xyz-d65":case"xyz":u=cieXyz65ToSRgb;break;default:return}const l=(_=c,_.parameters.map((t=>t.value))).map((t=>parseFloat(t.number)));var _;const p=u(l);!inGamut(l)&&o&&n.warn(r,`"${a}" is out of gamut for "${c.colorSpace}". Given "preserve: true" is set, this will lead to unexpected results in some browsers.`),t.nodes=[{sourceIndex:0,sourceEndIndex:1,value:String(Math.round(255*p[0])),type:"word"},{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""},{sourceIndex:0,sourceEndIndex:1,value:String(Math.round(255*p[1])),type:"word"},{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""},{sourceIndex:0,sourceEndIndex:1,value:String(Math.round(255*p[2])),type:"word"}],c.alpha&&(t.nodes.push({sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),t.nodes.push(c.alpha))}function isColorSpaceNode(t){if(!t||"word"!==t.type)return!1;switch(t.value.toLowerCase()){case"srgb":case"srgb-linear":case"display-p3":case"a98-rgb":case"prophoto-rgb":case"rec2020":case"xyz-d50":case"xyz-d65":case"xyz":return!0;default:return!1}}function isNumericNode(t){if(!t||"word"!==t.type)return!1;if(!canParseAsUnit(t))return!1;const n=e.unit(t.value);return!!n&&!!n.number}function isNumericNodePercentageOrNumber(t){if(!t||"word"!==t.type)return!1;if(!canParseAsUnit(t))return!1;const n=e.unit(t.value);return!!n&&("%"===n.unit||""===n.unit)}function isCalcNode(t){return t&&"function"===t.type&&"calc"===t.value.toLowerCase()}function isVarNode(t){return t&&"function"===t.type&&"var"===t.value.toLowerCase()}function colorFunctionContents(t){if(!isColorSpaceNode(t[0]))return null;const n={colorSpace:t[0].value.toLowerCase(),colorSpaceNode:t[0],parameters:[]};for(let o=1;o<t.length;o++)if((r=t[o])&&"div"===r.type&&"/"===r.value)n.slash=t[o];else{if(n.slash&&(isNumericNodePercentageOrNumber(t[o])||isCalcNode(t[o])||isVarNode(t[o]))){n.alpha=t[o];break}if(!isNumericNodePercentageOrNumber(t[o]))return null;{const r=e.unit(t[o].value);"%"===r.unit&&(r.number=String(parseFloat(r.number)/100),r.unit="",t[o].value=String(r.number)),n.parameters.push({value:r,node:t[o]})}}var r;return 0===n.parameters.length||(n.parameters.length<3&&(n.parameters=[...n.parameters,{node:{sourceIndex:0,sourceEndIndex:1,value:"0",type:"word"},value:{number:"0",unit:""}},{node:{sourceIndex:0,sourceEndIndex:1,value:"0",type:"word"},value:{number:"0",unit:""}}]),n.parameters.length>3&&(n.parameters=n.parameters.slice(0,3))),n}function transformAlpha(t,n,r){if(!n||!r)return;if(t.value="rgba",n.value=",",n.before="",!isNumericNode(r))return;const o=e.unit(r.value);o&&"%"===o.unit&&(o.number=String(parseFloat(o.number)/100),r.value=String(o.number))}function canParseAsUnit(t){if(!t||!t.value)return!1;try{return!1!==e.unit(t.value)}catch(t){return!1}}function modifiedValues(t,n,r,o){let a;try{a=e(t)}catch(e){n.warn(r,`Failed to parse value '${t}' as a color function. Leaving the original value intact.`)}if(void 0===a)return;a.walk((t=>{t.type&&"function"===t.type&&"color"===t.value.toLowerCase()&&onCSSFunctionSRgb(t,n,r,o)}));const s=String(a);return s!==t?s:void 0}const basePlugin=t=>{const e="preserve"in Object(t)&&Boolean(t.preserve);return{postcssPlugin:"postcss-color-function",Declaration:(t,{result:n})=>{if(hasFallback(t))return;if(hasSupportsAtRuleAncestor(t))return;const r=t.value;if(!r.toLowerCase().includes("color("))return;const o=modifiedValues(r,t,n,e);void 0!==o&&(t.cloneBefore({value:o}),e||t.remove())}}};basePlugin.postcss=!0;const postcssPlugin=e=>{const n=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},e);return n.enableProgressiveCustomProperties&&n.preserve?{postcssPlugin:"postcss-color-function",plugins:[t(),basePlugin(n)]}:basePlugin(n)};postcssPlugin.postcss=!0;export{postcssPlugin as default};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@csstools/postcss-color-function",
3
3
  "description": "Use the color() function in CSS",
4
- "version": "1.1.1",
4
+ "version": "2.0.0",
5
5
  "author": "Jonathan Neal <jonathantneal@hotmail.com>",
6
6
  "license": "CC0-1.0",
7
7
  "funding": {
@@ -9,7 +9,7 @@
9
9
  "url": "https://opencollective.com/csstools"
10
10
  },
11
11
  "engines": {
12
- "node": "^12 || ^14 || >=16"
12
+ "node": "^14 || ^16 || >=18"
13
13
  },
14
14
  "main": "dist/index.cjs",
15
15
  "module": "dist/index.mjs",
@@ -28,18 +28,19 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@csstools/postcss-progressive-custom-properties": "^1.1.0",
31
+ "@csstools/postcss-progressive-custom-properties": "^2.0.0",
32
32
  "postcss-value-parser": "^4.2.0"
33
33
  },
34
34
  "peerDependencies": {
35
- "postcss": "^8.2"
35
+ "postcss": "^8.4"
36
36
  },
37
37
  "devDependencies": {
38
38
  "postcss-lab-function": "^4.0.3"
39
39
  },
40
40
  "scripts": {
41
- "build": "rollup -c ../../rollup/default.js",
42
- "clean": "node -e \"fs.rmSync('./dist', { recursive: true, force: true });\"",
41
+ "prebuild": "npm run clean",
42
+ "build": "rollup -c ../../rollup/default.mjs",
43
+ "clean": "node -e \"fs.rmSync('./dist', { recursive: true, force: true }); fs.mkdirSync('./dist');\"",
43
44
  "docs": "node ../../.github/bin/generate-docs/install.mjs && node ../../.github/bin/generate-docs/readme.mjs",
44
45
  "lint": "npm run lint:eslint && npm run lint:package-json",
45
46
  "lint:eslint": "eslint ./src --ext .js --ext .ts --ext .mjs --no-error-on-unmatched-pattern",
@@ -58,8 +59,6 @@
58
59
  "bugs": "https://github.com/csstools/postcss-plugins/issues",
59
60
  "keywords": [
60
61
  "color",
61
- "color",
62
- "colors",
63
62
  "css",
64
63
  "design",
65
64
  "display-p3",