@csstools/postcss-color-function 1.0.1 → 1.0.2

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
+ ### 1.0.2 (February 12, 2022)
4
+
5
+ - Updated `@csstools/postcss-progressive-custom-properties` to `1.1.0`.
6
+
3
7
  ### 1.0.1 (February 11, 2022)
4
8
 
5
9
  - Add tests for percentage values in non-xyz color spaces.
package/README.md CHANGED
@@ -75,7 +75,7 @@ postcssColorFunction({ preserve: true })
75
75
  The `enableProgressiveCustomProperties` option determines whether the original notation
76
76
  is wrapped with `@supports` when used in Custom Properties. By default, it is enabled.
77
77
 
78
- ⚠️ Only disabled this when you set `preserve` to `false` or if you bring your own fix for Custom Properties.
78
+ ⚠️ We only recommend disabling this when you set `preserve` to `false` or if you bring your own fix for Custom Properties. See what the plugin does in its [README](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-progressive-custom-properties#readme).
79
79
 
80
80
  ```js
81
81
  postcssColorFunction({ enableProgressiveCustomProperties: false })
package/dist/index.cjs CHANGED
@@ -27,4 +27,4 @@ function a(e,t){const r=e.length;let n,u;n=Array.isArray(e[0])?e:[e],Array.isArr
27
27
  * @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
28
  *
29
29
  * @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/deltaEOK.js
30
- */function h(e,t){const[r,n,u]=e,[a,o,s]=t,c=r-a,i=n-o,l=u-s;return Math.sqrt(c**2+i**2+l**2)}function m(e,t,r){return function(e,t,r){let n=0,u=e[1];const a=e;for(;u-n>1e-4;){h(a,r(b(t(a))))-.02<1e-4?n=a[1]:u=a[1],a[1]=(u+n)/2}return b(t([...a]))}(e,t,r)}function b(e){return e.map((e=>e<0?0:e>1?1:e))}function y(e){const[t,r,n]=e;return t>=-1e-4&&t<=1.0001&&r>=-1e-4&&r<=1.0001&&n>=-1e-4&&n<=1.0001}function g(e){let t=e.slice();t=t.map((function(e){const t=e<0?-1:1,r=Math.abs(e);return t*Math.pow(r,563/256)})),t=a([[.5766690429101305,.1855582379065463,.1882286462349947],[.29734497525053605,.6273635662554661,.07529145849399788],[.02703136138641234,.07068885253582723,.9913375368376388]],t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function x(e){let t=e.slice();t=l(t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function M(e){let t=e.slice(),r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function w(e){let t=e.slice();t=o(t),t=a([[.4865709486482162,.26566769316909306,.1982172852343625],[.2289745640697488,.6917385218365064,.079286914093745],[0,.04511338185890264,1.043944368900976]],t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function I(e){let t=e.slice();t=t.map((function(e){const t=e<0?-1:1;return Math.abs(e)<=.03125?e/16:t*Math.pow(e,1.8)})),t=a([[.7977604896723027,.13518583717574031,.0313493495815248],[.2880711282292934,.7118432178101014,8565396060525902e-20],[0,0,.8251046025104601]],t),t=l(t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function S(e){let t=e.slice();t=function(e){const t=1.09929682680944;return e.map((function(e){const r=e<0?-1:1,n=Math.abs(e);return n<.08124285829863151?e/4.5:r*Math.pow((n+t-1)/t,1/.45)}))}(t),t=a([[.6369580483012914,.14461690358620832,.1688809751641721],[.2627002120112671,.6779980715188708,.05930171646986196],[0,.028072693049087428,1.060985057710791]],t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function k(e){let t=e.slice();t=c(t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function P(e){let t=e.slice();t=o(t),t=c(t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function z(e,t,r,n){const a=u.default.stringify(e),o=e.value,s=e.nodes.slice().filter((e=>"comment"!==e.type&&"space"!==e.type));let c,i=null;if("color"===o&&(i=function(e){if(!function(e){if(!e||"word"!==e.type)return!1;switch(e.value){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,colorSpaceNode:e[0],parameters:[]};for(let r=1;r<e.length;r++)if(F(e[r]))t.slash=e[r];else{if(t.slash&&(E(e[r])||A(e[r])||q(e[r]))){t.alpha=e[r];break}if(!t.colorSpace.startsWith("xyz")&&E(e[r])){const n=u.default.unit(e[r].value);"%"===n.unit&&(n.number=String(parseFloat(n.number)/100),n.unit="",e[r].value=String(n.number)),t.parameters.push({value:n,node:e[r]})}else{if(!t.colorSpace.startsWith("xyz")||!E(e[r]))return null;{const n=u.default.unit(e[r].value);if(""!==n.unit)return null;t.parameters.push({value:n,node:e[r]})}}}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,r){if(!t||!r)return;if(e.value="rgba",t.value=",",t.before="",!function(e){if(!e||"word"!==e.type)return!1;if(!O(e))return!1;const t=u.default.unit(e.value);if(!t)return!1;return!!t.number}(r))return;const n=u.default.unit(r.value);if(!n)return;"%"===n.unit&&(n.number=String(parseFloat(n.number)/100),r.value=String(n.number))}(e,i.slash,i.alpha),i.colorSpace){case"srgb":c=P;break;case"srgb-linear":c=k;break;case"a98-rgb":c=g;break;case"prophoto-rgb":c=I;break;case"display-p3":c=w;break;case"rec2020":c=S;break;case"xyz-d50":c=x;break;case"xyz-d65":case"xyz":c=M;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&&t.warn(r,`"${a}" is out of gamut for "${i.colorSpace}". When "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 E(e){if(!e||"word"!==e.type)return!1;if(!O(e))return!1;const t=u.default.unit(e.value);return!!t&&("%"===t.unit||""===t.unit)}function A(e){return e&&"function"===e.type&&"calc"===e.value}function q(e){return e&&"function"===e.type&&"var"===e.value}function F(e){return e&&"div"===e.type&&"/"===e.value}function O(e){if(!e||!e.value)return!1;try{return!1!==u.default.unit(e.value)}catch(e){return!1}}const j=e=>{const t="preserve"in Object(e)&&Boolean(e.preserve);return{postcssPlugin:"postcss-color-function",Declaration:(e,{result:r})=>{if(function(e){const t=e.parent;if(!t)return!1;const r=t.index(e);for(let n=0;n<r;n++){const r=t.nodes[n];if("decl"===r.type&&r.prop===e.prop)return!0}return!1}(e))return;if(function(e){let t=e.parent;for(;t;)if("atrule"===t.type){if("supports"===t.name&&-1!==t.params.indexOf("color("))return!0;t=t.parent}else t=t.parent;return!1}(e))return;const n=e.value;if(-1===n.indexOf("color("))return;const a=function(e,t,r,n){let a;try{a=u.default(e)}catch(n){t.warn(r,`Failed to parse value '${e}' as a color function. Leaving the original value intact.`)}if(void 0===a)return;a.walk((e=>{e.type&&"function"===e.type&&"color"===e.value&&z(e,t,r,n)}));const o=String(a);return o!==e?o:void 0}(n,e,r,t);void 0!==a&&(t?e.cloneBefore({value:a}):e.value=a)}}};j.postcss=!0;const W=e=>{const t=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},e);return t.enableProgressiveCustomProperties&&t.preserve?{postcssPlugin:"postcss-color-function",plugins:[n.default(),j(t)]}:j(t)};W.postcss=!0,module.exports=W;
30
+ */function h(e,t){const[r,n,u]=e,[a,o,s]=t,c=r-a,i=n-o,l=u-s;return Math.sqrt(c**2+i**2+l**2)}function m(e,t,r){return function(e,t,r){let n=0,u=e[1];const a=e;for(;u-n>1e-4;){h(a,r(b(t(a))))-.02<1e-4?n=a[1]:u=a[1],a[1]=(u+n)/2}return b(t([...a]))}(e,t,r)}function b(e){return e.map((e=>e<0?0:e>1?1:e))}function y(e){const[t,r,n]=e;return t>=-1e-4&&t<=1.0001&&r>=-1e-4&&r<=1.0001&&n>=-1e-4&&n<=1.0001}function g(e){let t=e.slice();t=t.map((function(e){const t=e<0?-1:1,r=Math.abs(e);return t*Math.pow(r,563/256)})),t=a([[.5766690429101305,.1855582379065463,.1882286462349947],[.29734497525053605,.6273635662554661,.07529145849399788],[.02703136138641234,.07068885253582723,.9913375368376388]],t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function x(e){let t=e.slice();t=l(t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function M(e){let t=e.slice(),r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function w(e){let t=e.slice();t=o(t),t=a([[.4865709486482162,.26566769316909306,.1982172852343625],[.2289745640697488,.6917385218365064,.079286914093745],[0,.04511338185890264,1.043944368900976]],t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function I(e){let t=e.slice();t=t.map((function(e){const t=e<0?-1:1;return Math.abs(e)<=.03125?e/16:t*Math.pow(e,1.8)})),t=a([[.7977604896723027,.13518583717574031,.0313493495815248],[.2880711282292934,.7118432178101014,8565396060525902e-20],[0,0,.8251046025104601]],t),t=l(t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function S(e){let t=e.slice();t=function(e){const t=1.09929682680944;return e.map((function(e){const r=e<0?-1:1,n=Math.abs(e);return n<.08124285829863151?e/4.5:r*Math.pow((n+t-1)/t,1/.45)}))}(t),t=a([[.6369580483012914,.14461690358620832,.1688809751641721],[.2627002120112671,.6779980715188708,.05930171646986196],[0,.028072693049087428,1.060985057710791]],t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function k(e){let t=e.slice();t=c(t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function P(e){let t=e.slice();t=o(t),t=c(t);let r=t.slice();return r=p(r),r=d(r),t=i(t),t=s(t),y(t)?b(t):m(r,(e=>s(e=i(e=f(e=v(e))))),(e=>d(e=p(e=c(e=o(e))))))}function z(e,t,r,n){const a=u.default.stringify(e),o=e.value,s=e.nodes.slice().filter((e=>"comment"!==e.type&&"space"!==e.type));let c,i=null;if("color"===o&&(i=function(e){if(!function(e){if(!e||"word"!==e.type)return!1;switch(e.value){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,colorSpaceNode:e[0],parameters:[]};for(let r=1;r<e.length;r++)if(F(e[r]))t.slash=e[r];else{if(t.slash&&(E(e[r])||A(e[r])||q(e[r]))){t.alpha=e[r];break}if(!t.colorSpace.startsWith("xyz")&&E(e[r])){const n=u.default.unit(e[r].value);"%"===n.unit&&(n.number=String(parseFloat(n.number)/100),n.unit="",e[r].value=String(n.number)),t.parameters.push({value:n,node:e[r]})}else{if(!t.colorSpace.startsWith("xyz")||!E(e[r]))return null;{const n=u.default.unit(e[r].value);if(""!==n.unit)return null;t.parameters.push({value:n,node:e[r]})}}}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,r){if(!t||!r)return;if(e.value="rgba",t.value=",",t.before="",!function(e){if(!e||"word"!==e.type)return!1;if(!O(e))return!1;const t=u.default.unit(e.value);if(!t)return!1;return!!t.number}(r))return;const n=u.default.unit(r.value);if(!n)return;"%"===n.unit&&(n.number=String(parseFloat(n.number)/100),r.value=String(n.number))}(e,i.slash,i.alpha),i.colorSpace){case"srgb":c=P;break;case"srgb-linear":c=k;break;case"a98-rgb":c=g;break;case"prophoto-rgb":c=I;break;case"display-p3":c=w;break;case"rec2020":c=S;break;case"xyz-d50":c=x;break;case"xyz-d65":case"xyz":c=M;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&&t.warn(r,`"${a}" 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 E(e){if(!e||"word"!==e.type)return!1;if(!O(e))return!1;const t=u.default.unit(e.value);return!!t&&("%"===t.unit||""===t.unit)}function A(e){return e&&"function"===e.type&&"calc"===e.value}function q(e){return e&&"function"===e.type&&"var"===e.value}function F(e){return e&&"div"===e.type&&"/"===e.value}function O(e){if(!e||!e.value)return!1;try{return!1!==u.default.unit(e.value)}catch(e){return!1}}const j=e=>{const t="preserve"in Object(e)&&Boolean(e.preserve);return{postcssPlugin:"postcss-color-function",Declaration:(e,{result:r})=>{if(function(e){const t=e.parent;if(!t)return!1;const r=t.index(e);for(let n=0;n<r;n++){const r=t.nodes[n];if("decl"===r.type&&r.prop===e.prop)return!0}return!1}(e))return;if(function(e){let t=e.parent;for(;t;)if("atrule"===t.type){if("supports"===t.name&&-1!==t.params.indexOf("color("))return!0;t=t.parent}else t=t.parent;return!1}(e))return;const n=e.value;if(-1===n.indexOf("color("))return;const a=function(e,t,r,n){let a;try{a=u.default(e)}catch(n){t.warn(r,`Failed to parse value '${e}' as a color function. Leaving the original value intact.`)}if(void 0===a)return;a.walk((e=>{e.type&&"function"===e.type&&"color"===e.value&&z(e,t,r,n)}));const o=String(a);return o!==e?o:void 0}(n,e,r,t);void 0!==a&&(t?e.cloneBefore({value:a}):e.value=a)}}};j.postcss=!0;const $=e=>{const t=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},e);return t.enableProgressiveCustomProperties&&t.preserve?{postcssPlugin:"postcss-color-function",plugins:[n.default(),j(t)]}:j(t)};$.postcss=!0,module.exports=$;
package/dist/index.mjs CHANGED
@@ -27,4 +27,4 @@ function t(e,r){const t=e.length;let n,u;n=Array.isArray(e[0])?e:[e],Array.isArr
27
27
  * @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
28
  *
29
29
  * @see https://github.com/w3c/csswg-drafts/blob/main/css-color-4/deltaEOK.js
30
- */function f(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 d(e,r,t){return function(e,r,t){let n=0,u=e[1];const o=e;for(;u-n>1e-4;){f(o,t(v(r(o))))-.02<1e-4?n=o[1]:u=o[1],o[1]=(u+n)/2}return v(r([...o]))}(e,r,t)}function v(e){return e.map((e=>e<0?0:e>1?1:e))}function h(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 m(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),r=a(r),r=u(r),h(r)?v(r):d(s,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function b(e){let r=e.slice();r=s(r);let t=r.slice();return t=c(t),t=l(t),r=a(r),r=u(r),h(r)?v(r):d(t,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function y(e){let r=e.slice(),t=r.slice();return t=c(t),t=l(t),r=a(r),r=u(r),h(r)?v(r):d(t,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(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),r=a(r),r=u(r),h(r)?v(r):d(s,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function x(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),r=a(r),r=u(r),h(r)?v(r):d(f,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function M(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),r=a(r),r=u(r),h(r)?v(r):d(s,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function w(e){let r=e.slice();r=o(r);let t=r.slice();return t=c(t),t=l(t),r=a(r),r=u(r),h(r)?v(r):d(t,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function I(e){let r=e.slice();r=n(r),r=o(r);let t=r.slice();return t=c(t),t=l(t),r=a(r),r=u(r),h(r)?v(r):d(t,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function S(e,t,n,u){const o=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&&(i=function(e){if(!function(e){if(!e||"word"!==e.type)return!1;switch(e.value){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,colorSpaceNode:e[0],parameters:[]};for(let n=1;n<e.length;n++)if(E(e[n]))t.slash=e[n];else{if(t.slash&&(k(e[n])||P(e[n])||z(e[n]))){t.alpha=e[n];break}if(!t.colorSpace.startsWith("xyz")&&k(e[n])){const u=r.unit(e[n].value);"%"===u.unit&&(u.number=String(parseFloat(u.number)/100),u.unit="",e[n].value=String(u.number)),t.parameters.push({value:u,node:e[n]})}else{if(!t.colorSpace.startsWith("xyz")||!k(e[n]))return null;{const u=r.unit(e[n].value);if(""!==u.unit)return null;t.parameters.push({value:u,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(!A(e))return!1;const t=r.unit(e.value);if(!t)return!1;return!!t.number}(n))return;const u=r.unit(n.value);if(!u)return;"%"===u.unit&&(u.number=String(parseFloat(u.number)/100),n.value=String(u.number))}(e,i.slash,i.alpha),i.colorSpace){case"srgb":c=I;break;case"srgb-linear":c=w;break;case"a98-rgb":c=m;break;case"prophoto-rgb":c=x;break;case"display-p3":c=g;break;case"rec2020":c=M;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);!h(l)&&u&&t.warn(n,`"${o}" is out of gamut for "${i.colorSpace}". When "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 k(e){if(!e||"word"!==e.type)return!1;if(!A(e))return!1;const t=r.unit(e.value);return!!t&&("%"===t.unit||""===t.unit)}function P(e){return e&&"function"===e.type&&"calc"===e.value}function z(e){return e&&"function"===e.type&&"var"===e.value}function E(e){return e&&"div"===e.type&&"/"===e.value}function A(e){if(!e||!e.value)return!1;try{return!1!==r.unit(e.value)}catch(e){return!1}}const F=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=r.index(e);for(let n=0;n<t;n++){const t=r.nodes[n];if("decl"===t.type&&t.prop===e.prop)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 u=e.value;if(-1===u.indexOf("color("))return;const o=function(e,t,n,u){let o;try{o=r(e)}catch(r){t.warn(n,`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&&S(e,t,n,u)}));const a=String(o);return a!==e?a:void 0}(u,e,n,t);void 0!==o&&(t?e.cloneBefore({value:o}):e.value=o)}}};F.postcss=!0;const O=r=>{const t=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},r);return t.enableProgressiveCustomProperties&&t.preserve?{postcssPlugin:"postcss-color-function",plugins:[e(),F(t)]}:F(t)};O.postcss=!0;export{O as default};
30
+ */function f(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 d(e,r,t){return function(e,r,t){let n=0,u=e[1];const o=e;for(;u-n>1e-4;){f(o,t(v(r(o))))-.02<1e-4?n=o[1]:u=o[1],o[1]=(u+n)/2}return v(r([...o]))}(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),r=a(r),r=u(r),m(r)?v(r):d(s,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function b(e){let r=e.slice();r=s(r);let t=r.slice();return t=c(t),t=l(t),r=a(r),r=u(r),m(r)?v(r):d(t,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function y(e){let r=e.slice(),t=r.slice();return t=c(t),t=l(t),r=a(r),r=u(r),m(r)?v(r):d(t,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(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),r=a(r),r=u(r),m(r)?v(r):d(s,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function x(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),r=a(r),r=u(r),m(r)?v(r):d(f,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function M(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),r=a(r),r=u(r),m(r)?v(r):d(s,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function w(e){let r=e.slice();r=o(r);let t=r.slice();return t=c(t),t=l(t),r=a(r),r=u(r),m(r)?v(r):d(t,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function I(e){let r=e.slice();r=n(r),r=o(r);let t=r.slice();return t=c(t),t=l(t),r=a(r),r=u(r),m(r)?v(r):d(t,(e=>u(e=a(e=i(e=p(e))))),(e=>l(e=c(e=o(e=n(e))))))}function S(e,t,n,u){const o=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&&(i=function(e){if(!function(e){if(!e||"word"!==e.type)return!1;switch(e.value){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,colorSpaceNode:e[0],parameters:[]};for(let n=1;n<e.length;n++)if(E(e[n]))t.slash=e[n];else{if(t.slash&&(k(e[n])||P(e[n])||z(e[n]))){t.alpha=e[n];break}if(!t.colorSpace.startsWith("xyz")&&k(e[n])){const u=r.unit(e[n].value);"%"===u.unit&&(u.number=String(parseFloat(u.number)/100),u.unit="",e[n].value=String(u.number)),t.parameters.push({value:u,node:e[n]})}else{if(!t.colorSpace.startsWith("xyz")||!k(e[n]))return null;{const u=r.unit(e[n].value);if(""!==u.unit)return null;t.parameters.push({value:u,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(!A(e))return!1;const t=r.unit(e.value);if(!t)return!1;return!!t.number}(n))return;const u=r.unit(n.value);if(!u)return;"%"===u.unit&&(u.number=String(parseFloat(u.number)/100),n.value=String(u.number))}(e,i.slash,i.alpha),i.colorSpace){case"srgb":c=I;break;case"srgb-linear":c=w;break;case"a98-rgb":c=h;break;case"prophoto-rgb":c=x;break;case"display-p3":c=g;break;case"rec2020":c=M;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)&&u&&t.warn(n,`"${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 k(e){if(!e||"word"!==e.type)return!1;if(!A(e))return!1;const t=r.unit(e.value);return!!t&&("%"===t.unit||""===t.unit)}function P(e){return e&&"function"===e.type&&"calc"===e.value}function z(e){return e&&"function"===e.type&&"var"===e.value}function E(e){return e&&"div"===e.type&&"/"===e.value}function A(e){if(!e||!e.value)return!1;try{return!1!==r.unit(e.value)}catch(e){return!1}}const F=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=r.index(e);for(let n=0;n<t;n++){const t=r.nodes[n];if("decl"===t.type&&t.prop===e.prop)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 u=e.value;if(-1===u.indexOf("color("))return;const o=function(e,t,n,u){let o;try{o=r(e)}catch(r){t.warn(n,`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&&S(e,t,n,u)}));const a=String(o);return a!==e?a:void 0}(u,e,n,t);void 0!==o&&(t?e.cloneBefore({value:o}):e.value=o)}}};F.postcss=!0;const O=r=>{const t=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},r);return t.enableProgressiveCustomProperties&&t.preserve?{postcssPlugin:"postcss-color-function",plugins:[e(),F(t)]}:F(t)};O.postcss=!0;export{O as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@csstools/postcss-color-function",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Use the color() function in CSS",
5
5
  "author": "Jonathan Neal <jonathantneal@hotmail.com>",
6
6
  "license": "CC0-1.0",
@@ -36,7 +36,7 @@
36
36
  "node": "^12 || ^14 || >=16"
37
37
  },
38
38
  "dependencies": {
39
- "@csstools/postcss-progressive-custom-properties": "^1.0.0",
39
+ "@csstools/postcss-progressive-custom-properties": "^1.1.0",
40
40
  "postcss-value-parser": "^4.2.0"
41
41
  },
42
42
  "devDependencies": {