From b29824020a0ea7e511446973fb088dddb859bde0 Mon Sep 17 00:00:00 2001 From: Romain Menke <11521496+romainmenke@users.noreply.github.com> Date: Sun, 18 Aug 2024 17:48:00 +0200 Subject: [PATCH] css-calc: `rawPercentages` (#1461) --- packages/css-calc/CHANGELOG.md | 2 + packages/css-calc/dist/index.cjs | 2 +- packages/css-calc/dist/index.d.ts | 7 + packages/css-calc/dist/index.mjs | 2 +- packages/css-calc/docs/css-calc.api.json | 2 +- .../docs/css-calc.conversionoptions.md | 1 + packages/css-calc/src/functions/abs.ts | 11 +- packages/css-calc/src/functions/calc.ts | 131 +++++------ packages/css-calc/src/functions/clamp.ts | 9 +- packages/css-calc/src/functions/hypot.ts | 9 +- packages/css-calc/src/functions/max.ts | 9 +- packages/css-calc/src/functions/min.ts | 9 +- packages/css-calc/src/functions/round.ts | 9 +- packages/css-calc/src/functions/sign.ts | 11 +- packages/css-calc/src/index.ts | 2 +- packages/css-calc/src/options.ts | 8 + packages/css-calc/test/wpt/invalid.mjs | 2 + .../css-calc/test/wpt/max-20-arguments.mjs | 4 +- .../test/wpt/minmax-percentage-computed.mjs | 212 ++++++++++++------ packages/css-calc/test/wpt/round-function.mjs | 25 +++ packages/css-color-parser/dist/index.cjs | 2 +- packages/css-color-parser/dist/index.mjs | 2 +- .../src/functions/color-mix.ts | 1 + .../css-color-parser/src/functions/color.ts | 1 + .../functions/three-channel-legacy-syntax.ts | 1 + .../three-channel-space-separated.ts | 1 + .../src/static/js/blog_calc_2023_02_21.js | 2 +- 27 files changed, 319 insertions(+), 158 deletions(-) diff --git a/packages/css-calc/CHANGELOG.md b/packages/css-calc/CHANGELOG.md index 946c034be..3ca2ef4c7 100644 --- a/packages/css-calc/CHANGELOG.md +++ b/packages/css-calc/CHANGELOG.md @@ -2,6 +2,8 @@ ### Unreleased (patch) +- Add `rawPercentages` option to indicate that percentage values do not resolve against external values. +- Skip some calculations for values with percentages as those only have a known positive or negative value in a browser context. - Updated [`@csstools/css-tokenizer`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-tokenizer) to [`3.0.1`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-tokenizer/CHANGELOG.md#301) (patch) - Updated [`@csstools/css-parser-algorithms`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-parser-algorithms) to [`3.0.1`](https://github.com/csstools/postcss-plugins/tree/main/packages/css-parser-algorithms/CHANGELOG.md#301) (patch) diff --git a/packages/css-calc/dist/index.cjs b/packages/css-calc/dist/index.cjs index a94f02233..a8b7584c7 100644 --- a/packages/css-calc/dist/index.cjs +++ b/packages/css-calc/dist/index.cjs @@ -1 +1 @@ -"use strict";var e=require("@csstools/css-parser-algorithms"),n=require("@csstools/css-tokenizer");const t=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(t,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}const o={cm:"px",in:"px",mm:"px",pc:"px",pt:"px",px:"px",q:"px",deg:"deg",grad:"deg",rad:"deg",turn:"deg",ms:"s",s:"s",hz:"hz",khz:"hz"},r=new Map([["cm",e=>e],["mm",e=>10*e],["q",e=>40*e],["in",e=>e/2.54],["pc",e=>e/2.54*6],["pt",e=>e/2.54*72],["px",e=>e/2.54*96]]),i=new Map([["deg",e=>e],["grad",e=>e/.9],["rad",e=>e/180*Math.PI],["turn",e=>e/360]]),u=new Map([["deg",e=>.9*e],["grad",e=>e],["rad",e=>.9*e/180*Math.PI],["turn",e=>.9*e/360]]),a=new Map([["hz",e=>e],["khz",e=>e/1e3]]),s=new Map([["cm",e=>2.54*e],["mm",e=>25.4*e],["q",e=>25.4*e*4],["in",e=>e],["pc",e=>6*e],["pt",e=>72*e],["px",e=>96*e]]),l=new Map([["hz",e=>1e3*e],["khz",e=>e]]),c=new Map([["cm",e=>e/10],["mm",e=>e],["q",e=>4*e],["in",e=>e/25.4],["pc",e=>e/25.4*6],["pt",e=>e/25.4*72],["px",e=>e/25.4*96]]),T=new Map([["ms",e=>e],["s",e=>e/1e3]]),p=new Map([["cm",e=>e/6*2.54],["mm",e=>e/6*25.4],["q",e=>e/6*25.4*4],["in",e=>e/6],["pc",e=>e],["pt",e=>e/6*72],["px",e=>e/6*96]]),m=new Map([["cm",e=>e/72*2.54],["mm",e=>e/72*25.4],["q",e=>e/72*25.4*4],["in",e=>e/72],["pc",e=>e/72*6],["pt",e=>e],["px",e=>e/72*96]]),v=new Map([["cm",e=>e/96*2.54],["mm",e=>e/96*25.4],["q",e=>e/96*25.4*4],["in",e=>e/96],["pc",e=>e/96*6],["pt",e=>e/96*72],["px",e=>e]]),N=new Map([["cm",e=>e/4/10],["mm",e=>e/4],["q",e=>e],["in",e=>e/4/25.4],["pc",e=>e/4/25.4*6],["pt",e=>e/4/25.4*72],["px",e=>e/4/25.4*96]]),f=new Map([["deg",e=>180*e/Math.PI],["grad",e=>180*e/Math.PI/.9],["rad",e=>e],["turn",e=>180*e/Math.PI/360]]),k=new Map([["ms",e=>1e3*e],["s",e=>e]]),d=new Map([["deg",e=>360*e],["grad",e=>360*e/.9],["rad",e=>360*e/180*Math.PI],["turn",e=>e]]),b=new Map([["cm",r],["mm",c],["q",N],["in",s],["pc",p],["pt",m],["px",v],["ms",T],["s",k],["deg",i],["grad",u],["rad",f],["turn",d],["hz",a],["khz",l]]);function convertUnit(e,t){if(!n.isTokenDimension(e))return t;if(!n.isTokenDimension(t))return t;const o=toLowerCaseAZ(e[4].unit),r=toLowerCaseAZ(t[4].unit);if(o===r)return t;const i=b.get(r);if(!i)return t;const u=i.get(o);if(!u)return t;const a=u(t[4].value),s=[n.TokenType.Dimension,"",t[2],t[3],{...t[4],signCharacter:a<0?"-":void 0,type:Number.isInteger(a)?n.NumberType.Integer:n.NumberType.Number,value:a}];return n.mutateUnit(s,e[4].unit),s}function toCanonicalUnit(e){if(!n.isTokenDimension(e))return e;const t=toLowerCaseAZ(e[4].unit),r=o[t];if(t===r)return e;const i=b.get(t);if(!i)return e;const u=i.get(r);if(!u)return e;const a=u(e[4].value),s=[n.TokenType.Dimension,"",e[2],e[3],{...e[4],signCharacter:a<0?"-":void 0,type:Number.isInteger(a)?n.NumberType.Integer:n.NumberType.Number,value:a}];return n.mutateUnit(s,r),s}function addition(t){if(2!==t.length)return-1;const o=t[0].value;let r=t[1].value;if(n.isTokenNumber(o)&&n.isTokenNumber(r)){const t=o[4].value+r[4].value;return new e.TokenNode([n.TokenType.Number,t.toString(),o[2],r[3],{value:t,type:o[4].type===n.NumberType.Integer&&r[4].type===n.NumberType.Integer?n.NumberType.Integer:n.NumberType.Number}])}if(n.isTokenPercentage(o)&&n.isTokenPercentage(r)){const t=o[4].value+r[4].value;return new e.TokenNode([n.TokenType.Percentage,t.toString()+"%",o[2],r[3],{value:t}])}if(n.isTokenDimension(o)&&n.isTokenDimension(r)&&(r=convertUnit(o,r),toLowerCaseAZ(o[4].unit)===toLowerCaseAZ(r[4].unit))){const t=o[4].value+r[4].value;return new e.TokenNode([n.TokenType.Dimension,t.toString()+o[4].unit,o[2],r[3],{value:t,type:o[4].type===n.NumberType.Integer&&r[4].type===n.NumberType.Integer?n.NumberType.Integer:n.NumberType.Number,unit:o[4].unit}])}return-1}function division(t){if(2!==t.length)return-1;const o=t[0].value,r=t[1].value;if(n.isTokenNumber(o)&&n.isTokenNumber(r)){const t=o[4].value/r[4].value;return new e.TokenNode([n.TokenType.Number,t.toString(),o[2],r[3],{value:t,type:Number.isInteger(t)?n.NumberType.Integer:n.NumberType.Number}])}if(n.isTokenPercentage(o)&&n.isTokenNumber(r)){const t=o[4].value/r[4].value;return new e.TokenNode([n.TokenType.Percentage,t.toString()+"%",o[2],r[3],{value:t}])}if(n.isTokenDimension(o)&&n.isTokenNumber(r)){const t=o[4].value/r[4].value;return new e.TokenNode([n.TokenType.Dimension,t.toString()+o[4].unit,o[2],r[3],{value:t,type:Number.isInteger(t)?n.NumberType.Integer:n.NumberType.Number,unit:o[4].unit}])}return-1}function isCalculation(e){return!!e&&"object"==typeof e&&"inputs"in e&&Array.isArray(e.inputs)&&"operation"in e}function solve(n){if(-1===n)return-1;const t=[];for(let o=0;oe.type))).size)return-1;const i=r.value;if(!n.isTokenNumeric(i))return-1;const u=o.map((e=>convertUnit(i,e.value)));if(!arrayOfSameNumeric(u))return-1;const a=u.map((e=>e[4].value)),s=Math.hypot(...a);return resultToCalculation(t,i,s)}function solveMax(t,o){const r=o[0];if(!r||!e.isTokenNode(r))return-1;if(1!==new Set(o.map((e=>e.type))).size)return-1;const i=r.value;if(!n.isTokenNumeric(i))return-1;const u=o.map((e=>convertUnit(i,e.value)));if(!arrayOfSameNumeric(u))return-1;const a=u.map((e=>e[4].value)),s=Math.max(...a);return resultToCalculation(t,i,s)}function solveMin(t,o){const r=o[0];if(!r||!e.isTokenNode(r))return-1;if(1!==new Set(o.map((e=>e.type))).size)return-1;const i=r.value;if(!n.isTokenNumeric(i))return-1;const u=o.map((e=>convertUnit(i,e.value)));if(!arrayOfSameNumeric(u))return-1;const a=u.map((e=>e[4].value)),s=Math.min(...a);return resultToCalculation(t,i,s)}function solveMod(e,t,o){const r=t.value;if(!n.isTokenNumeric(r))return-1;const i=convertUnit(r,o.value);if(!twoOfSameNumeric(r,i))return-1;let u;return u=0===i[4].value?Number.NaN:Number.isFinite(r[4].value)&&(Number.isFinite(i[4].value)||(i[4].value!==Number.POSITIVE_INFINITY||r[4].value!==Number.NEGATIVE_INFINITY&&!Object.is(0*r[4].value,-0))&&(i[4].value!==Number.NEGATIVE_INFINITY||r[4].value!==Number.POSITIVE_INFINITY&&!Object.is(0*r[4].value,0)))?Number.isFinite(i[4].value)?(r[4].value%i[4].value+i[4].value)%i[4].value:r[4].value:Number.NaN,resultToCalculation(e,r,u)}function solvePow(e,t,o){const r=t.value,i=o.value;if(!n.isTokenNumber(r))return-1;if(!twoOfSameNumeric(r,i))return-1;return numberToCalculation(e,Math.pow(r[4].value,i[4].value))}function solveRem(e,t,o){const r=t.value;if(!n.isTokenNumeric(r))return-1;const i=convertUnit(r,o.value);if(!twoOfSameNumeric(r,i))return-1;let u;return u=0===i[4].value?Number.NaN:Number.isFinite(r[4].value)?Number.isFinite(i[4].value)?r[4].value%i[4].value:r[4].value:Number.NaN,resultToCalculation(e,r,u)}function solveRound(e,t,o,r){const i=o.value;if(!n.isTokenNumeric(i))return-1;const u=convertUnit(i,r.value);if(!twoOfSameNumeric(i,u))return-1;let a;if(0===u[4].value)a=Number.NaN;else if(Number.isFinite(i[4].value)||Number.isFinite(u[4].value))if(!Number.isFinite(i[4].value)&&Number.isFinite(u[4].value))a=i[4].value;else if(Number.isFinite(i[4].value)&&!Number.isFinite(u[4].value))switch(t){case"down":a=i[4].value<0?-1/0:Object.is(-0,0*i[4].value)?-0:0;break;case"up":a=i[4].value>0?1/0:Object.is(0,0*i[4].value)?0:-0;break;default:a=Object.is(0,0*i[4].value)?0:-0}else if(Number.isFinite(u[4].value))switch(t){case"down":a=Math.floor(i[4].value/u[4].value)*u[4].value;break;case"up":a=Math.ceil(i[4].value/u[4].value)*u[4].value;break;case"to-zero":a=Math.trunc(i[4].value/u[4].value)*u[4].value;break;default:{let e=Math.floor(i[4].value/u[4].value)*u[4].value,n=Math.ceil(i[4].value/u[4].value)*u[4].value;if(e>n){const t=e;e=n,n=t}const t=Math.abs(i[4].value-e),o=Math.abs(i[4].value-n);a=t===o?n:t0?1/0:-1/0:Math.tan(s),numberToCalculation(e,s)}function subtraction(t){if(2!==t.length)return-1;const o=t[0].value;let r=t[1].value;if(n.isTokenNumber(o)&&n.isTokenNumber(r)){const t=o[4].value-r[4].value;return new e.TokenNode([n.TokenType.Number,t.toString(),o[2],r[3],{value:t,type:o[4].type===n.NumberType.Integer&&r[4].type===n.NumberType.Integer?n.NumberType.Integer:n.NumberType.Number}])}if(n.isTokenPercentage(o)&&n.isTokenPercentage(r)){const t=o[4].value-r[4].value;return new e.TokenNode([n.TokenType.Percentage,t.toString()+"%",o[2],r[3],{value:t}])}if(n.isTokenDimension(o)&&n.isTokenDimension(r)&&(r=convertUnit(o,r),toLowerCaseAZ(o[4].unit)===toLowerCaseAZ(r[4].unit))){const t=o[4].value-r[4].value;return new e.TokenNode([n.TokenType.Dimension,t.toString()+o[4].unit,o[2],r[3],{value:t,type:o[4].type===n.NumberType.Integer&&r[4].type===n.NumberType.Integer?n.NumberType.Integer:n.NumberType.Number,unit:o[4].unit}])}return-1}function solveLog(t,o){if(1===o.length){const r=o[0];if(!r||!e.isTokenNode(r))return-1;const i=r.value;if(!n.isTokenNumber(i))return-1;return numberToCalculation(t,Math.log(i[4].value))}if(2===o.length){const r=o[0];if(!r||!e.isTokenNode(r))return-1;const i=r.value;if(!n.isTokenNumber(i))return-1;const u=o[1];if(!u||!e.isTokenNode(u))return-1;const a=u.value;if(!n.isTokenNumber(a))return-1;return numberToCalculation(t,Math.log(i[4].value)/Math.log(a[4].value))}return-1}const g=/^none$/i;function isNone(t){if(Array.isArray(t)){const n=t.filter((n=>!(e.isWhitespaceNode(n)&&e.isCommentNode(n))));return 1===n.length&&isNone(n[0])}if(!e.isTokenNode(t))return!1;const o=t.value;return!!n.isTokenIdent(o)&&g.test(o[4].value)}const y=new Map([["abs",function abs(e,n){return singleNodeSolver(e,n,solveAbs)}],["acos",function acos(e,n){return singleNodeSolver(e,n,solveACos)}],["asin",function asin(e,n){return singleNodeSolver(e,n,solveASin)}],["atan",function atan(e,n){return singleNodeSolver(e,n,solveATan)}],["atan2",function atan2(e,n){return twoCommaSeparatedNodesSolver(e,n,solveATan2)}],["calc",calc$1],["clamp",function clamp(t,o){const r=resolveGlobalsAndConstants([...t.value.filter((n=>!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],o),i=[],u=[],a=[];{let t=i;for(let o=0;o!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],o);let i="",u=!1;const a=[],s=[];{let t=a;for(let o=0;o!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],o);if(1===r.length&&e.isTokenNode(r[0]))return{inputs:[r[0]],operation:unary};let i=0;for(;i!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],t)),t));return-1===r?-1:o(n,r)}function twoCommaSeparatedNodesSolver(t,o,r){const i=resolveGlobalsAndConstants([...t.value.filter((n=>!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],o),u=[],a=[];{let t=u;for(let o=0;o!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],o),u=[];{const t=[];let r=[];for(let o=0;o{if(!e.isFunctionNode(n))return;const r=y.get(n.getName().toLowerCase());if(!r)return;const i=patchCalcResult(solve(r(n,o)),t);return-1!==i?i:void 0}))}const w=new Set(y.keys());exports.calc=function calc(t,o){return calcFromComponentValues(e.parseCommaSeparatedListOfComponentValues(n.tokenize({css:t}),{}),o).map((e=>e.map((e=>n.stringify(...e.tokens()))).join(""))).join(",")},exports.calcFromComponentValues=calcFromComponentValues,exports.mathFunctionNames=w; +"use strict";var e=require("@csstools/css-parser-algorithms"),n=require("@csstools/css-tokenizer");const t=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(t,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}const o={cm:"px",in:"px",mm:"px",pc:"px",pt:"px",px:"px",q:"px",deg:"deg",grad:"deg",rad:"deg",turn:"deg",ms:"s",s:"s",hz:"hz",khz:"hz"},r=new Map([["cm",e=>e],["mm",e=>10*e],["q",e=>40*e],["in",e=>e/2.54],["pc",e=>e/2.54*6],["pt",e=>e/2.54*72],["px",e=>e/2.54*96]]),i=new Map([["deg",e=>e],["grad",e=>e/.9],["rad",e=>e/180*Math.PI],["turn",e=>e/360]]),u=new Map([["deg",e=>.9*e],["grad",e=>e],["rad",e=>.9*e/180*Math.PI],["turn",e=>.9*e/360]]),a=new Map([["hz",e=>e],["khz",e=>e/1e3]]),s=new Map([["cm",e=>2.54*e],["mm",e=>25.4*e],["q",e=>25.4*e*4],["in",e=>e],["pc",e=>6*e],["pt",e=>72*e],["px",e=>96*e]]),l=new Map([["hz",e=>1e3*e],["khz",e=>e]]),c=new Map([["cm",e=>e/10],["mm",e=>e],["q",e=>4*e],["in",e=>e/25.4],["pc",e=>e/25.4*6],["pt",e=>e/25.4*72],["px",e=>e/25.4*96]]),T=new Map([["ms",e=>e],["s",e=>e/1e3]]),p=new Map([["cm",e=>e/6*2.54],["mm",e=>e/6*25.4],["q",e=>e/6*25.4*4],["in",e=>e/6],["pc",e=>e],["pt",e=>e/6*72],["px",e=>e/6*96]]),m=new Map([["cm",e=>e/72*2.54],["mm",e=>e/72*25.4],["q",e=>e/72*25.4*4],["in",e=>e/72],["pc",e=>e/72*6],["pt",e=>e],["px",e=>e/72*96]]),v=new Map([["cm",e=>e/96*2.54],["mm",e=>e/96*25.4],["q",e=>e/96*25.4*4],["in",e=>e/96],["pc",e=>e/96*6],["pt",e=>e/96*72],["px",e=>e]]),N=new Map([["cm",e=>e/4/10],["mm",e=>e/4],["q",e=>e],["in",e=>e/4/25.4],["pc",e=>e/4/25.4*6],["pt",e=>e/4/25.4*72],["px",e=>e/4/25.4*96]]),f=new Map([["deg",e=>180*e/Math.PI],["grad",e=>180*e/Math.PI/.9],["rad",e=>e],["turn",e=>180*e/Math.PI/360]]),k=new Map([["ms",e=>1e3*e],["s",e=>e]]),d=new Map([["deg",e=>360*e],["grad",e=>360*e/.9],["rad",e=>360*e/180*Math.PI],["turn",e=>e]]),b=new Map([["cm",r],["mm",c],["q",N],["in",s],["pc",p],["pt",m],["px",v],["ms",T],["s",k],["deg",i],["grad",u],["rad",f],["turn",d],["hz",a],["khz",l]]);function convertUnit(e,t){if(!n.isTokenDimension(e))return t;if(!n.isTokenDimension(t))return t;const o=toLowerCaseAZ(e[4].unit),r=toLowerCaseAZ(t[4].unit);if(o===r)return t;const i=b.get(r);if(!i)return t;const u=i.get(o);if(!u)return t;const a=u(t[4].value),s=[n.TokenType.Dimension,"",t[2],t[3],{...t[4],signCharacter:a<0?"-":void 0,type:Number.isInteger(a)?n.NumberType.Integer:n.NumberType.Number,value:a}];return n.mutateUnit(s,e[4].unit),s}function toCanonicalUnit(e){if(!n.isTokenDimension(e))return e;const t=toLowerCaseAZ(e[4].unit),r=o[t];if(t===r)return e;const i=b.get(t);if(!i)return e;const u=i.get(r);if(!u)return e;const a=u(e[4].value),s=[n.TokenType.Dimension,"",e[2],e[3],{...e[4],signCharacter:a<0?"-":void 0,type:Number.isInteger(a)?n.NumberType.Integer:n.NumberType.Number,value:a}];return n.mutateUnit(s,r),s}function addition(t){if(2!==t.length)return-1;const o=t[0].value;let r=t[1].value;if(n.isTokenNumber(o)&&n.isTokenNumber(r)){const t=o[4].value+r[4].value;return new e.TokenNode([n.TokenType.Number,t.toString(),o[2],r[3],{value:t,type:o[4].type===n.NumberType.Integer&&r[4].type===n.NumberType.Integer?n.NumberType.Integer:n.NumberType.Number}])}if(n.isTokenPercentage(o)&&n.isTokenPercentage(r)){const t=o[4].value+r[4].value;return new e.TokenNode([n.TokenType.Percentage,t.toString()+"%",o[2],r[3],{value:t}])}if(n.isTokenDimension(o)&&n.isTokenDimension(r)&&(r=convertUnit(o,r),toLowerCaseAZ(o[4].unit)===toLowerCaseAZ(r[4].unit))){const t=o[4].value+r[4].value;return new e.TokenNode([n.TokenType.Dimension,t.toString()+o[4].unit,o[2],r[3],{value:t,type:o[4].type===n.NumberType.Integer&&r[4].type===n.NumberType.Integer?n.NumberType.Integer:n.NumberType.Number,unit:o[4].unit}])}return-1}function division(t){if(2!==t.length)return-1;const o=t[0].value,r=t[1].value;if(n.isTokenNumber(o)&&n.isTokenNumber(r)){const t=o[4].value/r[4].value;return new e.TokenNode([n.TokenType.Number,t.toString(),o[2],r[3],{value:t,type:Number.isInteger(t)?n.NumberType.Integer:n.NumberType.Number}])}if(n.isTokenPercentage(o)&&n.isTokenNumber(r)){const t=o[4].value/r[4].value;return new e.TokenNode([n.TokenType.Percentage,t.toString()+"%",o[2],r[3],{value:t}])}if(n.isTokenDimension(o)&&n.isTokenNumber(r)){const t=o[4].value/r[4].value;return new e.TokenNode([n.TokenType.Dimension,t.toString()+o[4].unit,o[2],r[3],{value:t,type:Number.isInteger(t)?n.NumberType.Integer:n.NumberType.Number,unit:o[4].unit}])}return-1}function isCalculation(e){return!!e&&"object"==typeof e&&"inputs"in e&&Array.isArray(e.inputs)&&"operation"in e}function solve(n){if(-1===n)return-1;const t=[];for(let o=0;oe.type))).size)return-1;const u=i.value;if(!n.isTokenNumeric(u))return-1;if(!r.rawPercentages&&n.isTokenPercentage(u))return-1;const a=o.map((e=>convertUnit(u,e.value)));if(!arrayOfSameNumeric(a))return-1;const s=a.map((e=>e[4].value)),l=Math.hypot(...s);return resultToCalculation(t,u,l)}function solveMax(t,o,r){const i=o[0];if(!i||!e.isTokenNode(i))return-1;if(1!==new Set(o.map((e=>e.type))).size)return-1;const u=i.value;if(!n.isTokenNumeric(u))return-1;if(!r.rawPercentages&&n.isTokenPercentage(u))return-1;const a=o.map((e=>convertUnit(u,e.value)));if(!arrayOfSameNumeric(a))return-1;const s=a.map((e=>e[4].value)),l=Math.max(...s);return resultToCalculation(t,u,l)}function solveMin(t,o,r){const i=o[0];if(!i||!e.isTokenNode(i))return-1;if(1!==new Set(o.map((e=>e.type))).size)return-1;const u=i.value;if(!n.isTokenNumeric(u))return-1;if(!r.rawPercentages&&n.isTokenPercentage(u))return-1;const a=o.map((e=>convertUnit(u,e.value)));if(!arrayOfSameNumeric(a))return-1;const s=a.map((e=>e[4].value)),l=Math.min(...s);return resultToCalculation(t,u,l)}function solveMod(e,t,o){const r=t.value;if(!n.isTokenNumeric(r))return-1;const i=convertUnit(r,o.value);if(!twoOfSameNumeric(r,i))return-1;let u;return u=0===i[4].value?Number.NaN:Number.isFinite(r[4].value)&&(Number.isFinite(i[4].value)||(i[4].value!==Number.POSITIVE_INFINITY||r[4].value!==Number.NEGATIVE_INFINITY&&!Object.is(0*r[4].value,-0))&&(i[4].value!==Number.NEGATIVE_INFINITY||r[4].value!==Number.POSITIVE_INFINITY&&!Object.is(0*r[4].value,0)))?Number.isFinite(i[4].value)?(r[4].value%i[4].value+i[4].value)%i[4].value:r[4].value:Number.NaN,resultToCalculation(e,r,u)}function solvePow(e,t,o){const r=t.value,i=o.value;if(!n.isTokenNumber(r))return-1;if(!twoOfSameNumeric(r,i))return-1;return numberToCalculation(e,Math.pow(r[4].value,i[4].value))}function solveRem(e,t,o){const r=t.value;if(!n.isTokenNumeric(r))return-1;const i=convertUnit(r,o.value);if(!twoOfSameNumeric(r,i))return-1;let u;return u=0===i[4].value?Number.NaN:Number.isFinite(r[4].value)?Number.isFinite(i[4].value)?r[4].value%i[4].value:r[4].value:Number.NaN,resultToCalculation(e,r,u)}function solveRound(e,t,o,r,i){const u=o.value;if(!n.isTokenNumeric(u))return-1;if(!i.rawPercentages&&n.isTokenPercentage(u))return-1;const a=convertUnit(u,r.value);if(!twoOfSameNumeric(u,a))return-1;let s;if(0===a[4].value)s=Number.NaN;else if(Number.isFinite(u[4].value)||Number.isFinite(a[4].value))if(!Number.isFinite(u[4].value)&&Number.isFinite(a[4].value))s=u[4].value;else if(Number.isFinite(u[4].value)&&!Number.isFinite(a[4].value))switch(t){case"down":s=u[4].value<0?-1/0:Object.is(-0,0*u[4].value)?-0:0;break;case"up":s=u[4].value>0?1/0:Object.is(0,0*u[4].value)?0:-0;break;default:s=Object.is(0,0*u[4].value)?0:-0}else if(Number.isFinite(a[4].value))switch(t){case"down":s=Math.floor(u[4].value/a[4].value)*a[4].value;break;case"up":s=Math.ceil(u[4].value/a[4].value)*a[4].value;break;case"to-zero":s=Math.trunc(u[4].value/a[4].value)*a[4].value;break;default:{let e=Math.floor(u[4].value/a[4].value)*a[4].value,n=Math.ceil(u[4].value/a[4].value)*a[4].value;if(e>n){const t=e;e=n,n=t}const t=Math.abs(u[4].value-e),o=Math.abs(u[4].value-n);s=t===o?n:t0?1/0:-1/0:Math.tan(s),numberToCalculation(e,s)}function subtraction(t){if(2!==t.length)return-1;const o=t[0].value;let r=t[1].value;if(n.isTokenNumber(o)&&n.isTokenNumber(r)){const t=o[4].value-r[4].value;return new e.TokenNode([n.TokenType.Number,t.toString(),o[2],r[3],{value:t,type:o[4].type===n.NumberType.Integer&&r[4].type===n.NumberType.Integer?n.NumberType.Integer:n.NumberType.Number}])}if(n.isTokenPercentage(o)&&n.isTokenPercentage(r)){const t=o[4].value-r[4].value;return new e.TokenNode([n.TokenType.Percentage,t.toString()+"%",o[2],r[3],{value:t}])}if(n.isTokenDimension(o)&&n.isTokenDimension(r)&&(r=convertUnit(o,r),toLowerCaseAZ(o[4].unit)===toLowerCaseAZ(r[4].unit))){const t=o[4].value-r[4].value;return new e.TokenNode([n.TokenType.Dimension,t.toString()+o[4].unit,o[2],r[3],{value:t,type:o[4].type===n.NumberType.Integer&&r[4].type===n.NumberType.Integer?n.NumberType.Integer:n.NumberType.Number,unit:o[4].unit}])}return-1}function solveLog(t,o){if(1===o.length){const r=o[0];if(!r||!e.isTokenNode(r))return-1;const i=r.value;if(!n.isTokenNumber(i))return-1;return numberToCalculation(t,Math.log(i[4].value))}if(2===o.length){const r=o[0];if(!r||!e.isTokenNode(r))return-1;const i=r.value;if(!n.isTokenNumber(i))return-1;const u=o[1];if(!u||!e.isTokenNode(u))return-1;const a=u.value;if(!n.isTokenNumber(a))return-1;return numberToCalculation(t,Math.log(i[4].value)/Math.log(a[4].value))}return-1}const g=/^none$/i;function isNone(t){if(Array.isArray(t)){const n=t.filter((n=>!(e.isWhitespaceNode(n)&&e.isCommentNode(n))));return 1===n.length&&isNone(n[0])}if(!e.isTokenNode(t))return!1;const o=t.value;return!!n.isTokenIdent(o)&&g.test(o[4].value)}const y=new Map([["abs",function abs(e,n,t){return singleNodeSolver(e,n,t,solveAbs)}],["acos",function acos(e,n,t){return singleNodeSolver(e,n,t,solveACos)}],["asin",function asin(e,n,t){return singleNodeSolver(e,n,t,solveASin)}],["atan",function atan(e,n,t){return singleNodeSolver(e,n,t,solveATan)}],["atan2",function atan2(e,n,t){return twoCommaSeparatedNodesSolver(e,n,t,solveATan2)}],["calc",calc$1],["clamp",function clamp(t,o,r){const i=resolveGlobalsAndConstants([...t.value.filter((n=>!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],o),u=[],a=[],s=[];{let t=u;for(let o=0;o!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],o);let u="",a=!1;const s=[],l=[];{let t=s;for(let o=0;o!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],o);if(1===i.length&&e.isTokenNode(i[0]))return{inputs:[i[0]],operation:unary};let u=0;for(;u!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],t)),t,o));return-1===i?-1:r(n,i,o)}function twoCommaSeparatedNodesSolver(t,o,r,i){const u=resolveGlobalsAndConstants([...t.value.filter((n=>!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],o),a=[],s=[];{let t=a;for(let o=0;o!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],o),a=[];{const t=[];let i=[];for(let o=0;o{if(!e.isFunctionNode(n))return;const r=y.get(n.getName().toLowerCase());if(!r)return;const i=patchCalcResult(solve(r(n,o,t??{})),t);return-1!==i?i:void 0}))}const w=new Set(y.keys());exports.calc=function calc(t,o){return calcFromComponentValues(e.parseCommaSeparatedListOfComponentValues(n.tokenize({css:t}),{}),o).map((e=>e.map((e=>n.stringify(...e.tokens()))).join(""))).join(",")},exports.calcFromComponentValues=calcFromComponentValues,exports.mathFunctionNames=w; diff --git a/packages/css-calc/dist/index.d.ts b/packages/css-calc/dist/index.d.ts index 8eabc729b..eaffefa1e 100644 --- a/packages/css-calc/dist/index.d.ts +++ b/packages/css-calc/dist/index.d.ts @@ -32,6 +32,13 @@ export declare type conversionOptions = { * 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; }; export declare type GlobalsWithStrings = Map; diff --git a/packages/css-calc/dist/index.mjs b/packages/css-calc/dist/index.mjs index df193ad95..b9604ba6d 100644 --- a/packages/css-calc/dist/index.mjs +++ b/packages/css-calc/dist/index.mjs @@ -1 +1 @@ -import{TokenNode as e,isTokenNode as n,isWhitespaceNode as t,isCommentNode as r,isSimpleBlockNode as u,isFunctionNode as a,FunctionNode as i,WhitespaceNode as o,parseCommaSeparatedListOfComponentValues as l,replaceComponentValues as c}from"@csstools/css-parser-algorithms";import{isTokenDimension as s,TokenType as v,NumberType as f,mutateUnit as p,isTokenNumber as m,isTokenPercentage as g,isTokenIdent as N,isTokenNumeric as d,isTokenOpenParen as b,isTokenDelim as h,isTokenComma as w,isToken as C,tokenizer as I,tokenize as S,stringify as y}from"@csstools/css-tokenizer";const M=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(M,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}const T={cm:"px",in:"px",mm:"px",pc:"px",pt:"px",px:"px",q:"px",deg:"deg",grad:"deg",rad:"deg",turn:"deg",ms:"s",s:"s",hz:"hz",khz:"hz"},A=new Map([["cm",e=>e],["mm",e=>10*e],["q",e=>40*e],["in",e=>e/2.54],["pc",e=>e/2.54*6],["pt",e=>e/2.54*72],["px",e=>e/2.54*96]]),k=new Map([["deg",e=>e],["grad",e=>e/.9],["rad",e=>e/180*Math.PI],["turn",e=>e/360]]),x=new Map([["deg",e=>.9*e],["grad",e=>e],["rad",e=>.9*e/180*Math.PI],["turn",e=>.9*e/360]]),F=new Map([["hz",e=>e],["khz",e=>e/1e3]]),P=new Map([["cm",e=>2.54*e],["mm",e=>25.4*e],["q",e=>25.4*e*4],["in",e=>e],["pc",e=>6*e],["pt",e=>72*e],["px",e=>96*e]]),O=new Map([["hz",e=>1e3*e],["khz",e=>e]]),W=new Map([["cm",e=>e/10],["mm",e=>e],["q",e=>4*e],["in",e=>e/25.4],["pc",e=>e/25.4*6],["pt",e=>e/25.4*72],["px",e=>e/25.4*96]]),D=new Map([["ms",e=>e],["s",e=>e/1e3]]),L=new Map([["cm",e=>e/6*2.54],["mm",e=>e/6*25.4],["q",e=>e/6*25.4*4],["in",e=>e/6],["pc",e=>e],["pt",e=>e/6*72],["px",e=>e/6*96]]),z=new Map([["cm",e=>e/72*2.54],["mm",e=>e/72*25.4],["q",e=>e/72*25.4*4],["in",e=>e/72],["pc",e=>e/72*6],["pt",e=>e],["px",e=>e/72*96]]),Z=new Map([["cm",e=>e/96*2.54],["mm",e=>e/96*25.4],["q",e=>e/96*25.4*4],["in",e=>e/96],["pc",e=>e/96*6],["pt",e=>e/96*72],["px",e=>e]]),U=new Map([["cm",e=>e/4/10],["mm",e=>e/4],["q",e=>e],["in",e=>e/4/25.4],["pc",e=>e/4/25.4*6],["pt",e=>e/4/25.4*72],["px",e=>e/4/25.4*96]]),$=new Map([["deg",e=>180*e/Math.PI],["grad",e=>180*e/Math.PI/.9],["rad",e=>e],["turn",e=>180*e/Math.PI/360]]),q=new Map([["ms",e=>1e3*e],["s",e=>e]]),E=new Map([["deg",e=>360*e],["grad",e=>360*e/.9],["rad",e=>360*e/180*Math.PI],["turn",e=>e]]),G=new Map([["cm",A],["mm",W],["q",U],["in",P],["pc",L],["pt",z],["px",Z],["ms",D],["s",q],["deg",k],["grad",x],["rad",$],["turn",E],["hz",F],["khz",O]]);function convertUnit(e,n){if(!s(e))return n;if(!s(n))return n;const t=toLowerCaseAZ(e[4].unit),r=toLowerCaseAZ(n[4].unit);if(t===r)return n;const u=G.get(r);if(!u)return n;const a=u.get(t);if(!a)return n;const i=a(n[4].value),o=[v.Dimension,"",n[2],n[3],{...n[4],signCharacter:i<0?"-":void 0,type:Number.isInteger(i)?f.Integer:f.Number,value:i}];return p(o,e[4].unit),o}function toCanonicalUnit(e){if(!s(e))return e;const n=toLowerCaseAZ(e[4].unit),t=T[n];if(n===t)return e;const r=G.get(n);if(!r)return e;const u=r.get(t);if(!u)return e;const a=u(e[4].value),i=[v.Dimension,"",e[2],e[3],{...e[4],signCharacter:a<0?"-":void 0,type:Number.isInteger(a)?f.Integer:f.Number,value:a}];return p(i,t),i}function addition(n){if(2!==n.length)return-1;const t=n[0].value;let r=n[1].value;if(m(t)&&m(r)){const n=t[4].value+r[4].value;return new e([v.Number,n.toString(),t[2],r[3],{value:n,type:t[4].type===f.Integer&&r[4].type===f.Integer?f.Integer:f.Number}])}if(g(t)&&g(r)){const n=t[4].value+r[4].value;return new e([v.Percentage,n.toString()+"%",t[2],r[3],{value:n}])}if(s(t)&&s(r)&&(r=convertUnit(t,r),toLowerCaseAZ(t[4].unit)===toLowerCaseAZ(r[4].unit))){const n=t[4].value+r[4].value;return new e([v.Dimension,n.toString()+t[4].unit,t[2],r[3],{value:n,type:t[4].type===f.Integer&&r[4].type===f.Integer?f.Integer:f.Number,unit:t[4].unit}])}return-1}function division(n){if(2!==n.length)return-1;const t=n[0].value,r=n[1].value;if(m(t)&&m(r)){const n=t[4].value/r[4].value;return new e([v.Number,n.toString(),t[2],r[3],{value:n,type:Number.isInteger(n)?f.Integer:f.Number}])}if(g(t)&&m(r)){const n=t[4].value/r[4].value;return new e([v.Percentage,n.toString()+"%",t[2],r[3],{value:n}])}if(s(t)&&m(r)){const n=t[4].value/r[4].value;return new e([v.Dimension,n.toString()+t[4].unit,t[2],r[3],{value:n,type:Number.isInteger(n)?f.Integer:f.Number,unit:t[4].unit}])}return-1}function isCalculation(e){return!!e&&"object"==typeof e&&"inputs"in e&&Array.isArray(e.inputs)&&"operation"in e}function solve(e){if(-1===e)return-1;const t=[];for(let r=0;re.type))).size)return-1;const u=r.value;if(!d(u))return-1;const a=t.map((e=>convertUnit(u,e.value)));if(!arrayOfSameNumeric(a))return-1;const i=a.map((e=>e[4].value)),o=Math.hypot(...i);return resultToCalculation(e,u,o)}function solveMax(e,t){const r=t[0];if(!r||!n(r))return-1;if(1!==new Set(t.map((e=>e.type))).size)return-1;const u=r.value;if(!d(u))return-1;const a=t.map((e=>convertUnit(u,e.value)));if(!arrayOfSameNumeric(a))return-1;const i=a.map((e=>e[4].value)),o=Math.max(...i);return resultToCalculation(e,u,o)}function solveMin(e,t){const r=t[0];if(!r||!n(r))return-1;if(1!==new Set(t.map((e=>e.type))).size)return-1;const u=r.value;if(!d(u))return-1;const a=t.map((e=>convertUnit(u,e.value)));if(!arrayOfSameNumeric(a))return-1;const i=a.map((e=>e[4].value)),o=Math.min(...i);return resultToCalculation(e,u,o)}function solveMod(e,n,t){const r=n.value;if(!d(r))return-1;const u=convertUnit(r,t.value);if(!twoOfSameNumeric(r,u))return-1;let a;return a=0===u[4].value?Number.NaN:Number.isFinite(r[4].value)&&(Number.isFinite(u[4].value)||(u[4].value!==Number.POSITIVE_INFINITY||r[4].value!==Number.NEGATIVE_INFINITY&&!Object.is(0*r[4].value,-0))&&(u[4].value!==Number.NEGATIVE_INFINITY||r[4].value!==Number.POSITIVE_INFINITY&&!Object.is(0*r[4].value,0)))?Number.isFinite(u[4].value)?(r[4].value%u[4].value+u[4].value)%u[4].value:r[4].value:Number.NaN,resultToCalculation(e,r,a)}function solvePow(e,n,t){const r=n.value,u=t.value;if(!m(r))return-1;if(!twoOfSameNumeric(r,u))return-1;return numberToCalculation(e,Math.pow(r[4].value,u[4].value))}function solveRem(e,n,t){const r=n.value;if(!d(r))return-1;const u=convertUnit(r,t.value);if(!twoOfSameNumeric(r,u))return-1;let a;return a=0===u[4].value?Number.NaN:Number.isFinite(r[4].value)?Number.isFinite(u[4].value)?r[4].value%u[4].value:r[4].value:Number.NaN,resultToCalculation(e,r,a)}function solveRound(e,n,t,r){const u=t.value;if(!d(u))return-1;const a=convertUnit(u,r.value);if(!twoOfSameNumeric(u,a))return-1;let i;if(0===a[4].value)i=Number.NaN;else if(Number.isFinite(u[4].value)||Number.isFinite(a[4].value))if(!Number.isFinite(u[4].value)&&Number.isFinite(a[4].value))i=u[4].value;else if(Number.isFinite(u[4].value)&&!Number.isFinite(a[4].value))switch(n){case"down":i=u[4].value<0?-1/0:Object.is(-0,0*u[4].value)?-0:0;break;case"up":i=u[4].value>0?1/0:Object.is(0,0*u[4].value)?0:-0;break;default:i=Object.is(0,0*u[4].value)?0:-0}else if(Number.isFinite(a[4].value))switch(n){case"down":i=Math.floor(u[4].value/a[4].value)*a[4].value;break;case"up":i=Math.ceil(u[4].value/a[4].value)*a[4].value;break;case"to-zero":i=Math.trunc(u[4].value/a[4].value)*a[4].value;break;default:{let e=Math.floor(u[4].value/a[4].value)*a[4].value,n=Math.ceil(u[4].value/a[4].value)*a[4].value;if(e>n){const t=e;e=n,n=t}const t=Math.abs(u[4].value-e),r=Math.abs(u[4].value-n);i=t===r?n:t0?1/0:-1/0:Math.tan(a),numberToCalculation(e,a)}function subtraction(n){if(2!==n.length)return-1;const t=n[0].value;let r=n[1].value;if(m(t)&&m(r)){const n=t[4].value-r[4].value;return new e([v.Number,n.toString(),t[2],r[3],{value:n,type:t[4].type===f.Integer&&r[4].type===f.Integer?f.Integer:f.Number}])}if(g(t)&&g(r)){const n=t[4].value-r[4].value;return new e([v.Percentage,n.toString()+"%",t[2],r[3],{value:n}])}if(s(t)&&s(r)&&(r=convertUnit(t,r),toLowerCaseAZ(t[4].unit)===toLowerCaseAZ(r[4].unit))){const n=t[4].value-r[4].value;return new e([v.Dimension,n.toString()+t[4].unit,t[2],r[3],{value:n,type:t[4].type===f.Integer&&r[4].type===f.Integer?f.Integer:f.Number,unit:t[4].unit}])}return-1}function solveLog(e,t){if(1===t.length){const r=t[0];if(!r||!n(r))return-1;const u=r.value;if(!m(u))return-1;return numberToCalculation(e,Math.log(u[4].value))}if(2===t.length){const r=t[0];if(!r||!n(r))return-1;const u=r.value;if(!m(u))return-1;const a=t[1];if(!a||!n(a))return-1;const i=a.value;if(!m(i))return-1;return numberToCalculation(e,Math.log(u[4].value)/Math.log(i[4].value))}return-1}const j=/^none$/i;function isNone(e){if(Array.isArray(e)){const n=e.filter((e=>!(t(e)&&r(e))));return 1===n.length&&isNone(n[0])}if(!n(e))return!1;const u=e.value;return!!N(u)&&j.test(u[4].value)}const V=new Map([["abs",function abs(e,n){return singleNodeSolver(e,n,solveAbs)}],["acos",function acos(e,n){return singleNodeSolver(e,n,solveACos)}],["asin",function asin(e,n){return singleNodeSolver(e,n,solveASin)}],["atan",function atan(e,n){return singleNodeSolver(e,n,solveATan)}],["atan2",function atan2(e,n){return twoCommaSeparatedNodesSolver(e,n,solveATan2)}],["calc",calc$1],["clamp",function clamp(u,a){const o=resolveGlobalsAndConstants([...u.value.filter((e=>!r(e)&&!t(e)))],a),l=[],c=[],s=[];{let e=l;for(let t=0;t!r(e)&&!t(e)))],a);let o="",l=!1;const c=[],s=[];{let e=c;for(let t=0;t!r(e)&&!t(e)))],i);if(1===o.length&&n(o[0]))return{inputs:[o[0]],operation:unary};let l=0;for(;l!r(e)&&!t(e)))],n)),n));return-1===a?-1:u(e,a)}function twoCommaSeparatedNodesSolver(e,u,a){const i=resolveGlobalsAndConstants([...e.value.filter((e=>!r(e)&&!t(e)))],u),o=[],l=[];{let e=o;for(let t=0;t!r(e)&&!t(e)))],u),o=[];{const e=[];let t=[];for(let r=0;re.map((e=>y(...e.tokens()))).join(""))).join(",")}function calcFromComponentValues(e,n){const t=tokenizeGlobals(n?.globals);return c(e,(e=>{if(!a(e))return;const r=V.get(e.getName().toLowerCase());if(!r)return;const u=patchCalcResult(solve(r(e,t)),n);return-1!==u?u:void 0}))}const Y=new Set(V.keys());export{calc,calcFromComponentValues,Y as mathFunctionNames}; +import{TokenNode as e,isTokenNode as n,isWhitespaceNode as t,isCommentNode as r,isSimpleBlockNode as u,isFunctionNode as a,FunctionNode as i,WhitespaceNode as o,parseCommaSeparatedListOfComponentValues as l,replaceComponentValues as c}from"@csstools/css-parser-algorithms";import{isTokenDimension as s,TokenType as v,NumberType as f,mutateUnit as p,isTokenNumber as m,isTokenPercentage as g,isTokenIdent as N,isTokenNumeric as d,isTokenOpenParen as w,isTokenDelim as b,isTokenComma as h,isToken as C,tokenizer as I,tokenize as S,stringify as y}from"@csstools/css-tokenizer";const M=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(M,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}const T={cm:"px",in:"px",mm:"px",pc:"px",pt:"px",px:"px",q:"px",deg:"deg",grad:"deg",rad:"deg",turn:"deg",ms:"s",s:"s",hz:"hz",khz:"hz"},A=new Map([["cm",e=>e],["mm",e=>10*e],["q",e=>40*e],["in",e=>e/2.54],["pc",e=>e/2.54*6],["pt",e=>e/2.54*72],["px",e=>e/2.54*96]]),k=new Map([["deg",e=>e],["grad",e=>e/.9],["rad",e=>e/180*Math.PI],["turn",e=>e/360]]),P=new Map([["deg",e=>.9*e],["grad",e=>e],["rad",e=>.9*e/180*Math.PI],["turn",e=>.9*e/360]]),x=new Map([["hz",e=>e],["khz",e=>e/1e3]]),F=new Map([["cm",e=>2.54*e],["mm",e=>25.4*e],["q",e=>25.4*e*4],["in",e=>e],["pc",e=>6*e],["pt",e=>72*e],["px",e=>96*e]]),O=new Map([["hz",e=>1e3*e],["khz",e=>e]]),W=new Map([["cm",e=>e/10],["mm",e=>e],["q",e=>4*e],["in",e=>e/25.4],["pc",e=>e/25.4*6],["pt",e=>e/25.4*72],["px",e=>e/25.4*96]]),L=new Map([["ms",e=>e],["s",e=>e/1e3]]),D=new Map([["cm",e=>e/6*2.54],["mm",e=>e/6*25.4],["q",e=>e/6*25.4*4],["in",e=>e/6],["pc",e=>e],["pt",e=>e/6*72],["px",e=>e/6*96]]),z=new Map([["cm",e=>e/72*2.54],["mm",e=>e/72*25.4],["q",e=>e/72*25.4*4],["in",e=>e/72],["pc",e=>e/72*6],["pt",e=>e],["px",e=>e/72*96]]),Z=new Map([["cm",e=>e/96*2.54],["mm",e=>e/96*25.4],["q",e=>e/96*25.4*4],["in",e=>e/96],["pc",e=>e/96*6],["pt",e=>e/96*72],["px",e=>e]]),U=new Map([["cm",e=>e/4/10],["mm",e=>e/4],["q",e=>e],["in",e=>e/4/25.4],["pc",e=>e/4/25.4*6],["pt",e=>e/4/25.4*72],["px",e=>e/4/25.4*96]]),$=new Map([["deg",e=>180*e/Math.PI],["grad",e=>180*e/Math.PI/.9],["rad",e=>e],["turn",e=>180*e/Math.PI/360]]),q=new Map([["ms",e=>1e3*e],["s",e=>e]]),E=new Map([["deg",e=>360*e],["grad",e=>360*e/.9],["rad",e=>360*e/180*Math.PI],["turn",e=>e]]),G=new Map([["cm",A],["mm",W],["q",U],["in",F],["pc",D],["pt",z],["px",Z],["ms",L],["s",q],["deg",k],["grad",P],["rad",$],["turn",E],["hz",x],["khz",O]]);function convertUnit(e,n){if(!s(e))return n;if(!s(n))return n;const t=toLowerCaseAZ(e[4].unit),r=toLowerCaseAZ(n[4].unit);if(t===r)return n;const u=G.get(r);if(!u)return n;const a=u.get(t);if(!a)return n;const i=a(n[4].value),o=[v.Dimension,"",n[2],n[3],{...n[4],signCharacter:i<0?"-":void 0,type:Number.isInteger(i)?f.Integer:f.Number,value:i}];return p(o,e[4].unit),o}function toCanonicalUnit(e){if(!s(e))return e;const n=toLowerCaseAZ(e[4].unit),t=T[n];if(n===t)return e;const r=G.get(n);if(!r)return e;const u=r.get(t);if(!u)return e;const a=u(e[4].value),i=[v.Dimension,"",e[2],e[3],{...e[4],signCharacter:a<0?"-":void 0,type:Number.isInteger(a)?f.Integer:f.Number,value:a}];return p(i,t),i}function addition(n){if(2!==n.length)return-1;const t=n[0].value;let r=n[1].value;if(m(t)&&m(r)){const n=t[4].value+r[4].value;return new e([v.Number,n.toString(),t[2],r[3],{value:n,type:t[4].type===f.Integer&&r[4].type===f.Integer?f.Integer:f.Number}])}if(g(t)&&g(r)){const n=t[4].value+r[4].value;return new e([v.Percentage,n.toString()+"%",t[2],r[3],{value:n}])}if(s(t)&&s(r)&&(r=convertUnit(t,r),toLowerCaseAZ(t[4].unit)===toLowerCaseAZ(r[4].unit))){const n=t[4].value+r[4].value;return new e([v.Dimension,n.toString()+t[4].unit,t[2],r[3],{value:n,type:t[4].type===f.Integer&&r[4].type===f.Integer?f.Integer:f.Number,unit:t[4].unit}])}return-1}function division(n){if(2!==n.length)return-1;const t=n[0].value,r=n[1].value;if(m(t)&&m(r)){const n=t[4].value/r[4].value;return new e([v.Number,n.toString(),t[2],r[3],{value:n,type:Number.isInteger(n)?f.Integer:f.Number}])}if(g(t)&&m(r)){const n=t[4].value/r[4].value;return new e([v.Percentage,n.toString()+"%",t[2],r[3],{value:n}])}if(s(t)&&m(r)){const n=t[4].value/r[4].value;return new e([v.Dimension,n.toString()+t[4].unit,t[2],r[3],{value:n,type:Number.isInteger(n)?f.Integer:f.Number,unit:t[4].unit}])}return-1}function isCalculation(e){return!!e&&"object"==typeof e&&"inputs"in e&&Array.isArray(e.inputs)&&"operation"in e}function solve(e){if(-1===e)return-1;const t=[];for(let r=0;re.type))).size)return-1;const a=u.value;if(!d(a))return-1;if(!r.rawPercentages&&g(a))return-1;const i=t.map((e=>convertUnit(a,e.value)));if(!arrayOfSameNumeric(i))return-1;const o=i.map((e=>e[4].value)),l=Math.hypot(...o);return resultToCalculation(e,a,l)}function solveMax(e,t,r){const u=t[0];if(!u||!n(u))return-1;if(1!==new Set(t.map((e=>e.type))).size)return-1;const a=u.value;if(!d(a))return-1;if(!r.rawPercentages&&g(a))return-1;const i=t.map((e=>convertUnit(a,e.value)));if(!arrayOfSameNumeric(i))return-1;const o=i.map((e=>e[4].value)),l=Math.max(...o);return resultToCalculation(e,a,l)}function solveMin(e,t,r){const u=t[0];if(!u||!n(u))return-1;if(1!==new Set(t.map((e=>e.type))).size)return-1;const a=u.value;if(!d(a))return-1;if(!r.rawPercentages&&g(a))return-1;const i=t.map((e=>convertUnit(a,e.value)));if(!arrayOfSameNumeric(i))return-1;const o=i.map((e=>e[4].value)),l=Math.min(...o);return resultToCalculation(e,a,l)}function solveMod(e,n,t){const r=n.value;if(!d(r))return-1;const u=convertUnit(r,t.value);if(!twoOfSameNumeric(r,u))return-1;let a;return a=0===u[4].value?Number.NaN:Number.isFinite(r[4].value)&&(Number.isFinite(u[4].value)||(u[4].value!==Number.POSITIVE_INFINITY||r[4].value!==Number.NEGATIVE_INFINITY&&!Object.is(0*r[4].value,-0))&&(u[4].value!==Number.NEGATIVE_INFINITY||r[4].value!==Number.POSITIVE_INFINITY&&!Object.is(0*r[4].value,0)))?Number.isFinite(u[4].value)?(r[4].value%u[4].value+u[4].value)%u[4].value:r[4].value:Number.NaN,resultToCalculation(e,r,a)}function solvePow(e,n,t){const r=n.value,u=t.value;if(!m(r))return-1;if(!twoOfSameNumeric(r,u))return-1;return numberToCalculation(e,Math.pow(r[4].value,u[4].value))}function solveRem(e,n,t){const r=n.value;if(!d(r))return-1;const u=convertUnit(r,t.value);if(!twoOfSameNumeric(r,u))return-1;let a;return a=0===u[4].value?Number.NaN:Number.isFinite(r[4].value)?Number.isFinite(u[4].value)?r[4].value%u[4].value:r[4].value:Number.NaN,resultToCalculation(e,r,a)}function solveRound(e,n,t,r,u){const a=t.value;if(!d(a))return-1;if(!u.rawPercentages&&g(a))return-1;const i=convertUnit(a,r.value);if(!twoOfSameNumeric(a,i))return-1;let o;if(0===i[4].value)o=Number.NaN;else if(Number.isFinite(a[4].value)||Number.isFinite(i[4].value))if(!Number.isFinite(a[4].value)&&Number.isFinite(i[4].value))o=a[4].value;else if(Number.isFinite(a[4].value)&&!Number.isFinite(i[4].value))switch(n){case"down":o=a[4].value<0?-1/0:Object.is(-0,0*a[4].value)?-0:0;break;case"up":o=a[4].value>0?1/0:Object.is(0,0*a[4].value)?0:-0;break;default:o=Object.is(0,0*a[4].value)?0:-0}else if(Number.isFinite(i[4].value))switch(n){case"down":o=Math.floor(a[4].value/i[4].value)*i[4].value;break;case"up":o=Math.ceil(a[4].value/i[4].value)*i[4].value;break;case"to-zero":o=Math.trunc(a[4].value/i[4].value)*i[4].value;break;default:{let e=Math.floor(a[4].value/i[4].value)*i[4].value,n=Math.ceil(a[4].value/i[4].value)*i[4].value;if(e>n){const t=e;e=n,n=t}const t=Math.abs(a[4].value-e),r=Math.abs(a[4].value-n);o=t===r?n:t0?1/0:-1/0:Math.tan(a),numberToCalculation(e,a)}function subtraction(n){if(2!==n.length)return-1;const t=n[0].value;let r=n[1].value;if(m(t)&&m(r)){const n=t[4].value-r[4].value;return new e([v.Number,n.toString(),t[2],r[3],{value:n,type:t[4].type===f.Integer&&r[4].type===f.Integer?f.Integer:f.Number}])}if(g(t)&&g(r)){const n=t[4].value-r[4].value;return new e([v.Percentage,n.toString()+"%",t[2],r[3],{value:n}])}if(s(t)&&s(r)&&(r=convertUnit(t,r),toLowerCaseAZ(t[4].unit)===toLowerCaseAZ(r[4].unit))){const n=t[4].value-r[4].value;return new e([v.Dimension,n.toString()+t[4].unit,t[2],r[3],{value:n,type:t[4].type===f.Integer&&r[4].type===f.Integer?f.Integer:f.Number,unit:t[4].unit}])}return-1}function solveLog(e,t){if(1===t.length){const r=t[0];if(!r||!n(r))return-1;const u=r.value;if(!m(u))return-1;return numberToCalculation(e,Math.log(u[4].value))}if(2===t.length){const r=t[0];if(!r||!n(r))return-1;const u=r.value;if(!m(u))return-1;const a=t[1];if(!a||!n(a))return-1;const i=a.value;if(!m(i))return-1;return numberToCalculation(e,Math.log(u[4].value)/Math.log(i[4].value))}return-1}const j=/^none$/i;function isNone(e){if(Array.isArray(e)){const n=e.filter((e=>!(t(e)&&r(e))));return 1===n.length&&isNone(n[0])}if(!n(e))return!1;const u=e.value;return!!N(u)&&j.test(u[4].value)}const V=new Map([["abs",function abs(e,n,t){return singleNodeSolver(e,n,t,solveAbs)}],["acos",function acos(e,n,t){return singleNodeSolver(e,n,t,solveACos)}],["asin",function asin(e,n,t){return singleNodeSolver(e,n,t,solveASin)}],["atan",function atan(e,n,t){return singleNodeSolver(e,n,t,solveATan)}],["atan2",function atan2(e,n,t){return twoCommaSeparatedNodesSolver(e,n,t,solveATan2)}],["calc",calc$1],["clamp",function clamp(u,a,o){const l=resolveGlobalsAndConstants([...u.value.filter((e=>!r(e)&&!t(e)))],a),c=[],s=[],f=[];{let e=c;for(let t=0;t!r(e)&&!t(e)))],a);let l="",c=!1;const s=[],p=[];{let e=s;for(let t=0;t!r(e)&&!t(e)))],i);if(1===l.length&&n(l[0]))return{inputs:[l[0]],operation:unary};let c=0;for(;c!r(e)&&!t(e)))],n)),n,u));return-1===i?-1:a(e,i,u)}function twoCommaSeparatedNodesSolver(e,u,a,i){const o=resolveGlobalsAndConstants([...e.value.filter((e=>!r(e)&&!t(e)))],u),l=[],c=[];{let e=l;for(let t=0;t!r(e)&&!t(e)))],u),l=[];{const e=[];let t=[];for(let r=0;re.map((e=>y(...e.tokens()))).join(""))).join(",")}function calcFromComponentValues(e,n){const t=tokenizeGlobals(n?.globals);return c(e,(e=>{if(!a(e))return;const r=V.get(e.getName().toLowerCase());if(!r)return;const u=patchCalcResult(solve(r(e,t,n??{})),n);return-1!==u?u:void 0}))}const Y=new Set(V.keys());export{calc,calcFromComponentValues,Y as mathFunctionNames}; diff --git a/packages/css-calc/docs/css-calc.api.json b/packages/css-calc/docs/css-calc.api.json index ade3b86b3..2ab60db3c 100644 --- a/packages/css-calc/docs/css-calc.api.json +++ b/packages/css-calc/docs/css-calc.api.json @@ -361,7 +361,7 @@ }, { "kind": "Content", - "text": ";\n precision?: number;\n toCanonicalUnits?: boolean;\n censorIntoStandardRepresentableValues?: boolean;\n}" + "text": ";\n precision?: number;\n toCanonicalUnits?: boolean;\n censorIntoStandardRepresentableValues?: boolean;\n rawPercentages?: boolean;\n}" }, { "kind": "Content", diff --git a/packages/css-calc/docs/css-calc.conversionoptions.md b/packages/css-calc/docs/css-calc.conversionoptions.md index f4fcea973..2e4a8ba73 100644 --- a/packages/css-calc/docs/css-calc.conversionoptions.md +++ b/packages/css-calc/docs/css-calc.conversionoptions.md @@ -12,6 +12,7 @@ export type conversionOptions = { precision?: number; toCanonicalUnits?: boolean; censorIntoStandardRepresentableValues?: boolean; + rawPercentages?: boolean; }; ``` **References:** [GlobalsWithStrings](./css-calc.globalswithstrings.md) diff --git a/packages/css-calc/src/functions/abs.ts b/packages/css-calc/src/functions/abs.ts index 0f331f605..00f4db5c1 100644 --- a/packages/css-calc/src/functions/abs.ts +++ b/packages/css-calc/src/functions/abs.ts @@ -1,11 +1,16 @@ import type { Calculation } from '../calculation'; import type { FunctionNode, TokenNode } from '@csstools/css-parser-algorithms'; import { resultToCalculation } from './result-to-calculation'; -import { isDimensionOrNumber } from '../util/kind-of-number'; +import type { conversionOptions } from '../options'; +import { isTokenNumeric, isTokenPercentage } from '@csstools/css-tokenizer'; -export function solveAbs(absNode: FunctionNode, a: TokenNode): Calculation | -1 { +export function solveAbs(absNode: FunctionNode, a: TokenNode, options: conversionOptions): Calculation | -1 { const aToken = a.value; - if (!isDimensionOrNumber(aToken)) { + if (!isTokenNumeric(aToken)) { + return -1; + } + + if (!options.rawPercentages && isTokenPercentage(aToken)) { return -1; } diff --git a/packages/css-calc/src/functions/calc.ts b/packages/css-calc/src/functions/calc.ts index 5a420433d..ad90132ed 100644 --- a/packages/css-calc/src/functions/calc.ts +++ b/packages/css-calc/src/functions/calc.ts @@ -31,8 +31,11 @@ import { subtraction } from '../operation/subtraction'; import { unary } from '../operation/unary'; import { solveLog } from './log'; import { isNone } from '../util/is-none'; +import type { conversionOptions } from '../options'; -export const mathFunctions = new Map([ +type mathFunction = (node: FunctionNode, globals: Globals, options: conversionOptions) => Calculation | -1 + +export const mathFunctions: Map = new Map([ ['abs', abs], ['acos', acos], ['asin', asin], @@ -56,7 +59,7 @@ export const mathFunctions = new Map([ ['tan', tan], ]); -function calc(calcNode: FunctionNode | SimpleBlockNode, globals: Globals): Calculation | -1 { +function calc(calcNode: FunctionNode | SimpleBlockNode, globals: Globals, options: conversionOptions): Calculation | -1 { const nodes: Array = resolveGlobalsAndConstants( [...(calcNode.value.filter(x => !isCommentNode(x) && !isWhitespaceNode(x)))], globals, @@ -74,7 +77,7 @@ function calc(calcNode: FunctionNode | SimpleBlockNode, globals: Globals): Calcu while (i < nodes.length) { const child = nodes[i]; if (isSimpleBlockNode(child) && isTokenOpenParen(child.startToken)) { - const subCalc = calc(child, globals); + const subCalc = calc(child, globals, options); if (subCalc === -1) { return -1; } @@ -88,7 +91,7 @@ function calc(calcNode: FunctionNode | SimpleBlockNode, globals: Globals): Calcu return -1; } - const subCalc = mathFunction(child, globals); + const subCalc = mathFunction(child, globals, options); if (subCalc === -1) { return -1; } @@ -211,21 +214,21 @@ function calc(calcNode: FunctionNode | SimpleBlockNode, globals: Globals): Calcu return -1; } -function singleNodeSolver(fnNode: FunctionNode, globals: Globals, solveFn: (node: FunctionNode, a: TokenNode) => Calculation | -1): Calculation | -1 { +function singleNodeSolver(fnNode: FunctionNode, globals: Globals, options: conversionOptions, solveFn: (node: FunctionNode, a: TokenNode, options: conversionOptions) => Calculation | -1): Calculation | -1 { const nodes: Array = resolveGlobalsAndConstants( [...(fnNode.value.filter(x => !isCommentNode(x) && !isWhitespaceNode(x)))], globals, ); - const a = solve(calc(calcWrapper(nodes), globals)); + const a = solve(calc(calcWrapper(nodes), globals, options)); if (a === -1) { return -1; } - return solveFn(fnNode, a); + return solveFn(fnNode, a, options); } -function twoCommaSeparatedNodesSolver(fnNode: FunctionNode, globals: Globals, solveFn: (node: FunctionNode, a: TokenNode, b: TokenNode) => Calculation | -1): Calculation | -1 { +function twoCommaSeparatedNodesSolver(fnNode: FunctionNode, globals: Globals, options: conversionOptions, solveFn: (node: FunctionNode, a: TokenNode, b: TokenNode, options: conversionOptions) => Calculation | -1): Calculation | -1 { const nodes: Array = resolveGlobalsAndConstants( [...(fnNode.value.filter(x => !isCommentNode(x) && !isWhitespaceNode(x)))], globals, @@ -257,20 +260,20 @@ function twoCommaSeparatedNodesSolver(fnNode: FunctionNode, globals: Globals, so } } - const a = solve(calc(calcWrapper(aValue), globals)); + const a = solve(calc(calcWrapper(aValue), globals, options)); if (a === -1) { return -1; } - const b = solve(calc(calcWrapper(bValue), globals)); + const b = solve(calc(calcWrapper(bValue), globals, options)); if (b === -1) { return -1; } - return solveFn(fnNode, a, b); + return solveFn(fnNode, a, b, options); } -function variadicNodesSolver(fnNode: FunctionNode, globals: Globals, solveFn: (node: FunctionNode, x: Array) => Calculation | -1): Calculation | -1 { +function variadicNodesSolver(fnNode: FunctionNode, globals: Globals, options: conversionOptions, solveFn: (node: FunctionNode, x: Array, options: conversionOptions) => Calculation | -1): Calculation | -1 { const nodes: Array = resolveGlobalsAndConstants( [...(fnNode.value.filter(x => !isCommentNode(x) && !isWhitespaceNode(x)))], globals, @@ -299,7 +302,7 @@ function variadicNodesSolver(fnNode: FunctionNode, globals: Globals, solveFn: (n return -1; } - const solvedChunk = solve(calc(calcWrapper(chunks[i]), globals)); + const solvedChunk = solve(calc(calcWrapper(chunks[i]), globals, options)); if (solvedChunk === -1) { return -1; } @@ -308,10 +311,10 @@ function variadicNodesSolver(fnNode: FunctionNode, globals: Globals, solveFn: (n } } - return solveFn(fnNode, solvedNodes); + return solveFn(fnNode, solvedNodes, options); } -function clamp(clampNode: FunctionNode, globals: Globals): Calculation | -1 { +function clamp(clampNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { const nodes: Array = resolveGlobalsAndConstants( [...(clampNode.value.filter(x => !isCommentNode(x) && !isWhitespaceNode(x)))], globals, @@ -351,51 +354,51 @@ function clamp(clampNode: FunctionNode, globals: Globals): Calculation | -1 { const minimumIsNone = isNone(minimumValue); const maximumIsNone = isNone(maximumValue); if (minimumIsNone && maximumIsNone) { - return calc(calcWrapper(centralValue), globals); + return calc(calcWrapper(centralValue), globals, options); } - const central = solve(calc(calcWrapper(centralValue), globals)); + const central = solve(calc(calcWrapper(centralValue), globals, options)); if (central === -1) { return -1; } { if (minimumIsNone) { - const maximum = solve(calc(calcWrapper(maximumValue), globals)); + const maximum = solve(calc(calcWrapper(maximumValue), globals, options)); if (maximum === -1) { return -1; } - return solveMin(minWrapper(central, maximum), [central, maximum]); + return solveMin(minWrapper(central, maximum), [central, maximum], options); } else if (maximumIsNone) { - const minimum = solve(calc(calcWrapper(minimumValue), globals)); + const minimum = solve(calc(calcWrapper(minimumValue), globals, options)); if (minimum === -1) { return -1; } - return solveMax(maxWrapper(minimum, central), [minimum, central]); + return solveMax(maxWrapper(minimum, central), [minimum, central], options); } } - const minimum = solve(calc(calcWrapper(minimumValue), globals)); + const minimum = solve(calc(calcWrapper(minimumValue), globals, options)); if (minimum === -1) { return -1; } - const maximum = solve(calc(calcWrapper(maximumValue), globals)); + const maximum = solve(calc(calcWrapper(maximumValue), globals, options)); if (maximum === -1) { return -1; } - return solveClamp(clampNode, minimum, central, maximum); + return solveClamp(clampNode, minimum, central, maximum, options); } -function max(maxNode: FunctionNode, globals: Globals): Calculation | -1 { - return variadicNodesSolver(maxNode, globals, solveMax); +function max(maxNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return variadicNodesSolver(maxNode, globals, options, solveMax); } -function min(minNode: FunctionNode, globals: Globals): Calculation | -1 { - return variadicNodesSolver(minNode, globals, solveMin); +function min(minNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return variadicNodesSolver(minNode, globals, options, solveMin); } const roundingStrategies = new Set([ @@ -405,7 +408,7 @@ const roundingStrategies = new Set([ 'to-zero', ]); -function round(roundNode: FunctionNode, globals: Globals): Calculation | -1 { +function round(roundNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { const nodes: Array = resolveGlobalsAndConstants( [...(roundNode.value.filter(x => !isCommentNode(x) && !isWhitespaceNode(x)))], globals, @@ -452,7 +455,7 @@ function round(roundNode: FunctionNode, globals: Globals): Calculation | -1 { } } - const a = solve(calc(calcWrapper(aValue), globals)); + const a = solve(calc(calcWrapper(aValue), globals, options)); if (a === -1) { return -1; } @@ -465,7 +468,7 @@ function round(roundNode: FunctionNode, globals: Globals): Calculation | -1 { ); } - const b = solve(calc(calcWrapper(bValue), globals)); + const b = solve(calc(calcWrapper(bValue), globals, options)); if (b === -1) { return -1; } @@ -474,71 +477,71 @@ function round(roundNode: FunctionNode, globals: Globals): Calculation | -1 { roundingStrategy = 'nearest'; } - return solveRound(roundNode, roundingStrategy, a, b); + return solveRound(roundNode, roundingStrategy, a, b, options); } -function mod(modNode: FunctionNode, globals: Globals): Calculation | -1 { - return twoCommaSeparatedNodesSolver(modNode, globals, solveMod); +function mod(modNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return twoCommaSeparatedNodesSolver(modNode, globals, options, solveMod); } -function rem(remNode: FunctionNode, globals: Globals): Calculation | -1 { - return twoCommaSeparatedNodesSolver(remNode, globals, solveRem); +function rem(remNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return twoCommaSeparatedNodesSolver(remNode, globals, options, solveRem); } -function abs(absNode: FunctionNode, globals: Globals): Calculation | -1 { - return singleNodeSolver(absNode, globals, solveAbs); +function abs(absNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return singleNodeSolver(absNode, globals, options, solveAbs); } -function sign(signNode: FunctionNode, globals: Globals): Calculation | -1 { - return singleNodeSolver(signNode, globals, solveSign); +function sign(signNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return singleNodeSolver(signNode, globals, options, solveSign); } -function sin(sinNode: FunctionNode, globals: Globals): Calculation | -1 { - return singleNodeSolver(sinNode, globals, solveSin); +function sin(sinNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return singleNodeSolver(sinNode, globals, options, solveSin); } -function cos(codNode: FunctionNode, globals: Globals): Calculation | -1 { - return singleNodeSolver(codNode, globals, solveCos); +function cos(codNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return singleNodeSolver(codNode, globals, options, solveCos); } -function tan(tanNode: FunctionNode, globals: Globals): Calculation | -1 { - return singleNodeSolver(tanNode, globals, solveTan); +function tan(tanNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return singleNodeSolver(tanNode, globals, options, solveTan); } -function asin(asinNode: FunctionNode, globals: Globals): Calculation | -1 { - return singleNodeSolver(asinNode, globals, solveASin); +function asin(asinNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return singleNodeSolver(asinNode, globals, options, solveASin); } -function acos(acosNode: FunctionNode, globals: Globals): Calculation | -1 { - return singleNodeSolver(acosNode, globals, solveACos); +function acos(acosNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return singleNodeSolver(acosNode, globals, options, solveACos); } -function atan(atanNode: FunctionNode, globals: Globals): Calculation | -1 { - return singleNodeSolver(atanNode, globals, solveATan); +function atan(atanNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return singleNodeSolver(atanNode, globals, options, solveATan); } -function atan2(atan2Node: FunctionNode, globals: Globals): Calculation | -1 { - return twoCommaSeparatedNodesSolver(atan2Node, globals, solveATan2); +function atan2(atan2Node: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return twoCommaSeparatedNodesSolver(atan2Node, globals, options, solveATan2); } -function exp(expNode: FunctionNode, globals: Globals): Calculation | -1 { - return singleNodeSolver(expNode, globals, solveExp); +function exp(expNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return singleNodeSolver(expNode, globals, options, solveExp); } -function sqrt(sqrtNode: FunctionNode, globals: Globals): Calculation | -1 { - return singleNodeSolver(sqrtNode, globals, solveSqrt); +function sqrt(sqrtNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return singleNodeSolver(sqrtNode, globals, options, solveSqrt); } -function pow(powNode: FunctionNode, globals: Globals): Calculation | -1 { - return twoCommaSeparatedNodesSolver(powNode, globals, solvePow); +function pow(powNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return twoCommaSeparatedNodesSolver(powNode, globals, options, solvePow); } -function hypot(hypotNode: FunctionNode, globals: Globals): Calculation | -1 { - return variadicNodesSolver(hypotNode, globals, solveHypot); +function hypot(hypotNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return variadicNodesSolver(hypotNode, globals, options, solveHypot); } -function log(logNode: FunctionNode, globals: Globals): Calculation | -1 { - return variadicNodesSolver(logNode, globals, solveLog); +function log(logNode: FunctionNode, globals: Globals, options: conversionOptions): Calculation | -1 { + return variadicNodesSolver(logNode, globals, options, solveLog); } function calcWrapper(v: Array): FunctionNode { diff --git a/packages/css-calc/src/functions/clamp.ts b/packages/css-calc/src/functions/clamp.ts index f1d85f56e..f7a1fbcef 100644 --- a/packages/css-calc/src/functions/clamp.ts +++ b/packages/css-calc/src/functions/clamp.ts @@ -4,9 +4,10 @@ import { convertUnit } from '../unit-conversions'; import { isTokenNode } from '@csstools/css-parser-algorithms'; import { resultToCalculation } from './result-to-calculation'; import { twoOfSameNumeric } from '../util/kind-of-number'; -import { isTokenNumeric } from '@csstools/css-tokenizer'; +import type { conversionOptions } from '../options'; +import { isTokenNumeric, isTokenPercentage } from '@csstools/css-tokenizer'; -export function solveClamp(clampNode: FunctionNode, minimum: TokenNode | -1, central: TokenNode | -1, maximum: TokenNode | -1): Calculation | -1 { +export function solveClamp(clampNode: FunctionNode, minimum: TokenNode | -1, central: TokenNode | -1, maximum: TokenNode | -1, options: conversionOptions): Calculation | -1 { if ( !isTokenNode(minimum) || !isTokenNode(central) || @@ -20,6 +21,10 @@ export function solveClamp(clampNode: FunctionNode, minimum: TokenNode | -1, cen return -1; } + if (!options.rawPercentages && isTokenPercentage(minimumToken)) { + return -1; + } + const centralToken = convertUnit(minimumToken, central.value); if (!twoOfSameNumeric(minimumToken, centralToken)) { return -1; diff --git a/packages/css-calc/src/functions/hypot.ts b/packages/css-calc/src/functions/hypot.ts index 67541514e..95f07695b 100644 --- a/packages/css-calc/src/functions/hypot.ts +++ b/packages/css-calc/src/functions/hypot.ts @@ -4,9 +4,10 @@ import { convertUnit } from '../unit-conversions'; import { isTokenNode } from '@csstools/css-parser-algorithms'; import { resultToCalculation } from './result-to-calculation'; import { arrayOfSameNumeric } from '../util/kind-of-number'; -import { isTokenNumeric } from '@csstools/css-tokenizer'; +import { isTokenNumeric, isTokenPercentage } from '@csstools/css-tokenizer'; +import type { conversionOptions } from '../options'; -export function solveHypot(hypotNode: FunctionNode, solvedNodes: Array): Calculation | -1 { +export function solveHypot(hypotNode: FunctionNode, solvedNodes: Array, options: conversionOptions): Calculation | -1 { const firstSolvedNode = solvedNodes[0]; if (!firstSolvedNode || !isTokenNode(firstSolvedNode)) { return -1; @@ -22,6 +23,10 @@ export function solveHypot(hypotNode: FunctionNode, solvedNodes: Array convertUnit(firstSolvedToken, (x as TokenNode).value)); if (!arrayOfSameNumeric(tokens)) { return -1; diff --git a/packages/css-calc/src/functions/max.ts b/packages/css-calc/src/functions/max.ts index 271320f56..457fd12dc 100644 --- a/packages/css-calc/src/functions/max.ts +++ b/packages/css-calc/src/functions/max.ts @@ -4,9 +4,10 @@ import { convertUnit } from '../unit-conversions'; import { isTokenNode } from '@csstools/css-parser-algorithms'; import { resultToCalculation } from './result-to-calculation'; import { arrayOfSameNumeric } from '../util/kind-of-number'; -import { isTokenNumeric } from '@csstools/css-tokenizer'; +import { isTokenNumeric, isTokenPercentage } from '@csstools/css-tokenizer'; +import type { conversionOptions } from '../options'; -export function solveMax(maxNode: FunctionNode, solvedNodes: Array): Calculation | -1 { +export function solveMax(maxNode: FunctionNode, solvedNodes: Array, options: conversionOptions): Calculation | -1 { const firstSolvedNode = solvedNodes[0]; if (!firstSolvedNode || !isTokenNode(firstSolvedNode)) { return -1; @@ -22,6 +23,10 @@ export function solveMax(maxNode: FunctionNode, solvedNodes: Array convertUnit(firstSolvedToken, (x as TokenNode).value)); if (!arrayOfSameNumeric(tokens)) { return -1; diff --git a/packages/css-calc/src/functions/min.ts b/packages/css-calc/src/functions/min.ts index eab433747..2fdbe259b 100644 --- a/packages/css-calc/src/functions/min.ts +++ b/packages/css-calc/src/functions/min.ts @@ -4,9 +4,10 @@ import { convertUnit } from '../unit-conversions'; import { isTokenNode } from '@csstools/css-parser-algorithms'; import { resultToCalculation } from './result-to-calculation'; import { arrayOfSameNumeric } from '../util/kind-of-number'; -import { isTokenNumeric } from '@csstools/css-tokenizer'; +import { isTokenNumeric, isTokenPercentage } from '@csstools/css-tokenizer'; +import type { conversionOptions } from '../options'; -export function solveMin(minNode: FunctionNode, solvedNodes: Array): Calculation | -1 { +export function solveMin(minNode: FunctionNode, solvedNodes: Array, options: conversionOptions): Calculation | -1 { const firstSolvedNode = solvedNodes[0]; if (!firstSolvedNode || !isTokenNode(firstSolvedNode)) { return -1; @@ -22,6 +23,10 @@ export function solveMin(minNode: FunctionNode, solvedNodes: Array convertUnit(firstSolvedToken, (x as TokenNode).value)); if (!arrayOfSameNumeric(tokens)) { return -1; diff --git a/packages/css-calc/src/functions/round.ts b/packages/css-calc/src/functions/round.ts index 765c1b23b..996de1769 100644 --- a/packages/css-calc/src/functions/round.ts +++ b/packages/css-calc/src/functions/round.ts @@ -3,14 +3,19 @@ import type { FunctionNode, TokenNode } from '@csstools/css-parser-algorithms'; import { convertUnit } from '../unit-conversions'; import { resultToCalculation } from './result-to-calculation'; import { twoOfSameNumeric } from '../util/kind-of-number'; -import { isTokenNumeric } from '@csstools/css-tokenizer'; +import { isTokenNumeric, isTokenPercentage } from '@csstools/css-tokenizer'; +import type { conversionOptions } from '../options'; -export function solveRound(roundNode: FunctionNode, roundingStrategy: string, a: TokenNode, b: TokenNode): Calculation | -1 { +export function solveRound(roundNode: FunctionNode, roundingStrategy: string, a: TokenNode, b: TokenNode, options: conversionOptions): Calculation | -1 { const aToken = a.value; if (!isTokenNumeric(aToken)) { return -1; } + if (!options.rawPercentages && isTokenPercentage(aToken)) { + return -1; + } + const bToken = convertUnit(aToken, b.value); if (!twoOfSameNumeric(aToken, bToken)) { return -1; diff --git a/packages/css-calc/src/functions/sign.ts b/packages/css-calc/src/functions/sign.ts index 1df281107..d65b08c73 100644 --- a/packages/css-calc/src/functions/sign.ts +++ b/packages/css-calc/src/functions/sign.ts @@ -1,11 +1,16 @@ import type { Calculation } from '../calculation'; import type { FunctionNode, TokenNode } from '@csstools/css-parser-algorithms'; import { numberToCalculation } from './result-to-calculation'; -import { isDimensionOrNumber } from '../util/kind-of-number'; +import { isTokenNumeric, isTokenPercentage } from '@csstools/css-tokenizer'; +import type { conversionOptions } from '../options'; -export function solveSign(signNode: FunctionNode, a: TokenNode): Calculation | -1 { +export function solveSign(signNode: FunctionNode, a: TokenNode, options: conversionOptions): Calculation | -1 { const aToken = a.value; - if (!isDimensionOrNumber(aToken)) { + if (!isTokenNumeric(aToken)) { + return -1; + } + + if (!options.rawPercentages && isTokenPercentage(aToken)) { return -1; } diff --git a/packages/css-calc/src/index.ts b/packages/css-calc/src/index.ts index b625da774..bf4a75ec2 100644 --- a/packages/css-calc/src/index.ts +++ b/packages/css-calc/src/index.ts @@ -30,7 +30,7 @@ export function calcFromComponentValues(componentValuesList: Array=100&&(a[0]=NaN);break;case exports.ColorNotation.LCH:!Number.isNaN(a[1])&&reducePrecision(a[1],4)<=0&&(a[2]=NaN);break;case exports.ColorNotation.OKLCH:!Number.isNaN(a[1])&&reducePrecision(a[1],6)<=0&&(a[2]=NaN)}return a}function convertPowerlessComponentsToZeroValuesForDisplay(e,o){const a=[...e];switch(o){case exports.ColorNotation.HSL:(reducePrecision(a[2])<=0||reducePrecision(a[2])>=100)&&(a[0]=NaN,a[1]=NaN),reducePrecision(a[1])<=0&&(a[0]=NaN);break;case exports.ColorNotation.HWB:Math.max(0,reducePrecision(a[1]))+Math.max(0,reducePrecision(a[2]))>=100&&(a[0]=NaN);break;case exports.ColorNotation.Lab:(reducePrecision(a[0])<=0||reducePrecision(a[0])>=100)&&(a[1]=NaN,a[2]=NaN);break;case exports.ColorNotation.LCH:reducePrecision(a[1])<=0&&(a[2]=NaN),(reducePrecision(a[0])<=0||reducePrecision(a[0])>=100)&&(a[1]=NaN,a[2]=NaN);break;case exports.ColorNotation.OKLab:(reducePrecision(a[0])<=0||reducePrecision(a[0])>=1)&&(a[1]=NaN,a[2]=NaN);break;case exports.ColorNotation.OKLCH:reducePrecision(a[1])<=0&&(a[2]=NaN),(reducePrecision(a[0])<=0||reducePrecision(a[0])>=1)&&(a[1]=NaN,a[2]=NaN)}return a}function carryForwardMissingComponents(e,o,a,n){const t=[...a];for(const a of o)Number.isNaN(e[o[a]])&&(t[n[a]]=NaN);return t}function normalizeRelativeColorDataChannels(e){const o=new Map;switch(e.colorNotation){case exports.ColorNotation.RGB:case exports.ColorNotation.HEX:o.set("r",dummyNumberToken(255*e.channels[0])),o.set("g",dummyNumberToken(255*e.channels[1])),o.set("b",dummyNumberToken(255*e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha));break;case exports.ColorNotation.HSL:o.set("h",dummyNumberToken(e.channels[0])),o.set("s",dummyNumberToken(e.channels[1])),o.set("l",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha));break;case exports.ColorNotation.HWB:o.set("h",dummyNumberToken(e.channels[0])),o.set("w",dummyNumberToken(e.channels[1])),o.set("b",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha));break;case exports.ColorNotation.Lab:case exports.ColorNotation.OKLab:o.set("l",dummyNumberToken(e.channels[0])),o.set("a",dummyNumberToken(e.channels[1])),o.set("b",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha));break;case exports.ColorNotation.LCH:case exports.ColorNotation.OKLCH:o.set("l",dummyNumberToken(e.channels[0])),o.set("c",dummyNumberToken(e.channels[1])),o.set("h",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha));break;case exports.ColorNotation.sRGB:case exports.ColorNotation.A98_RGB:case exports.ColorNotation.Display_P3:case exports.ColorNotation.Rec2020:case exports.ColorNotation.Linear_sRGB:case exports.ColorNotation.ProPhoto_RGB:o.set("r",dummyNumberToken(e.channels[0])),o.set("g",dummyNumberToken(e.channels[1])),o.set("b",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha));break;case exports.ColorNotation.XYZ_D50:case exports.ColorNotation.XYZ_D65:o.set("x",dummyNumberToken(e.channels[0])),o.set("y",dummyNumberToken(e.channels[1])),o.set("z",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha))}return o}function noneToZeroInRelativeColorDataChannels(e){const o=new Map(e);for(const[a,n]of e)Number.isNaN(n[4].value)&&o.set(a,dummyNumberToken(0));return o}function dummyNumberToken(e){return Number.isNaN(e)?[a.TokenType.Number,"none",-1,-1,{value:Number.NaN,type:a.NumberType.Number}]:[a.TokenType.Number,e.toString(),-1,-1,{value:e,type:a.NumberType.Number}]}function reducePrecision(e,o=7){if(Number.isNaN(e))return 0;const a=Math.pow(10,o);return Math.round(e*a)/a}function normalize(e,o,a,n){return Math.min(Math.max(e/o,a),n)}const l=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(l,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}function normalize_Color_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(a.isTokenPercentage(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,100,-2147483647,2147483647);return 3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,1,-2147483647,2147483647);return 3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}const i=new Set(["srgb","srgb-linear","display-p3","a98-rgb","prophoto-rgb","rec2020","xyz","xyz-d50","xyz-d65"]);function color$1(e,o){const n=[],s=[],l=[],u=[];let c,p,N=!1,m=!1;const h={colorNotation:exports.ColorNotation.sRGB,channels:[0,0,0],alpha:1,syntaxFlags:new Set([])};let x=n;for(let y=0;y=0){i=u.value[4].value;continue}}return!1}if(!l)return!1;n.push({color:l,percentage:i}),l=!1,i=!1}}if(l&&n.push({color:l,percentage:i}),2!==n.length)return!1;let u=n[0].percentage,c=n[1].percentage;return(!1===u||!(u<0||u>100))&&((!1===c||!(c<0||c>100))&&(!1===u&&!1===c?(u=50,c=50):!1!==u&&!1===c?c=100-u:!1===u&&!1!==c&&(u=100-c),(0!==u||0!==c)&&(!1!==u&&!1!==c&&(u+c>100&&(u=u/(u+c)*100,c=c/(u+c)*100),u+c<100&&(s=(u+c)/100,u=u/(u+c)*100,c=c/(u+c)*100),{a:{color:n[0].color,percentage:u},b:{color:n[1].color,percentage:c},alphaMultiplier:s}))))}function colorMixRectangular(e,o){if(!o)return!1;const a=o.a.color,n=o.b.color,t=o.a.percentage/100;let r=a.channels,s=n.channels,l=exports.ColorNotation.RGB,i=a.alpha;if("number"!=typeof i)return!1;let u=n.alpha;if("number"!=typeof u)return!1;switch(i=Number.isNaN(i)?u:i,u=Number.isNaN(u)?i:u,e){case"srgb":l=exports.ColorNotation.RGB;break;case"srgb-linear":l=exports.ColorNotation.Linear_sRGB;break;case"display-p3":l=exports.ColorNotation.Display_P3;break;case"a98-rgb":l=exports.ColorNotation.A98_RGB;break;case"prophoto-rgb":l=exports.ColorNotation.ProPhoto_RGB;break;case"rec2020":l=exports.ColorNotation.Rec2020;break;case"lab":l=exports.ColorNotation.Lab;break;case"oklab":l=exports.ColorNotation.OKLab;break;case"xyz-d50":l=exports.ColorNotation.XYZ_D50;break;case"xyz":case"xyz-d65":l=exports.ColorNotation.XYZ_D65}r=colorDataTo(a,l).channels,s=colorDataTo(n,l).channels,r[0]=fillInMissingComponent(r[0],s[0]),s[0]=fillInMissingComponent(s[0],r[0]),r[1]=fillInMissingComponent(r[1],s[1]),s[1]=fillInMissingComponent(s[1],r[1]),r[2]=fillInMissingComponent(r[2],s[2]),s[2]=fillInMissingComponent(s[2],r[2]),r[0]=premultiply(r[0],i),r[1]=premultiply(r[1],i),r[2]=premultiply(r[2],i),s[0]=premultiply(s[0],u),s[1]=premultiply(s[1],u),s[2]=premultiply(s[2],u);const c=interpolate(i,u,t),p={colorNotation:l,channels:[un_premultiply(interpolate(r[0],s[0],t),c),un_premultiply(interpolate(r[1],s[1],t),c),un_premultiply(interpolate(r[2],s[2],t),c)],alpha:c*o.alphaMultiplier,syntaxFlags:new Set([exports.SyntaxFlag.ColorMix])};return(o.a.color.syntaxFlags.has(exports.SyntaxFlag.Experimental)||o.b.color.syntaxFlags.has(exports.SyntaxFlag.Experimental))&&p.syntaxFlags.add(exports.SyntaxFlag.Experimental),p}function colorMixPolar(e,o,a){if(!a)return!1;const n=a.a.color,t=a.b.color,r=a.a.percentage/100;let s=n.channels,l=t.channels,i=0,u=0,c=0,p=0,N=0,m=0,h=exports.ColorNotation.RGB,x=n.alpha;if("number"!=typeof x)return!1;let y=t.alpha;if("number"!=typeof y)return!1;switch(x=Number.isNaN(x)?y:x,y=Number.isNaN(y)?x:y,e){case"hsl":h=exports.ColorNotation.HSL;break;case"hwb":h=exports.ColorNotation.HWB;break;case"lch":h=exports.ColorNotation.LCH;break;case"oklch":h=exports.ColorNotation.OKLCH}switch(s=colorDataTo(n,h).channels,l=colorDataTo(t,h).channels,e){case"hsl":case"hwb":i=s[0],u=l[0],c=s[1],p=l[1],N=s[2],m=l[2];break;case"lch":case"oklch":c=s[0],p=l[0],N=s[1],m=l[1],i=s[2],u=l[2]}i=fillInMissingComponent(i,u),Number.isNaN(i)&&(i=0),u=fillInMissingComponent(u,i),Number.isNaN(u)&&(u=0),c=fillInMissingComponent(c,p),p=fillInMissingComponent(p,c),N=fillInMissingComponent(N,m),m=fillInMissingComponent(m,N);const b=u-i;switch(o){case"shorter":b>180?i+=360:b<-180&&(u+=360);break;case"longer":-1800?i+=360:u+=360);break;case"increasing":b<0&&(u+=360);break;case"decreasing":b>0&&(i+=360);break;default:throw new Error("Unknown hue interpolation method")}c=premultiply(c,x),N=premultiply(N,x),p=premultiply(p,y),m=premultiply(m,y);let C=[0,0,0];const d=interpolate(x,y,r);switch(e){case"hsl":case"hwb":C=[interpolate(i,u,r),un_premultiply(interpolate(c,p,r),d),un_premultiply(interpolate(N,m,r),d)];break;case"lch":case"oklch":C=[un_premultiply(interpolate(c,p,r),d),un_premultiply(interpolate(N,m,r),d),interpolate(i,u,r)]}const T={colorNotation:h,channels:C,alpha:d*a.alphaMultiplier,syntaxFlags:new Set([exports.SyntaxFlag.ColorMix])};return(a.a.color.syntaxFlags.has(exports.SyntaxFlag.Experimental)||a.b.color.syntaxFlags.has(exports.SyntaxFlag.Experimental))&&T.syntaxFlags.add(exports.SyntaxFlag.Experimental),T}function fillInMissingComponent(e,o){return Number.isNaN(e)?o:e}function interpolate(e,o,a){return e*a+o*(1-a)}function premultiply(e,o){return Number.isNaN(o)?e:Number.isNaN(e)?NaN:e*o}function un_premultiply(e,o){return 0===o||Number.isNaN(o)?e:Number.isNaN(e)?NaN:e/o}function hex(e){const o=toLowerCaseAZ(e[4].value);if(o.match(/[^a-f0-9]/))return!1;const a={colorNotation:exports.ColorNotation.HEX,channels:[0,0,0],alpha:1,syntaxFlags:new Set([exports.SyntaxFlag.Hex])},n=o.length;if(3===n){const e=o[0],n=o[1],t=o[2];return a.channels=[parseInt(e+e,16)/255,parseInt(n+n,16)/255,parseInt(t+t,16)/255],a}if(6===n){const e=o[0]+o[1],n=o[2]+o[3],t=o[4]+o[5];return a.channels=[parseInt(e,16)/255,parseInt(n,16)/255,parseInt(t,16)/255],a}if(4===n){const e=o[0],n=o[1],t=o[2],r=o[3];return a.channels=[parseInt(e+e,16)/255,parseInt(n+n,16)/255,parseInt(t+t,16)/255],a.alpha=parseInt(r+r,16)/255,a.syntaxFlags.add(exports.SyntaxFlag.HasAlpha),a}if(8===n){const e=o[0]+o[1],n=o[2]+o[3],t=o[4]+o[5],r=o[6]+o[7];return a.channels=[parseInt(e,16)/255,parseInt(n,16)/255,parseInt(t,16)/255],a.alpha=parseInt(r,16)/255,a.syntaxFlags.add(exports.SyntaxFlag.HasAlpha),a}return!1}function normalizeHue(e){if(a.isTokenNumber(e))return e[4].value=e[4].value%360,e[1]=e[4].value.toString(),e;if(a.isTokenDimension(e)){let o=e[4].value;switch(toLowerCaseAZ(e[4].unit)){case"deg":break;case"rad":o=180*e[4].value/Math.PI;break;case"grad":o=.9*e[4].value;break;case"turn":o=360*e[4].value;break;default:return!1}return o%=360,[a.TokenType.Number,o.toString(),e[2],e[3],{value:o,type:a.NumberType.Number}]}return!1}function normalize_legacy_HSL_ChannelValues(e,o,n){if(0===o){const o=normalizeHue(e);return!1!==o&&(a.isTokenDimension(e)&&n.syntaxFlags.add(exports.SyntaxFlag.HasDimensionValues),o)}if(a.isTokenPercentage(e)){3===o?n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageAlpha):n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,1,0,100);return 3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){if(3!==o)return!1;let n=normalize(e[4].value,1,0,100);return 3===o&&(n=normalize(e[4].value,1,0,1)),[a.TokenType.Number,n.toString(),e[2],e[3],{value:n,type:a.NumberType.Number}]}return!1}function normalize_modern_HSL_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(0===o){const o=normalizeHue(e);return!1!==o&&(a.isTokenDimension(e)&&n.syntaxFlags.add(exports.SyntaxFlag.HasDimensionValues),o)}if(a.isTokenPercentage(e)){3===o?n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageAlpha):n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=e[4].value;return 3===o?t=normalize(e[4].value,100,0,1):1===o&&(t=normalize(e[4].value,1,0,2147483647)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=e[4].value;return 3===o?t=normalize(e[4].value,1,0,1):1===o&&(t=normalize(e[4].value,1,0,2147483647)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function threeChannelLegacySyntax(e,o,n,s){const l=[],i=[],u=[],c=[],p={colorNotation:n,channels:[0,0,0],alpha:1,syntaxFlags:new Set(s)};let N=l;for(let o=0;ot.isTokenNode(e)&&a.isTokenComma(e.value)))){const o=hslCommaSeparated(e);if(!1!==o)return o}{const a=hslSpaceSeparated(e,o);if(!1!==a)return a}return!1}function hslCommaSeparated(e){return threeChannelLegacySyntax(e,normalize_legacy_HSL_ChannelValues,exports.ColorNotation.HSL,[exports.SyntaxFlag.LegacyHSL])}function hslSpaceSeparated(e,o){return threeChannelSpaceSeparated(e,normalize_modern_HSL_ChannelValues,exports.ColorNotation.HSL,[],o)}function normalize_HWB_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(0===o){const o=normalizeHue(e);return!1!==o&&(a.isTokenDimension(e)&&n.syntaxFlags.add(exports.SyntaxFlag.HasDimensionValues),o)}if(a.isTokenPercentage(e)){3===o?n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageAlpha):n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=e[4].value;return 3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=e[4].value;return 3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function normalize_Lab_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(a.isTokenPercentage(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,1,0,100);return 1===o||2===o?t=normalize(e[4].value,.8,-2147483647,2147483647):3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,1,0,100);return 1===o||2===o?t=normalize(e[4].value,1,-2147483647,2147483647):3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function lab(e,o){return threeChannelSpaceSeparated(e,normalize_Lab_ChannelValues,exports.ColorNotation.Lab,[],o)}function normalize_LCH_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(2===o){const o=normalizeHue(e);return!1!==o&&(a.isTokenDimension(e)&&n.syntaxFlags.add(exports.SyntaxFlag.HasDimensionValues),o)}if(a.isTokenPercentage(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,1,0,100);return 1===o?t=normalize(e[4].value,100/150,0,2147483647):3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,1,0,100);return 1===o?t=normalize(e[4].value,1,0,2147483647):3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function lch(e,o){return threeChannelSpaceSeparated(e,normalize_LCH_ChannelValues,exports.ColorNotation.LCH,[],o)}const N=new Map;for(const[e,o]of Object.entries(n.namedColors))N.set(e,o);function namedColor(e){const o=N.get(toLowerCaseAZ(e));return!!o&&{colorNotation:exports.ColorNotation.RGB,channels:[o[0]/255,o[1]/255,o[2]/255],alpha:1,syntaxFlags:new Set([exports.SyntaxFlag.ColorKeyword,exports.SyntaxFlag.NamedColor])}}function normalize_OKLab_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(a.isTokenPercentage(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,100,0,1);return 1===o||2===o?t=normalize(e[4].value,250,-2147483647,2147483647):3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,1,0,1);return 1===o||2===o?t=normalize(e[4].value,1,-2147483647,2147483647):3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function oklab(e,o){return threeChannelSpaceSeparated(e,normalize_OKLab_ChannelValues,exports.ColorNotation.OKLab,[],o)}function normalize_OKLCH_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(2===o){const o=normalizeHue(e);return!1!==o&&(a.isTokenDimension(e)&&n.syntaxFlags.add(exports.SyntaxFlag.HasDimensionValues),o)}if(a.isTokenPercentage(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,100,0,1);return 1===o?t=normalize(e[4].value,250,0,2147483647):3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,1,0,1);return 1===o?t=normalize(e[4].value,1,0,2147483647):3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function oklch(e,o){return threeChannelSpaceSeparated(e,normalize_OKLCH_ChannelValues,exports.ColorNotation.OKLCH,[],o)}function normalize_legacy_sRGB_ChannelValues(e,o,n){if(a.isTokenPercentage(e)){3===o?n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageAlpha):n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);const t=normalize(e[4].value,100,0,1);return[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,255,0,1);return 3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function normalize_modern_sRGB_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===e[4].value.toLowerCase())return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(a.isTokenPercentage(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,100,-2147483647,2147483647);return 3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,255,-2147483647,2147483647);return 3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function rgb(e,o){if(e.value.some((e=>t.isTokenNode(e)&&a.isTokenComma(e.value)))){const o=rgbCommaSeparated(e);if(!1!==o)return(!o.syntaxFlags.has(exports.SyntaxFlag.HasNumberValues)||!o.syntaxFlags.has(exports.SyntaxFlag.HasPercentageValues))&&o}else{const a=rgbSpaceSeparated(e,o);if(!1!==a)return a}return!1}function rgbCommaSeparated(e){return threeChannelLegacySyntax(e,normalize_legacy_sRGB_ChannelValues,exports.ColorNotation.RGB,[exports.SyntaxFlag.LegacyRGB])}function rgbSpaceSeparated(e,o){return threeChannelSpaceSeparated(e,normalize_modern_sRGB_ChannelValues,exports.ColorNotation.RGB,[],o)}function XYZ_D50_to_sRGB_Gamut(e){const o=n.XYZ_D50_to_sRGB(e);if(n.inGamut(o))return n.clip(o);let a=e;return a=n.XYZ_D50_to_OKLCH(a),a[0]<1e-6&&(a=[0,0,0]),a[0]>.999999&&(a=[1,0,0]),n.gam_sRGB(n.mapGamutRayTrace(a,oklch_to_lin_srgb,lin_srgb_to_oklch))}function oklch_to_lin_srgb(e){return e=n.OKLCH_to_OKLab(e),e=n.OKLab_to_XYZ(e),n.XYZ_to_lin_sRGB(e)}function lin_srgb_to_oklch(e){return e=n.lin_sRGB_to_XYZ(e),e=n.XYZ_to_OKLab(e),n.OKLab_to_OKLCH(e)}function contrastColor(e,o){let r=!1,s=!1;for(let n=0;nu?[1,1,1]:[0,0,0],l}function XYZ_D50_to_P3_Gamut(e){const o=n.XYZ_D50_to_P3(e);if(n.inGamut(o))return n.clip(o);let a=e;return a=n.XYZ_D50_to_OKLCH(a),a[0]<1e-6&&(a=[0,0,0]),a[0]>.999999&&(a=[1,0,0]),n.gam_P3(n.mapGamutRayTrace(a,oklch_to_lin_p3,lin_p3_to_oklch))}function oklch_to_lin_p3(e){return e=n.OKLCH_to_OKLab(e),e=n.OKLab_to_XYZ(e),n.XYZ_to_lin_P3(e)}function lin_p3_to_oklch(e){return e=n.lin_P3_to_XYZ(e),e=n.XYZ_to_OKLab(e),n.OKLab_to_OKLCH(e)}function toPrecision(e,o=7){e=+e,o=+o;const a=(Math.floor(e)+"").length;if(o>a)return+e.toFixed(o-a);{const n=10**(a-o);return Math.round(e/n)*n}}function serializeWithAlpha(e,o,n,r){const s=[a.TokenType.CloseParen,")",-1,-1,void 0];if("number"==typeof e.alpha){const l=Math.min(1,Math.max(0,toPrecision(Number.isNaN(e.alpha)?0:e.alpha)));return 1===toPrecision(l,4)?new t.FunctionNode(o,s,r):new t.FunctionNode(o,s,[...r,new t.WhitespaceNode([n]),new t.TokenNode([a.TokenType.Delim,"/",-1,-1,{value:"/"}]),new t.WhitespaceNode([n]),new t.TokenNode([a.TokenType.Number,toPrecision(l,4).toString(),-1,-1,{value:e.alpha,type:a.NumberType.Integer}])])}return new t.FunctionNode(o,s,[...r,new t.WhitespaceNode([n]),new t.TokenNode([a.TokenType.Delim,"/",-1,-1,{value:"/"}]),new t.WhitespaceNode([n]),e.alpha])}exports.color=function color(e){if(t.isFunctionNode(e)){switch(toLowerCaseAZ(e.getName())){case"rgb":case"rgba":return rgb(e,color);case"hsl":case"hsla":return hsl(e,color);case"hwb":return o=color,threeChannelSpaceSeparated(e,normalize_HWB_ChannelValues,exports.ColorNotation.HWB,[],o);case"lab":return lab(e,color);case"lch":return lch(e,color);case"oklab":return oklab(e,color);case"oklch":return oklch(e,color);case"color":return color$1(e,color);case"color-mix":return colorMix(e,color);case"contrast-color":return contrastColor(e,color)}}var o;if(t.isTokenNode(e)){if(a.isTokenHash(e.value))return hex(e.value);if(a.isTokenIdent(e.value)){const o=namedColor(e.value[4].value);return!1!==o?o:"transparent"===toLowerCaseAZ(e.value[4].value)&&{colorNotation:exports.ColorNotation.RGB,channels:[0,0,0],alpha:0,syntaxFlags:new Set([exports.SyntaxFlag.ColorKeyword])}}}return!1},exports.colorDataFitsDisplayP3_Gamut=function colorDataFitsDisplayP3_Gamut(e){const o={...e,channels:[...e.channels]};return o.channels=convertPowerlessComponentsToZeroValuesForDisplay(o.channels,o.colorNotation),!colorDataTo(o,exports.ColorNotation.Display_P3).channels.find((e=>e<-1e-5||e>1.00001))},exports.colorDataFitsRGB_Gamut=function colorDataFitsRGB_Gamut(e){const o={...e,channels:[...e.channels]};return o.channels=convertPowerlessComponentsToZeroValuesForDisplay(o.channels,o.colorNotation),!colorDataTo(o,exports.ColorNotation.RGB).channels.find((e=>e<-1e-5||e>1.00001))},exports.serializeHSL=function serializeHSL(e,o=!0){e.channels=convertPowerlessComponentsToZeroValuesForDisplay(e.channels,e.colorNotation);let r=e.channels.map((e=>Number.isNaN(e)?0:e));r=o?n.XYZ_D50_to_HSL(n.sRGB_to_XYZ_D50(XYZ_D50_to_sRGB_Gamut(colorData_to_XYZ_D50(e).channels))):n.XYZ_D50_to_HSL(colorData_to_XYZ_D50(e).channels),r=r.map((e=>Number.isNaN(e)?0:e));const s=Math.min(360,Math.max(0,Math.round(toPrecision(r[0])))),l=Math.min(100,Math.max(0,Math.round(toPrecision(r[1])))),i=Math.min(100,Math.max(0,Math.round(toPrecision(r[2])))),u=[a.TokenType.CloseParen,")",-1,-1,void 0],c=[a.TokenType.Whitespace," ",-1,-1,void 0],p=[a.TokenType.Comma,",",-1,-1,void 0],N=[new t.TokenNode([a.TokenType.Number,s.toString(),-1,-1,{value:r[0],type:a.NumberType.Integer}]),new t.TokenNode(p),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Percentage,l.toString()+"%",-1,-1,{value:r[1]}]),new t.TokenNode(p),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Percentage,i.toString()+"%",-1,-1,{value:r[2]}])];if("number"==typeof e.alpha){const o=Math.min(1,Math.max(0,toPrecision(Number.isNaN(e.alpha)?0:e.alpha)));return 1===toPrecision(o,4)?new t.FunctionNode([a.TokenType.Function,"hsl(",-1,-1,{value:"hsl"}],u,N):new t.FunctionNode([a.TokenType.Function,"hsla(",-1,-1,{value:"hsla"}],u,[...N,new t.TokenNode(p),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,toPrecision(o,4).toString(),-1,-1,{value:e.alpha,type:a.NumberType.Number}])])}return new t.FunctionNode([a.TokenType.Function,"hsla(",-1,-1,{value:"hsla"}],u,[...N,new t.TokenNode(p),new t.WhitespaceNode([c]),e.alpha])},exports.serializeOKLCH=function serializeOKLCH(e){e.channels=convertPowerlessComponentsToZeroValuesForDisplay(e.channels,e.colorNotation);let o=e.channels.map((e=>Number.isNaN(e)?0:e));e.colorNotation!==exports.ColorNotation.OKLCH&&(o=n.XYZ_D50_to_OKLCH(colorData_to_XYZ_D50(e).channels));const r=toPrecision(o[0],6),s=toPrecision(o[1],6),l=toPrecision(o[2],6),i=[a.TokenType.Function,"oklch(",-1,-1,{value:"oklch"}],u=[a.TokenType.Whitespace," ",-1,-1,void 0];return serializeWithAlpha(e,i,u,[new t.TokenNode([a.TokenType.Number,r.toString(),-1,-1,{value:o[0],type:a.NumberType.Number}]),new t.WhitespaceNode([u]),new t.TokenNode([a.TokenType.Number,s.toString(),-1,-1,{value:o[1],type:a.NumberType.Number}]),new t.WhitespaceNode([u]),new t.TokenNode([a.TokenType.Number,l.toString(),-1,-1,{value:o[2],type:a.NumberType.Number}])])},exports.serializeP3=function serializeP3(e,o=!0){e.channels=convertPowerlessComponentsToZeroValuesForDisplay(e.channels,e.colorNotation);let r=e.channels.map((e=>Number.isNaN(e)?0:e));o?r=XYZ_D50_to_P3_Gamut(colorData_to_XYZ_D50(e).channels):e.colorNotation!==exports.ColorNotation.Display_P3&&(r=n.XYZ_D50_to_P3(colorData_to_XYZ_D50(e).channels));const s=o?Math.min(1,Math.max(0,toPrecision(r[0],6))):toPrecision(r[0],6),l=o?Math.min(1,Math.max(0,toPrecision(r[1],6))):toPrecision(r[1],6),i=o?Math.min(1,Math.max(0,toPrecision(r[2],6))):toPrecision(r[2],6),u=[a.TokenType.Function,"color(",-1,-1,{value:"color"}],c=[a.TokenType.Whitespace," ",-1,-1,void 0];return serializeWithAlpha(e,u,c,[new t.TokenNode([a.TokenType.Ident,"display-p3",-1,-1,{value:"display-p3"}]),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,s.toString(),-1,-1,{value:r[0],type:a.NumberType.Number}]),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,l.toString(),-1,-1,{value:r[1],type:a.NumberType.Number}]),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,i.toString(),-1,-1,{value:r[2],type:a.NumberType.Number}])])},exports.serializeRGB=function serializeRGB(e,o=!0){e.channels=convertPowerlessComponentsToZeroValuesForDisplay(e.channels,e.colorNotation);let r=e.channels.map((e=>Number.isNaN(e)?0:e));r=o?XYZ_D50_to_sRGB_Gamut(colorData_to_XYZ_D50(e).channels):n.XYZ_D50_to_sRGB(colorData_to_XYZ_D50(e).channels);const s=Math.min(255,Math.max(0,Math.round(255*toPrecision(r[0])))),l=Math.min(255,Math.max(0,Math.round(255*toPrecision(r[1])))),i=Math.min(255,Math.max(0,Math.round(255*toPrecision(r[2])))),u=[a.TokenType.CloseParen,")",-1,-1,void 0],c=[a.TokenType.Whitespace," ",-1,-1,void 0],p=[a.TokenType.Comma,",",-1,-1,void 0],N=[new t.TokenNode([a.TokenType.Number,s.toString(),-1,-1,{value:Math.min(255,255*Math.max(0,r[0])),type:a.NumberType.Integer}]),new t.TokenNode(p),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,l.toString(),-1,-1,{value:Math.min(255,255*Math.max(0,r[1])),type:a.NumberType.Integer}]),new t.TokenNode(p),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,i.toString(),-1,-1,{value:Math.min(255,255*Math.max(0,r[2])),type:a.NumberType.Integer}])];if("number"==typeof e.alpha){const o=Math.min(1,Math.max(0,toPrecision(Number.isNaN(e.alpha)?0:e.alpha)));return 1===toPrecision(o,4)?new t.FunctionNode([a.TokenType.Function,"rgb(",-1,-1,{value:"rgb"}],u,N):new t.FunctionNode([a.TokenType.Function,"rgba(",-1,-1,{value:"rgba"}],u,[...N,new t.TokenNode(p),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,toPrecision(o,4).toString(),-1,-1,{value:e.alpha,type:a.NumberType.Number}])])}return new t.FunctionNode([a.TokenType.Function,"rgba(",-1,-1,{value:"rgba"}],u,[...N,new t.TokenNode(p),new t.WhitespaceNode([c]),e.alpha])}; +"use strict";var e,o,a=require("@csstools/css-tokenizer"),n=require("@csstools/color-helpers"),t=require("@csstools/css-parser-algorithms"),r=require("@csstools/css-calc");function convertNaNToZero(e){return[Number.isNaN(e[0])?0:e[0],Number.isNaN(e[1])?0:e[1],Number.isNaN(e[2])?0:e[2]]}function colorData_to_XYZ_D50(e){switch(e.colorNotation){case exports.ColorNotation.HEX:case exports.ColorNotation.RGB:case exports.ColorNotation.sRGB:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.sRGB_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.Linear_sRGB:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.lin_sRGB_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.Display_P3:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.P3_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.Rec2020:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.rec_2020_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.A98_RGB:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.a98_RGB_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.ProPhoto_RGB:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.ProPhoto_RGB_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.HSL:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.HSL_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.HWB:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.HWB_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.Lab:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.Lab_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.OKLab:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.OKLab_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.LCH:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.LCH_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.OKLCH:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.OKLCH_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.XYZ_D50:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.XYZ_D50_to_XYZ_D50(convertNaNToZero(e.channels))};case exports.ColorNotation.XYZ_D65:return{...e,colorNotation:exports.ColorNotation.XYZ_D50,channels:n.XYZ_D65_to_XYZ_D50(convertNaNToZero(e.channels))};default:throw new Error("Unsupported color notation")}}exports.ColorNotation=void 0,(e=exports.ColorNotation||(exports.ColorNotation={})).A98_RGB="a98-rgb",e.Display_P3="display-p3",e.HEX="hex",e.HSL="hsl",e.HWB="hwb",e.LCH="lch",e.Lab="lab",e.Linear_sRGB="srgb-linear",e.OKLCH="oklch",e.OKLab="oklab",e.ProPhoto_RGB="prophoto-rgb",e.RGB="rgb",e.sRGB="srgb",e.Rec2020="rec2020",e.XYZ_D50="xyz-d50",e.XYZ_D65="xyz-d65",exports.SyntaxFlag=void 0,(o=exports.SyntaxFlag||(exports.SyntaxFlag={})).ColorKeyword="color-keyword",o.HasAlpha="has-alpha",o.HasDimensionValues="has-dimension-values",o.HasNoneKeywords="has-none-keywords",o.HasNumberValues="has-number-values",o.HasPercentageAlpha="has-percentage-alpha",o.HasPercentageValues="has-percentage-values",o.HasVariableAlpha="has-variable-alpha",o.Hex="hex",o.LegacyHSL="legacy-hsl",o.LegacyRGB="legacy-rgb",o.NamedColor="named-color",o.RelativeColorSyntax="relative-color-syntax",o.ColorMix="color-mix",o.ContrastColor="contrast-color",o.Experimental="experimental";const s=new Set([exports.ColorNotation.A98_RGB,exports.ColorNotation.Display_P3,exports.ColorNotation.HEX,exports.ColorNotation.Linear_sRGB,exports.ColorNotation.ProPhoto_RGB,exports.ColorNotation.RGB,exports.ColorNotation.sRGB,exports.ColorNotation.Rec2020,exports.ColorNotation.XYZ_D50,exports.ColorNotation.XYZ_D65]);function colorDataTo(e,o){const a={...e};if(e.colorNotation!==o){const e=colorData_to_XYZ_D50(a);switch(o){case exports.ColorNotation.HEX:case exports.ColorNotation.RGB:a.colorNotation=exports.ColorNotation.RGB,a.channels=n.XYZ_D50_to_sRGB(e.channels);break;case exports.ColorNotation.sRGB:a.colorNotation=exports.ColorNotation.sRGB,a.channels=n.XYZ_D50_to_sRGB(e.channels);break;case exports.ColorNotation.Linear_sRGB:a.colorNotation=exports.ColorNotation.Linear_sRGB,a.channels=n.XYZ_D50_to_lin_sRGB(e.channels);break;case exports.ColorNotation.Display_P3:a.colorNotation=exports.ColorNotation.Display_P3,a.channels=n.XYZ_D50_to_P3(e.channels);break;case exports.ColorNotation.Rec2020:a.colorNotation=exports.ColorNotation.Rec2020,a.channels=n.XYZ_D50_to_rec_2020(e.channels);break;case exports.ColorNotation.ProPhoto_RGB:a.colorNotation=exports.ColorNotation.ProPhoto_RGB,a.channels=n.XYZ_D50_to_ProPhoto(e.channels);break;case exports.ColorNotation.A98_RGB:a.colorNotation=exports.ColorNotation.A98_RGB,a.channels=n.XYZ_D50_to_a98_RGB(e.channels);break;case exports.ColorNotation.HSL:a.colorNotation=exports.ColorNotation.HSL,a.channels=n.XYZ_D50_to_HSL(e.channels);break;case exports.ColorNotation.HWB:a.colorNotation=exports.ColorNotation.HWB,a.channels=n.XYZ_D50_to_HWB(e.channels);break;case exports.ColorNotation.Lab:a.colorNotation=exports.ColorNotation.Lab,a.channels=n.XYZ_D50_to_Lab(e.channels);break;case exports.ColorNotation.LCH:a.colorNotation=exports.ColorNotation.LCH,a.channels=n.XYZ_D50_to_LCH(e.channels);break;case exports.ColorNotation.OKLCH:a.colorNotation=exports.ColorNotation.OKLCH,a.channels=n.XYZ_D50_to_OKLCH(e.channels);break;case exports.ColorNotation.OKLab:a.colorNotation=exports.ColorNotation.OKLab,a.channels=n.XYZ_D50_to_OKLab(e.channels);break;case exports.ColorNotation.XYZ_D50:a.colorNotation=exports.ColorNotation.XYZ_D50,a.channels=n.XYZ_D50_to_XYZ_D50(e.channels);break;case exports.ColorNotation.XYZ_D65:a.colorNotation=exports.ColorNotation.XYZ_D65,a.channels=n.XYZ_D50_to_XYZ_D65(e.channels);break;default:throw new Error("Unsupported color notation")}}else a.channels=convertNaNToZero(e.channels);if(o===e.colorNotation)a.channels=carryForwardMissingComponents(e.channels,[0,1,2],a.channels,[0,1,2]);else if(s.has(o)&&s.has(e.colorNotation))a.channels=carryForwardMissingComponents(e.channels,[0,1,2],a.channels,[0,1,2]);else switch(o){case exports.ColorNotation.HSL:switch(e.colorNotation){case exports.ColorNotation.HWB:a.channels=carryForwardMissingComponents(e.channels,[0],a.channels,[0]);break;case exports.ColorNotation.Lab:case exports.ColorNotation.OKLab:a.channels=carryForwardMissingComponents(e.channels,[2],a.channels,[0]);break;case exports.ColorNotation.LCH:case exports.ColorNotation.OKLCH:a.channels=carryForwardMissingComponents(e.channels,[0,1,2],a.channels,[2,1,0])}break;case exports.ColorNotation.HWB:switch(e.colorNotation){case exports.ColorNotation.HSL:a.channels=carryForwardMissingComponents(e.channels,[0],a.channels,[0]);break;case exports.ColorNotation.LCH:case exports.ColorNotation.OKLCH:a.channels=carryForwardMissingComponents(e.channels,[0],a.channels,[2])}break;case exports.ColorNotation.Lab:case exports.ColorNotation.OKLab:switch(e.colorNotation){case exports.ColorNotation.HSL:a.channels=carryForwardMissingComponents(e.channels,[0],a.channels,[2]);break;case exports.ColorNotation.Lab:case exports.ColorNotation.OKLab:a.channels=carryForwardMissingComponents(e.channels,[0,1,2],a.channels,[0,1,2]);break;case exports.ColorNotation.LCH:case exports.ColorNotation.OKLCH:a.channels=carryForwardMissingComponents(e.channels,[0],a.channels,[0])}break;case exports.ColorNotation.LCH:case exports.ColorNotation.OKLCH:switch(e.colorNotation){case exports.ColorNotation.HSL:a.channels=carryForwardMissingComponents(e.channels,[0,1,2],a.channels,[2,1,0]);break;case exports.ColorNotation.HWB:a.channels=carryForwardMissingComponents(e.channels,[0],a.channels,[2]);break;case exports.ColorNotation.Lab:case exports.ColorNotation.OKLab:a.channels=carryForwardMissingComponents(e.channels,[0],a.channels,[0]);break;case exports.ColorNotation.LCH:case exports.ColorNotation.OKLCH:a.channels=carryForwardMissingComponents(e.channels,[0,1,2],a.channels,[0,1,2])}}return a.channels=convertPowerlessComponentsToMissingComponents(a.channels,o),a}function convertPowerlessComponentsToMissingComponents(e,o){const a=[...e];switch(o){case exports.ColorNotation.HSL:!Number.isNaN(a[1])&&reducePrecision(a[1],4)<=0&&(a[0]=NaN);break;case exports.ColorNotation.HWB:Math.max(0,reducePrecision(a[1],4))+Math.max(0,reducePrecision(a[2],4))>=100&&(a[0]=NaN);break;case exports.ColorNotation.LCH:!Number.isNaN(a[1])&&reducePrecision(a[1],4)<=0&&(a[2]=NaN);break;case exports.ColorNotation.OKLCH:!Number.isNaN(a[1])&&reducePrecision(a[1],6)<=0&&(a[2]=NaN)}return a}function convertPowerlessComponentsToZeroValuesForDisplay(e,o){const a=[...e];switch(o){case exports.ColorNotation.HSL:(reducePrecision(a[2])<=0||reducePrecision(a[2])>=100)&&(a[0]=NaN,a[1]=NaN),reducePrecision(a[1])<=0&&(a[0]=NaN);break;case exports.ColorNotation.HWB:Math.max(0,reducePrecision(a[1]))+Math.max(0,reducePrecision(a[2]))>=100&&(a[0]=NaN);break;case exports.ColorNotation.Lab:(reducePrecision(a[0])<=0||reducePrecision(a[0])>=100)&&(a[1]=NaN,a[2]=NaN);break;case exports.ColorNotation.LCH:reducePrecision(a[1])<=0&&(a[2]=NaN),(reducePrecision(a[0])<=0||reducePrecision(a[0])>=100)&&(a[1]=NaN,a[2]=NaN);break;case exports.ColorNotation.OKLab:(reducePrecision(a[0])<=0||reducePrecision(a[0])>=1)&&(a[1]=NaN,a[2]=NaN);break;case exports.ColorNotation.OKLCH:reducePrecision(a[1])<=0&&(a[2]=NaN),(reducePrecision(a[0])<=0||reducePrecision(a[0])>=1)&&(a[1]=NaN,a[2]=NaN)}return a}function carryForwardMissingComponents(e,o,a,n){const t=[...a];for(const a of o)Number.isNaN(e[o[a]])&&(t[n[a]]=NaN);return t}function normalizeRelativeColorDataChannels(e){const o=new Map;switch(e.colorNotation){case exports.ColorNotation.RGB:case exports.ColorNotation.HEX:o.set("r",dummyNumberToken(255*e.channels[0])),o.set("g",dummyNumberToken(255*e.channels[1])),o.set("b",dummyNumberToken(255*e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha));break;case exports.ColorNotation.HSL:o.set("h",dummyNumberToken(e.channels[0])),o.set("s",dummyNumberToken(e.channels[1])),o.set("l",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha));break;case exports.ColorNotation.HWB:o.set("h",dummyNumberToken(e.channels[0])),o.set("w",dummyNumberToken(e.channels[1])),o.set("b",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha));break;case exports.ColorNotation.Lab:case exports.ColorNotation.OKLab:o.set("l",dummyNumberToken(e.channels[0])),o.set("a",dummyNumberToken(e.channels[1])),o.set("b",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha));break;case exports.ColorNotation.LCH:case exports.ColorNotation.OKLCH:o.set("l",dummyNumberToken(e.channels[0])),o.set("c",dummyNumberToken(e.channels[1])),o.set("h",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha));break;case exports.ColorNotation.sRGB:case exports.ColorNotation.A98_RGB:case exports.ColorNotation.Display_P3:case exports.ColorNotation.Rec2020:case exports.ColorNotation.Linear_sRGB:case exports.ColorNotation.ProPhoto_RGB:o.set("r",dummyNumberToken(e.channels[0])),o.set("g",dummyNumberToken(e.channels[1])),o.set("b",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha));break;case exports.ColorNotation.XYZ_D50:case exports.ColorNotation.XYZ_D65:o.set("x",dummyNumberToken(e.channels[0])),o.set("y",dummyNumberToken(e.channels[1])),o.set("z",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&o.set("alpha",dummyNumberToken(e.alpha))}return o}function noneToZeroInRelativeColorDataChannels(e){const o=new Map(e);for(const[a,n]of e)Number.isNaN(n[4].value)&&o.set(a,dummyNumberToken(0));return o}function dummyNumberToken(e){return Number.isNaN(e)?[a.TokenType.Number,"none",-1,-1,{value:Number.NaN,type:a.NumberType.Number}]:[a.TokenType.Number,e.toString(),-1,-1,{value:e,type:a.NumberType.Number}]}function reducePrecision(e,o=7){if(Number.isNaN(e))return 0;const a=Math.pow(10,o);return Math.round(e*a)/a}function normalize(e,o,a,n){return Math.min(Math.max(e/o,a),n)}const l=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(l,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}function normalize_Color_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(a.isTokenPercentage(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,100,-2147483647,2147483647);return 3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,1,-2147483647,2147483647);return 3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}const i=new Set(["srgb","srgb-linear","display-p3","a98-rgb","prophoto-rgb","rec2020","xyz","xyz-d50","xyz-d65"]);function color$1(e,o){const n=[],s=[],l=[],u=[];let c,p,N=!1,m=!1;const h={colorNotation:exports.ColorNotation.sRGB,channels:[0,0,0],alpha:1,syntaxFlags:new Set([])};let x=n;for(let y=0;y=0){i=u.value[4].value;continue}}return!1}if(!l)return!1;n.push({color:l,percentage:i}),l=!1,i=!1}}if(l&&n.push({color:l,percentage:i}),2!==n.length)return!1;let u=n[0].percentage,c=n[1].percentage;return(!1===u||!(u<0||u>100))&&((!1===c||!(c<0||c>100))&&(!1===u&&!1===c?(u=50,c=50):!1!==u&&!1===c?c=100-u:!1===u&&!1!==c&&(u=100-c),(0!==u||0!==c)&&(!1!==u&&!1!==c&&(u+c>100&&(u=u/(u+c)*100,c=c/(u+c)*100),u+c<100&&(s=(u+c)/100,u=u/(u+c)*100,c=c/(u+c)*100),{a:{color:n[0].color,percentage:u},b:{color:n[1].color,percentage:c},alphaMultiplier:s}))))}function colorMixRectangular(e,o){if(!o)return!1;const a=o.a.color,n=o.b.color,t=o.a.percentage/100;let r=a.channels,s=n.channels,l=exports.ColorNotation.RGB,i=a.alpha;if("number"!=typeof i)return!1;let u=n.alpha;if("number"!=typeof u)return!1;switch(i=Number.isNaN(i)?u:i,u=Number.isNaN(u)?i:u,e){case"srgb":l=exports.ColorNotation.RGB;break;case"srgb-linear":l=exports.ColorNotation.Linear_sRGB;break;case"display-p3":l=exports.ColorNotation.Display_P3;break;case"a98-rgb":l=exports.ColorNotation.A98_RGB;break;case"prophoto-rgb":l=exports.ColorNotation.ProPhoto_RGB;break;case"rec2020":l=exports.ColorNotation.Rec2020;break;case"lab":l=exports.ColorNotation.Lab;break;case"oklab":l=exports.ColorNotation.OKLab;break;case"xyz-d50":l=exports.ColorNotation.XYZ_D50;break;case"xyz":case"xyz-d65":l=exports.ColorNotation.XYZ_D65}r=colorDataTo(a,l).channels,s=colorDataTo(n,l).channels,r[0]=fillInMissingComponent(r[0],s[0]),s[0]=fillInMissingComponent(s[0],r[0]),r[1]=fillInMissingComponent(r[1],s[1]),s[1]=fillInMissingComponent(s[1],r[1]),r[2]=fillInMissingComponent(r[2],s[2]),s[2]=fillInMissingComponent(s[2],r[2]),r[0]=premultiply(r[0],i),r[1]=premultiply(r[1],i),r[2]=premultiply(r[2],i),s[0]=premultiply(s[0],u),s[1]=premultiply(s[1],u),s[2]=premultiply(s[2],u);const c=interpolate(i,u,t),p={colorNotation:l,channels:[un_premultiply(interpolate(r[0],s[0],t),c),un_premultiply(interpolate(r[1],s[1],t),c),un_premultiply(interpolate(r[2],s[2],t),c)],alpha:c*o.alphaMultiplier,syntaxFlags:new Set([exports.SyntaxFlag.ColorMix])};return(o.a.color.syntaxFlags.has(exports.SyntaxFlag.Experimental)||o.b.color.syntaxFlags.has(exports.SyntaxFlag.Experimental))&&p.syntaxFlags.add(exports.SyntaxFlag.Experimental),p}function colorMixPolar(e,o,a){if(!a)return!1;const n=a.a.color,t=a.b.color,r=a.a.percentage/100;let s=n.channels,l=t.channels,i=0,u=0,c=0,p=0,N=0,m=0,h=exports.ColorNotation.RGB,x=n.alpha;if("number"!=typeof x)return!1;let y=t.alpha;if("number"!=typeof y)return!1;switch(x=Number.isNaN(x)?y:x,y=Number.isNaN(y)?x:y,e){case"hsl":h=exports.ColorNotation.HSL;break;case"hwb":h=exports.ColorNotation.HWB;break;case"lch":h=exports.ColorNotation.LCH;break;case"oklch":h=exports.ColorNotation.OKLCH}switch(s=colorDataTo(n,h).channels,l=colorDataTo(t,h).channels,e){case"hsl":case"hwb":i=s[0],u=l[0],c=s[1],p=l[1],N=s[2],m=l[2];break;case"lch":case"oklch":c=s[0],p=l[0],N=s[1],m=l[1],i=s[2],u=l[2]}i=fillInMissingComponent(i,u),Number.isNaN(i)&&(i=0),u=fillInMissingComponent(u,i),Number.isNaN(u)&&(u=0),c=fillInMissingComponent(c,p),p=fillInMissingComponent(p,c),N=fillInMissingComponent(N,m),m=fillInMissingComponent(m,N);const b=u-i;switch(o){case"shorter":b>180?i+=360:b<-180&&(u+=360);break;case"longer":-1800?i+=360:u+=360);break;case"increasing":b<0&&(u+=360);break;case"decreasing":b>0&&(i+=360);break;default:throw new Error("Unknown hue interpolation method")}c=premultiply(c,x),N=premultiply(N,x),p=premultiply(p,y),m=premultiply(m,y);let C=[0,0,0];const d=interpolate(x,y,r);switch(e){case"hsl":case"hwb":C=[interpolate(i,u,r),un_premultiply(interpolate(c,p,r),d),un_premultiply(interpolate(N,m,r),d)];break;case"lch":case"oklch":C=[un_premultiply(interpolate(c,p,r),d),un_premultiply(interpolate(N,m,r),d),interpolate(i,u,r)]}const T={colorNotation:h,channels:C,alpha:d*a.alphaMultiplier,syntaxFlags:new Set([exports.SyntaxFlag.ColorMix])};return(a.a.color.syntaxFlags.has(exports.SyntaxFlag.Experimental)||a.b.color.syntaxFlags.has(exports.SyntaxFlag.Experimental))&&T.syntaxFlags.add(exports.SyntaxFlag.Experimental),T}function fillInMissingComponent(e,o){return Number.isNaN(e)?o:e}function interpolate(e,o,a){return e*a+o*(1-a)}function premultiply(e,o){return Number.isNaN(o)?e:Number.isNaN(e)?NaN:e*o}function un_premultiply(e,o){return 0===o||Number.isNaN(o)?e:Number.isNaN(e)?NaN:e/o}function hex(e){const o=toLowerCaseAZ(e[4].value);if(o.match(/[^a-f0-9]/))return!1;const a={colorNotation:exports.ColorNotation.HEX,channels:[0,0,0],alpha:1,syntaxFlags:new Set([exports.SyntaxFlag.Hex])},n=o.length;if(3===n){const e=o[0],n=o[1],t=o[2];return a.channels=[parseInt(e+e,16)/255,parseInt(n+n,16)/255,parseInt(t+t,16)/255],a}if(6===n){const e=o[0]+o[1],n=o[2]+o[3],t=o[4]+o[5];return a.channels=[parseInt(e,16)/255,parseInt(n,16)/255,parseInt(t,16)/255],a}if(4===n){const e=o[0],n=o[1],t=o[2],r=o[3];return a.channels=[parseInt(e+e,16)/255,parseInt(n+n,16)/255,parseInt(t+t,16)/255],a.alpha=parseInt(r+r,16)/255,a.syntaxFlags.add(exports.SyntaxFlag.HasAlpha),a}if(8===n){const e=o[0]+o[1],n=o[2]+o[3],t=o[4]+o[5],r=o[6]+o[7];return a.channels=[parseInt(e,16)/255,parseInt(n,16)/255,parseInt(t,16)/255],a.alpha=parseInt(r,16)/255,a.syntaxFlags.add(exports.SyntaxFlag.HasAlpha),a}return!1}function normalizeHue(e){if(a.isTokenNumber(e))return e[4].value=e[4].value%360,e[1]=e[4].value.toString(),e;if(a.isTokenDimension(e)){let o=e[4].value;switch(toLowerCaseAZ(e[4].unit)){case"deg":break;case"rad":o=180*e[4].value/Math.PI;break;case"grad":o=.9*e[4].value;break;case"turn":o=360*e[4].value;break;default:return!1}return o%=360,[a.TokenType.Number,o.toString(),e[2],e[3],{value:o,type:a.NumberType.Number}]}return!1}function normalize_legacy_HSL_ChannelValues(e,o,n){if(0===o){const o=normalizeHue(e);return!1!==o&&(a.isTokenDimension(e)&&n.syntaxFlags.add(exports.SyntaxFlag.HasDimensionValues),o)}if(a.isTokenPercentage(e)){3===o?n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageAlpha):n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,1,0,100);return 3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){if(3!==o)return!1;let n=normalize(e[4].value,1,0,100);return 3===o&&(n=normalize(e[4].value,1,0,1)),[a.TokenType.Number,n.toString(),e[2],e[3],{value:n,type:a.NumberType.Number}]}return!1}function normalize_modern_HSL_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(0===o){const o=normalizeHue(e);return!1!==o&&(a.isTokenDimension(e)&&n.syntaxFlags.add(exports.SyntaxFlag.HasDimensionValues),o)}if(a.isTokenPercentage(e)){3===o?n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageAlpha):n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=e[4].value;return 3===o?t=normalize(e[4].value,100,0,1):1===o&&(t=normalize(e[4].value,1,0,2147483647)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=e[4].value;return 3===o?t=normalize(e[4].value,1,0,1):1===o&&(t=normalize(e[4].value,1,0,2147483647)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function threeChannelLegacySyntax(e,o,n,s){const l=[],i=[],u=[],c=[],p={colorNotation:n,channels:[0,0,0],alpha:1,syntaxFlags:new Set(s)};let N=l;for(let o=0;ot.isTokenNode(e)&&a.isTokenComma(e.value)))){const o=hslCommaSeparated(e);if(!1!==o)return o}{const a=hslSpaceSeparated(e,o);if(!1!==a)return a}return!1}function hslCommaSeparated(e){return threeChannelLegacySyntax(e,normalize_legacy_HSL_ChannelValues,exports.ColorNotation.HSL,[exports.SyntaxFlag.LegacyHSL])}function hslSpaceSeparated(e,o){return threeChannelSpaceSeparated(e,normalize_modern_HSL_ChannelValues,exports.ColorNotation.HSL,[],o)}function normalize_HWB_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(0===o){const o=normalizeHue(e);return!1!==o&&(a.isTokenDimension(e)&&n.syntaxFlags.add(exports.SyntaxFlag.HasDimensionValues),o)}if(a.isTokenPercentage(e)){3===o?n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageAlpha):n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=e[4].value;return 3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=e[4].value;return 3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function normalize_Lab_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(a.isTokenPercentage(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,1,0,100);return 1===o||2===o?t=normalize(e[4].value,.8,-2147483647,2147483647):3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,1,0,100);return 1===o||2===o?t=normalize(e[4].value,1,-2147483647,2147483647):3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function lab(e,o){return threeChannelSpaceSeparated(e,normalize_Lab_ChannelValues,exports.ColorNotation.Lab,[],o)}function normalize_LCH_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(2===o){const o=normalizeHue(e);return!1!==o&&(a.isTokenDimension(e)&&n.syntaxFlags.add(exports.SyntaxFlag.HasDimensionValues),o)}if(a.isTokenPercentage(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,1,0,100);return 1===o?t=normalize(e[4].value,100/150,0,2147483647):3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,1,0,100);return 1===o?t=normalize(e[4].value,1,0,2147483647):3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function lch(e,o){return threeChannelSpaceSeparated(e,normalize_LCH_ChannelValues,exports.ColorNotation.LCH,[],o)}const N=new Map;for(const[e,o]of Object.entries(n.namedColors))N.set(e,o);function namedColor(e){const o=N.get(toLowerCaseAZ(e));return!!o&&{colorNotation:exports.ColorNotation.RGB,channels:[o[0]/255,o[1]/255,o[2]/255],alpha:1,syntaxFlags:new Set([exports.SyntaxFlag.ColorKeyword,exports.SyntaxFlag.NamedColor])}}function normalize_OKLab_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(a.isTokenPercentage(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,100,0,1);return 1===o||2===o?t=normalize(e[4].value,250,-2147483647,2147483647):3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,1,0,1);return 1===o||2===o?t=normalize(e[4].value,1,-2147483647,2147483647):3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function oklab(e,o){return threeChannelSpaceSeparated(e,normalize_OKLab_ChannelValues,exports.ColorNotation.OKLab,[],o)}function normalize_OKLCH_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===toLowerCaseAZ(e[4].value))return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(2===o){const o=normalizeHue(e);return!1!==o&&(a.isTokenDimension(e)&&n.syntaxFlags.add(exports.SyntaxFlag.HasDimensionValues),o)}if(a.isTokenPercentage(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,100,0,1);return 1===o?t=normalize(e[4].value,250,0,2147483647):3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,1,0,1);return 1===o?t=normalize(e[4].value,1,0,2147483647):3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function oklch(e,o){return threeChannelSpaceSeparated(e,normalize_OKLCH_ChannelValues,exports.ColorNotation.OKLCH,[],o)}function normalize_legacy_sRGB_ChannelValues(e,o,n){if(a.isTokenPercentage(e)){3===o?n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageAlpha):n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);const t=normalize(e[4].value,100,0,1);return[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,255,0,1);return 3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function normalize_modern_sRGB_ChannelValues(e,o,n){if(a.isTokenIdent(e)&&"none"===e[4].value.toLowerCase())return n.syntaxFlags.add(exports.SyntaxFlag.HasNoneKeywords),[a.TokenType.Number,"none",e[2],e[3],{value:NaN,type:a.NumberType.Number}];if(a.isTokenPercentage(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasPercentageValues);let t=normalize(e[4].value,100,-2147483647,2147483647);return 3===o&&(t=normalize(e[4].value,100,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}if(a.isTokenNumber(e)){3!==o&&n.syntaxFlags.add(exports.SyntaxFlag.HasNumberValues);let t=normalize(e[4].value,255,-2147483647,2147483647);return 3===o&&(t=normalize(e[4].value,1,0,1)),[a.TokenType.Number,t.toString(),e[2],e[3],{value:t,type:a.NumberType.Number}]}return!1}function rgb(e,o){if(e.value.some((e=>t.isTokenNode(e)&&a.isTokenComma(e.value)))){const o=rgbCommaSeparated(e);if(!1!==o)return(!o.syntaxFlags.has(exports.SyntaxFlag.HasNumberValues)||!o.syntaxFlags.has(exports.SyntaxFlag.HasPercentageValues))&&o}else{const a=rgbSpaceSeparated(e,o);if(!1!==a)return a}return!1}function rgbCommaSeparated(e){return threeChannelLegacySyntax(e,normalize_legacy_sRGB_ChannelValues,exports.ColorNotation.RGB,[exports.SyntaxFlag.LegacyRGB])}function rgbSpaceSeparated(e,o){return threeChannelSpaceSeparated(e,normalize_modern_sRGB_ChannelValues,exports.ColorNotation.RGB,[],o)}function XYZ_D50_to_sRGB_Gamut(e){const o=n.XYZ_D50_to_sRGB(e);if(n.inGamut(o))return n.clip(o);let a=e;return a=n.XYZ_D50_to_OKLCH(a),a[0]<1e-6&&(a=[0,0,0]),a[0]>.999999&&(a=[1,0,0]),n.gam_sRGB(n.mapGamutRayTrace(a,oklch_to_lin_srgb,lin_srgb_to_oklch))}function oklch_to_lin_srgb(e){return e=n.OKLCH_to_OKLab(e),e=n.OKLab_to_XYZ(e),n.XYZ_to_lin_sRGB(e)}function lin_srgb_to_oklch(e){return e=n.lin_sRGB_to_XYZ(e),e=n.XYZ_to_OKLab(e),n.OKLab_to_OKLCH(e)}function contrastColor(e,o){let r=!1,s=!1;for(let n=0;nu?[1,1,1]:[0,0,0],l}function XYZ_D50_to_P3_Gamut(e){const o=n.XYZ_D50_to_P3(e);if(n.inGamut(o))return n.clip(o);let a=e;return a=n.XYZ_D50_to_OKLCH(a),a[0]<1e-6&&(a=[0,0,0]),a[0]>.999999&&(a=[1,0,0]),n.gam_P3(n.mapGamutRayTrace(a,oklch_to_lin_p3,lin_p3_to_oklch))}function oklch_to_lin_p3(e){return e=n.OKLCH_to_OKLab(e),e=n.OKLab_to_XYZ(e),n.XYZ_to_lin_P3(e)}function lin_p3_to_oklch(e){return e=n.lin_P3_to_XYZ(e),e=n.XYZ_to_OKLab(e),n.OKLab_to_OKLCH(e)}function toPrecision(e,o=7){e=+e,o=+o;const a=(Math.floor(e)+"").length;if(o>a)return+e.toFixed(o-a);{const n=10**(a-o);return Math.round(e/n)*n}}function serializeWithAlpha(e,o,n,r){const s=[a.TokenType.CloseParen,")",-1,-1,void 0];if("number"==typeof e.alpha){const l=Math.min(1,Math.max(0,toPrecision(Number.isNaN(e.alpha)?0:e.alpha)));return 1===toPrecision(l,4)?new t.FunctionNode(o,s,r):new t.FunctionNode(o,s,[...r,new t.WhitespaceNode([n]),new t.TokenNode([a.TokenType.Delim,"/",-1,-1,{value:"/"}]),new t.WhitespaceNode([n]),new t.TokenNode([a.TokenType.Number,toPrecision(l,4).toString(),-1,-1,{value:e.alpha,type:a.NumberType.Integer}])])}return new t.FunctionNode(o,s,[...r,new t.WhitespaceNode([n]),new t.TokenNode([a.TokenType.Delim,"/",-1,-1,{value:"/"}]),new t.WhitespaceNode([n]),e.alpha])}exports.color=function color(e){if(t.isFunctionNode(e)){switch(toLowerCaseAZ(e.getName())){case"rgb":case"rgba":return rgb(e,color);case"hsl":case"hsla":return hsl(e,color);case"hwb":return o=color,threeChannelSpaceSeparated(e,normalize_HWB_ChannelValues,exports.ColorNotation.HWB,[],o);case"lab":return lab(e,color);case"lch":return lch(e,color);case"oklab":return oklab(e,color);case"oklch":return oklch(e,color);case"color":return color$1(e,color);case"color-mix":return colorMix(e,color);case"contrast-color":return contrastColor(e,color)}}var o;if(t.isTokenNode(e)){if(a.isTokenHash(e.value))return hex(e.value);if(a.isTokenIdent(e.value)){const o=namedColor(e.value[4].value);return!1!==o?o:"transparent"===toLowerCaseAZ(e.value[4].value)&&{colorNotation:exports.ColorNotation.RGB,channels:[0,0,0],alpha:0,syntaxFlags:new Set([exports.SyntaxFlag.ColorKeyword])}}}return!1},exports.colorDataFitsDisplayP3_Gamut=function colorDataFitsDisplayP3_Gamut(e){const o={...e,channels:[...e.channels]};return o.channels=convertPowerlessComponentsToZeroValuesForDisplay(o.channels,o.colorNotation),!colorDataTo(o,exports.ColorNotation.Display_P3).channels.find((e=>e<-1e-5||e>1.00001))},exports.colorDataFitsRGB_Gamut=function colorDataFitsRGB_Gamut(e){const o={...e,channels:[...e.channels]};return o.channels=convertPowerlessComponentsToZeroValuesForDisplay(o.channels,o.colorNotation),!colorDataTo(o,exports.ColorNotation.RGB).channels.find((e=>e<-1e-5||e>1.00001))},exports.serializeHSL=function serializeHSL(e,o=!0){e.channels=convertPowerlessComponentsToZeroValuesForDisplay(e.channels,e.colorNotation);let r=e.channels.map((e=>Number.isNaN(e)?0:e));r=o?n.XYZ_D50_to_HSL(n.sRGB_to_XYZ_D50(XYZ_D50_to_sRGB_Gamut(colorData_to_XYZ_D50(e).channels))):n.XYZ_D50_to_HSL(colorData_to_XYZ_D50(e).channels),r=r.map((e=>Number.isNaN(e)?0:e));const s=Math.min(360,Math.max(0,Math.round(toPrecision(r[0])))),l=Math.min(100,Math.max(0,Math.round(toPrecision(r[1])))),i=Math.min(100,Math.max(0,Math.round(toPrecision(r[2])))),u=[a.TokenType.CloseParen,")",-1,-1,void 0],c=[a.TokenType.Whitespace," ",-1,-1,void 0],p=[a.TokenType.Comma,",",-1,-1,void 0],N=[new t.TokenNode([a.TokenType.Number,s.toString(),-1,-1,{value:r[0],type:a.NumberType.Integer}]),new t.TokenNode(p),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Percentage,l.toString()+"%",-1,-1,{value:r[1]}]),new t.TokenNode(p),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Percentage,i.toString()+"%",-1,-1,{value:r[2]}])];if("number"==typeof e.alpha){const o=Math.min(1,Math.max(0,toPrecision(Number.isNaN(e.alpha)?0:e.alpha)));return 1===toPrecision(o,4)?new t.FunctionNode([a.TokenType.Function,"hsl(",-1,-1,{value:"hsl"}],u,N):new t.FunctionNode([a.TokenType.Function,"hsla(",-1,-1,{value:"hsla"}],u,[...N,new t.TokenNode(p),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,toPrecision(o,4).toString(),-1,-1,{value:e.alpha,type:a.NumberType.Number}])])}return new t.FunctionNode([a.TokenType.Function,"hsla(",-1,-1,{value:"hsla"}],u,[...N,new t.TokenNode(p),new t.WhitespaceNode([c]),e.alpha])},exports.serializeOKLCH=function serializeOKLCH(e){e.channels=convertPowerlessComponentsToZeroValuesForDisplay(e.channels,e.colorNotation);let o=e.channels.map((e=>Number.isNaN(e)?0:e));e.colorNotation!==exports.ColorNotation.OKLCH&&(o=n.XYZ_D50_to_OKLCH(colorData_to_XYZ_D50(e).channels));const r=toPrecision(o[0],6),s=toPrecision(o[1],6),l=toPrecision(o[2],6),i=[a.TokenType.Function,"oklch(",-1,-1,{value:"oklch"}],u=[a.TokenType.Whitespace," ",-1,-1,void 0];return serializeWithAlpha(e,i,u,[new t.TokenNode([a.TokenType.Number,r.toString(),-1,-1,{value:o[0],type:a.NumberType.Number}]),new t.WhitespaceNode([u]),new t.TokenNode([a.TokenType.Number,s.toString(),-1,-1,{value:o[1],type:a.NumberType.Number}]),new t.WhitespaceNode([u]),new t.TokenNode([a.TokenType.Number,l.toString(),-1,-1,{value:o[2],type:a.NumberType.Number}])])},exports.serializeP3=function serializeP3(e,o=!0){e.channels=convertPowerlessComponentsToZeroValuesForDisplay(e.channels,e.colorNotation);let r=e.channels.map((e=>Number.isNaN(e)?0:e));o?r=XYZ_D50_to_P3_Gamut(colorData_to_XYZ_D50(e).channels):e.colorNotation!==exports.ColorNotation.Display_P3&&(r=n.XYZ_D50_to_P3(colorData_to_XYZ_D50(e).channels));const s=o?Math.min(1,Math.max(0,toPrecision(r[0],6))):toPrecision(r[0],6),l=o?Math.min(1,Math.max(0,toPrecision(r[1],6))):toPrecision(r[1],6),i=o?Math.min(1,Math.max(0,toPrecision(r[2],6))):toPrecision(r[2],6),u=[a.TokenType.Function,"color(",-1,-1,{value:"color"}],c=[a.TokenType.Whitespace," ",-1,-1,void 0];return serializeWithAlpha(e,u,c,[new t.TokenNode([a.TokenType.Ident,"display-p3",-1,-1,{value:"display-p3"}]),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,s.toString(),-1,-1,{value:r[0],type:a.NumberType.Number}]),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,l.toString(),-1,-1,{value:r[1],type:a.NumberType.Number}]),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,i.toString(),-1,-1,{value:r[2],type:a.NumberType.Number}])])},exports.serializeRGB=function serializeRGB(e,o=!0){e.channels=convertPowerlessComponentsToZeroValuesForDisplay(e.channels,e.colorNotation);let r=e.channels.map((e=>Number.isNaN(e)?0:e));r=o?XYZ_D50_to_sRGB_Gamut(colorData_to_XYZ_D50(e).channels):n.XYZ_D50_to_sRGB(colorData_to_XYZ_D50(e).channels);const s=Math.min(255,Math.max(0,Math.round(255*toPrecision(r[0])))),l=Math.min(255,Math.max(0,Math.round(255*toPrecision(r[1])))),i=Math.min(255,Math.max(0,Math.round(255*toPrecision(r[2])))),u=[a.TokenType.CloseParen,")",-1,-1,void 0],c=[a.TokenType.Whitespace," ",-1,-1,void 0],p=[a.TokenType.Comma,",",-1,-1,void 0],N=[new t.TokenNode([a.TokenType.Number,s.toString(),-1,-1,{value:Math.min(255,255*Math.max(0,r[0])),type:a.NumberType.Integer}]),new t.TokenNode(p),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,l.toString(),-1,-1,{value:Math.min(255,255*Math.max(0,r[1])),type:a.NumberType.Integer}]),new t.TokenNode(p),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,i.toString(),-1,-1,{value:Math.min(255,255*Math.max(0,r[2])),type:a.NumberType.Integer}])];if("number"==typeof e.alpha){const o=Math.min(1,Math.max(0,toPrecision(Number.isNaN(e.alpha)?0:e.alpha)));return 1===toPrecision(o,4)?new t.FunctionNode([a.TokenType.Function,"rgb(",-1,-1,{value:"rgb"}],u,N):new t.FunctionNode([a.TokenType.Function,"rgba(",-1,-1,{value:"rgba"}],u,[...N,new t.TokenNode(p),new t.WhitespaceNode([c]),new t.TokenNode([a.TokenType.Number,toPrecision(o,4).toString(),-1,-1,{value:e.alpha,type:a.NumberType.Number}])])}return new t.FunctionNode([a.TokenType.Function,"rgba(",-1,-1,{value:"rgba"}],u,[...N,new t.TokenNode(p),new t.WhitespaceNode([c]),e.alpha])}; diff --git a/packages/css-color-parser/dist/index.mjs b/packages/css-color-parser/dist/index.mjs index 620c0ba6c..810570d28 100644 --- a/packages/css-color-parser/dist/index.mjs +++ b/packages/css-color-parser/dist/index.mjs @@ -1 +1 @@ -import{TokenType as e,NumberType as a,isTokenIdent as n,isTokenPercentage as r,isTokenNumber as o,isTokenDelim as t,isTokenNumeric as l,isTokenComma as s,isTokenDimension as u,isTokenHash as c}from"@csstools/css-tokenizer";import{XYZ_D65_to_XYZ_D50 as i,XYZ_D50_to_XYZ_D50 as h,OKLCH_to_XYZ_D50 as m,LCH_to_XYZ_D50 as p,OKLab_to_XYZ_D50 as N,Lab_to_XYZ_D50 as b,HWB_to_XYZ_D50 as v,HSL_to_XYZ_D50 as g,ProPhoto_RGB_to_XYZ_D50 as f,a98_RGB_to_XYZ_D50 as d,rec_2020_to_XYZ_D50 as y,P3_to_XYZ_D50 as _,lin_sRGB_to_XYZ_D50 as C,sRGB_to_XYZ_D50 as w,XYZ_D50_to_XYZ_D65 as x,XYZ_D50_to_OKLab as H,XYZ_D50_to_OKLCH as L,XYZ_D50_to_LCH as k,XYZ_D50_to_Lab as S,XYZ_D50_to_HWB as P,XYZ_D50_to_HSL as z,XYZ_D50_to_a98_RGB as D,XYZ_D50_to_ProPhoto as F,XYZ_D50_to_rec_2020 as Z,XYZ_D50_to_P3 as M,XYZ_D50_to_lin_sRGB as R,XYZ_D50_to_sRGB as B,namedColors as G,inGamut as T,clip as V,gam_sRGB as A,mapGamutRayTrace as X,OKLCH_to_OKLab as Y,OKLab_to_XYZ as K,XYZ_to_lin_sRGB as I,lin_sRGB_to_XYZ as O,XYZ_to_OKLab as E,OKLab_to_OKLCH as W,contrast_ratio_wcag_2_1 as U,gam_P3 as $,XYZ_to_lin_P3 as j,lin_P3_to_XYZ as q}from"@csstools/color-helpers";import{isWhitespaceNode as J,isCommentNode as Q,isTokenNode as ee,isFunctionNode as ae,TokenNode as ne,FunctionNode as re,WhitespaceNode as oe}from"@csstools/css-parser-algorithms";import{mathFunctionNames as te,calcFromComponentValues as le}from"@csstools/css-calc";var se,ue;function convertNaNToZero(e){return[Number.isNaN(e[0])?0:e[0],Number.isNaN(e[1])?0:e[1],Number.isNaN(e[2])?0:e[2]]}function colorData_to_XYZ_D50(e){switch(e.colorNotation){case se.HEX:case se.RGB:case se.sRGB:return{...e,colorNotation:se.XYZ_D50,channels:w(convertNaNToZero(e.channels))};case se.Linear_sRGB:return{...e,colorNotation:se.XYZ_D50,channels:C(convertNaNToZero(e.channels))};case se.Display_P3:return{...e,colorNotation:se.XYZ_D50,channels:_(convertNaNToZero(e.channels))};case se.Rec2020:return{...e,colorNotation:se.XYZ_D50,channels:y(convertNaNToZero(e.channels))};case se.A98_RGB:return{...e,colorNotation:se.XYZ_D50,channels:d(convertNaNToZero(e.channels))};case se.ProPhoto_RGB:return{...e,colorNotation:se.XYZ_D50,channels:f(convertNaNToZero(e.channels))};case se.HSL:return{...e,colorNotation:se.XYZ_D50,channels:g(convertNaNToZero(e.channels))};case se.HWB:return{...e,colorNotation:se.XYZ_D50,channels:v(convertNaNToZero(e.channels))};case se.Lab:return{...e,colorNotation:se.XYZ_D50,channels:b(convertNaNToZero(e.channels))};case se.OKLab:return{...e,colorNotation:se.XYZ_D50,channels:N(convertNaNToZero(e.channels))};case se.LCH:return{...e,colorNotation:se.XYZ_D50,channels:p(convertNaNToZero(e.channels))};case se.OKLCH:return{...e,colorNotation:se.XYZ_D50,channels:m(convertNaNToZero(e.channels))};case se.XYZ_D50:return{...e,colorNotation:se.XYZ_D50,channels:h(convertNaNToZero(e.channels))};case se.XYZ_D65:return{...e,colorNotation:se.XYZ_D50,channels:i(convertNaNToZero(e.channels))};default:throw new Error("Unsupported color notation")}}!function(e){e.A98_RGB="a98-rgb",e.Display_P3="display-p3",e.HEX="hex",e.HSL="hsl",e.HWB="hwb",e.LCH="lch",e.Lab="lab",e.Linear_sRGB="srgb-linear",e.OKLCH="oklch",e.OKLab="oklab",e.ProPhoto_RGB="prophoto-rgb",e.RGB="rgb",e.sRGB="srgb",e.Rec2020="rec2020",e.XYZ_D50="xyz-d50",e.XYZ_D65="xyz-d65"}(se||(se={})),function(e){e.ColorKeyword="color-keyword",e.HasAlpha="has-alpha",e.HasDimensionValues="has-dimension-values",e.HasNoneKeywords="has-none-keywords",e.HasNumberValues="has-number-values",e.HasPercentageAlpha="has-percentage-alpha",e.HasPercentageValues="has-percentage-values",e.HasVariableAlpha="has-variable-alpha",e.Hex="hex",e.LegacyHSL="legacy-hsl",e.LegacyRGB="legacy-rgb",e.NamedColor="named-color",e.RelativeColorSyntax="relative-color-syntax",e.ColorMix="color-mix",e.ContrastColor="contrast-color",e.Experimental="experimental"}(ue||(ue={}));const ce=new Set([se.A98_RGB,se.Display_P3,se.HEX,se.Linear_sRGB,se.ProPhoto_RGB,se.RGB,se.sRGB,se.Rec2020,se.XYZ_D50,se.XYZ_D65]);function colorDataTo(e,a){const n={...e};if(e.colorNotation!==a){const e=colorData_to_XYZ_D50(n);switch(a){case se.HEX:case se.RGB:n.colorNotation=se.RGB,n.channels=B(e.channels);break;case se.sRGB:n.colorNotation=se.sRGB,n.channels=B(e.channels);break;case se.Linear_sRGB:n.colorNotation=se.Linear_sRGB,n.channels=R(e.channels);break;case se.Display_P3:n.colorNotation=se.Display_P3,n.channels=M(e.channels);break;case se.Rec2020:n.colorNotation=se.Rec2020,n.channels=Z(e.channels);break;case se.ProPhoto_RGB:n.colorNotation=se.ProPhoto_RGB,n.channels=F(e.channels);break;case se.A98_RGB:n.colorNotation=se.A98_RGB,n.channels=D(e.channels);break;case se.HSL:n.colorNotation=se.HSL,n.channels=z(e.channels);break;case se.HWB:n.colorNotation=se.HWB,n.channels=P(e.channels);break;case se.Lab:n.colorNotation=se.Lab,n.channels=S(e.channels);break;case se.LCH:n.colorNotation=se.LCH,n.channels=k(e.channels);break;case se.OKLCH:n.colorNotation=se.OKLCH,n.channels=L(e.channels);break;case se.OKLab:n.colorNotation=se.OKLab,n.channels=H(e.channels);break;case se.XYZ_D50:n.colorNotation=se.XYZ_D50,n.channels=h(e.channels);break;case se.XYZ_D65:n.colorNotation=se.XYZ_D65,n.channels=x(e.channels);break;default:throw new Error("Unsupported color notation")}}else n.channels=convertNaNToZero(e.channels);if(a===e.colorNotation)n.channels=carryForwardMissingComponents(e.channels,[0,1,2],n.channels,[0,1,2]);else if(ce.has(a)&&ce.has(e.colorNotation))n.channels=carryForwardMissingComponents(e.channels,[0,1,2],n.channels,[0,1,2]);else switch(a){case se.HSL:switch(e.colorNotation){case se.HWB:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[0]);break;case se.Lab:case se.OKLab:n.channels=carryForwardMissingComponents(e.channels,[2],n.channels,[0]);break;case se.LCH:case se.OKLCH:n.channels=carryForwardMissingComponents(e.channels,[0,1,2],n.channels,[2,1,0])}break;case se.HWB:switch(e.colorNotation){case se.HSL:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[0]);break;case se.LCH:case se.OKLCH:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[2])}break;case se.Lab:case se.OKLab:switch(e.colorNotation){case se.HSL:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[2]);break;case se.Lab:case se.OKLab:n.channels=carryForwardMissingComponents(e.channels,[0,1,2],n.channels,[0,1,2]);break;case se.LCH:case se.OKLCH:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[0])}break;case se.LCH:case se.OKLCH:switch(e.colorNotation){case se.HSL:n.channels=carryForwardMissingComponents(e.channels,[0,1,2],n.channels,[2,1,0]);break;case se.HWB:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[2]);break;case se.Lab:case se.OKLab:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[0]);break;case se.LCH:case se.OKLCH:n.channels=carryForwardMissingComponents(e.channels,[0,1,2],n.channels,[0,1,2])}}return n.channels=convertPowerlessComponentsToMissingComponents(n.channels,a),n}function convertPowerlessComponentsToMissingComponents(e,a){const n=[...e];switch(a){case se.HSL:!Number.isNaN(n[1])&&reducePrecision(n[1],4)<=0&&(n[0]=NaN);break;case se.HWB:Math.max(0,reducePrecision(n[1],4))+Math.max(0,reducePrecision(n[2],4))>=100&&(n[0]=NaN);break;case se.LCH:!Number.isNaN(n[1])&&reducePrecision(n[1],4)<=0&&(n[2]=NaN);break;case se.OKLCH:!Number.isNaN(n[1])&&reducePrecision(n[1],6)<=0&&(n[2]=NaN)}return n}function convertPowerlessComponentsToZeroValuesForDisplay(e,a){const n=[...e];switch(a){case se.HSL:(reducePrecision(n[2])<=0||reducePrecision(n[2])>=100)&&(n[0]=NaN,n[1]=NaN),reducePrecision(n[1])<=0&&(n[0]=NaN);break;case se.HWB:Math.max(0,reducePrecision(n[1]))+Math.max(0,reducePrecision(n[2]))>=100&&(n[0]=NaN);break;case se.Lab:(reducePrecision(n[0])<=0||reducePrecision(n[0])>=100)&&(n[1]=NaN,n[2]=NaN);break;case se.LCH:reducePrecision(n[1])<=0&&(n[2]=NaN),(reducePrecision(n[0])<=0||reducePrecision(n[0])>=100)&&(n[1]=NaN,n[2]=NaN);break;case se.OKLab:(reducePrecision(n[0])<=0||reducePrecision(n[0])>=1)&&(n[1]=NaN,n[2]=NaN);break;case se.OKLCH:reducePrecision(n[1])<=0&&(n[2]=NaN),(reducePrecision(n[0])<=0||reducePrecision(n[0])>=1)&&(n[1]=NaN,n[2]=NaN)}return n}function carryForwardMissingComponents(e,a,n,r){const o=[...n];for(const n of a)Number.isNaN(e[a[n]])&&(o[r[n]]=NaN);return o}function normalizeRelativeColorDataChannels(e){const a=new Map;switch(e.colorNotation){case se.RGB:case se.HEX:a.set("r",dummyNumberToken(255*e.channels[0])),a.set("g",dummyNumberToken(255*e.channels[1])),a.set("b",dummyNumberToken(255*e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha));break;case se.HSL:a.set("h",dummyNumberToken(e.channels[0])),a.set("s",dummyNumberToken(e.channels[1])),a.set("l",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha));break;case se.HWB:a.set("h",dummyNumberToken(e.channels[0])),a.set("w",dummyNumberToken(e.channels[1])),a.set("b",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha));break;case se.Lab:case se.OKLab:a.set("l",dummyNumberToken(e.channels[0])),a.set("a",dummyNumberToken(e.channels[1])),a.set("b",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha));break;case se.LCH:case se.OKLCH:a.set("l",dummyNumberToken(e.channels[0])),a.set("c",dummyNumberToken(e.channels[1])),a.set("h",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha));break;case se.sRGB:case se.A98_RGB:case se.Display_P3:case se.Rec2020:case se.Linear_sRGB:case se.ProPhoto_RGB:a.set("r",dummyNumberToken(e.channels[0])),a.set("g",dummyNumberToken(e.channels[1])),a.set("b",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha));break;case se.XYZ_D50:case se.XYZ_D65:a.set("x",dummyNumberToken(e.channels[0])),a.set("y",dummyNumberToken(e.channels[1])),a.set("z",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha))}return a}function noneToZeroInRelativeColorDataChannels(e){const a=new Map(e);for(const[n,r]of e)Number.isNaN(r[4].value)&&a.set(n,dummyNumberToken(0));return a}function dummyNumberToken(n){return Number.isNaN(n)?[e.Number,"none",-1,-1,{value:Number.NaN,type:a.Number}]:[e.Number,n.toString(),-1,-1,{value:n,type:a.Number}]}function reducePrecision(e,a=7){if(Number.isNaN(e))return 0;const n=Math.pow(10,a);return Math.round(e*n)/n}function colorDataFitsRGB_Gamut(e){const a={...e,channels:[...e.channels]};a.channels=convertPowerlessComponentsToZeroValuesForDisplay(a.channels,a.colorNotation);return!colorDataTo(a,se.RGB).channels.find((e=>e<-1e-5||e>1.00001))}function colorDataFitsDisplayP3_Gamut(e){const a={...e,channels:[...e.channels]};a.channels=convertPowerlessComponentsToZeroValuesForDisplay(a.channels,a.colorNotation);return!colorDataTo(a,se.Display_P3).channels.find((e=>e<-1e-5||e>1.00001))}function normalize(e,a,n,r){return Math.min(Math.max(e/a,n),r)}const ie=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(ie,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}function normalize_Color_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(r(t)){3!==l&&s.syntaxFlags.add(ue.HasPercentageValues);let n=normalize(t[4].value,100,-2147483647,2147483647);return 3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=normalize(t[4].value,1,-2147483647,2147483647);return 3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}const he=new Set(["srgb","srgb-linear","display-p3","a98-rgb","prophoto-rgb","rec2020","xyz","xyz-d50","xyz-d65"]);function color$1(e,a){const r=[],s=[],u=[],c=[];let i,h,m=!1,p=!1;const N={colorNotation:se.sRGB,channels:[0,0,0],alpha:1,syntaxFlags:new Set([])};let b=r;for(let o=0;o=0){u=c.value[4].value;continue}}return!1}if(!t)return!1;n.push({color:t,percentage:u}),t=!1,u=!1}}if(t&&n.push({color:t,percentage:u}),2!==n.length)return!1;let c=n[0].percentage,i=n[1].percentage;return(!1===c||!(c<0||c>100))&&((!1===i||!(i<0||i>100))&&(!1===c&&!1===i?(c=50,i=50):!1!==c&&!1===i?i=100-c:!1===c&&!1!==i&&(c=100-i),(0!==c||0!==i)&&(!1!==c&&!1!==i&&(c+i>100&&(c=c/(c+i)*100,i=i/(c+i)*100),c+i<100&&(o=(c+i)/100,c=c/(c+i)*100,i=i/(c+i)*100),{a:{color:n[0].color,percentage:c},b:{color:n[1].color,percentage:i},alphaMultiplier:o}))))}function colorMixRectangular(e,a){if(!a)return!1;const n=a.a.color,r=a.b.color,o=a.a.percentage/100;let t=n.channels,l=r.channels,s=se.RGB,u=n.alpha;if("number"!=typeof u)return!1;let c=r.alpha;if("number"!=typeof c)return!1;switch(u=Number.isNaN(u)?c:u,c=Number.isNaN(c)?u:c,e){case"srgb":s=se.RGB;break;case"srgb-linear":s=se.Linear_sRGB;break;case"display-p3":s=se.Display_P3;break;case"a98-rgb":s=se.A98_RGB;break;case"prophoto-rgb":s=se.ProPhoto_RGB;break;case"rec2020":s=se.Rec2020;break;case"lab":s=se.Lab;break;case"oklab":s=se.OKLab;break;case"xyz-d50":s=se.XYZ_D50;break;case"xyz":case"xyz-d65":s=se.XYZ_D65}t=colorDataTo(n,s).channels,l=colorDataTo(r,s).channels,t[0]=fillInMissingComponent(t[0],l[0]),l[0]=fillInMissingComponent(l[0],t[0]),t[1]=fillInMissingComponent(t[1],l[1]),l[1]=fillInMissingComponent(l[1],t[1]),t[2]=fillInMissingComponent(t[2],l[2]),l[2]=fillInMissingComponent(l[2],t[2]),t[0]=premultiply(t[0],u),t[1]=premultiply(t[1],u),t[2]=premultiply(t[2],u),l[0]=premultiply(l[0],c),l[1]=premultiply(l[1],c),l[2]=premultiply(l[2],c);const i=interpolate(u,c,o),h={colorNotation:s,channels:[un_premultiply(interpolate(t[0],l[0],o),i),un_premultiply(interpolate(t[1],l[1],o),i),un_premultiply(interpolate(t[2],l[2],o),i)],alpha:i*a.alphaMultiplier,syntaxFlags:new Set([ue.ColorMix])};return(a.a.color.syntaxFlags.has(ue.Experimental)||a.b.color.syntaxFlags.has(ue.Experimental))&&h.syntaxFlags.add(ue.Experimental),h}function colorMixPolar(e,a,n){if(!n)return!1;const r=n.a.color,o=n.b.color,t=n.a.percentage/100;let l=r.channels,s=o.channels,u=0,c=0,i=0,h=0,m=0,p=0,N=se.RGB,b=r.alpha;if("number"!=typeof b)return!1;let v=o.alpha;if("number"!=typeof v)return!1;switch(b=Number.isNaN(b)?v:b,v=Number.isNaN(v)?b:v,e){case"hsl":N=se.HSL;break;case"hwb":N=se.HWB;break;case"lch":N=se.LCH;break;case"oklch":N=se.OKLCH}switch(l=colorDataTo(r,N).channels,s=colorDataTo(o,N).channels,e){case"hsl":case"hwb":u=l[0],c=s[0],i=l[1],h=s[1],m=l[2],p=s[2];break;case"lch":case"oklch":i=l[0],h=s[0],m=l[1],p=s[1],u=l[2],c=s[2]}u=fillInMissingComponent(u,c),Number.isNaN(u)&&(u=0),c=fillInMissingComponent(c,u),Number.isNaN(c)&&(c=0),i=fillInMissingComponent(i,h),h=fillInMissingComponent(h,i),m=fillInMissingComponent(m,p),p=fillInMissingComponent(p,m);const g=c-u;switch(a){case"shorter":g>180?u+=360:g<-180&&(c+=360);break;case"longer":-1800?u+=360:c+=360);break;case"increasing":g<0&&(c+=360);break;case"decreasing":g>0&&(u+=360);break;default:throw new Error("Unknown hue interpolation method")}i=premultiply(i,b),m=premultiply(m,b),h=premultiply(h,v),p=premultiply(p,v);let f=[0,0,0];const d=interpolate(b,v,t);switch(e){case"hsl":case"hwb":f=[interpolate(u,c,t),un_premultiply(interpolate(i,h,t),d),un_premultiply(interpolate(m,p,t),d)];break;case"lch":case"oklch":f=[un_premultiply(interpolate(i,h,t),d),un_premultiply(interpolate(m,p,t),d),interpolate(u,c,t)]}const y={colorNotation:N,channels:f,alpha:d*n.alphaMultiplier,syntaxFlags:new Set([ue.ColorMix])};return(n.a.color.syntaxFlags.has(ue.Experimental)||n.b.color.syntaxFlags.has(ue.Experimental))&&y.syntaxFlags.add(ue.Experimental),y}function fillInMissingComponent(e,a){return Number.isNaN(e)?a:e}function interpolate(e,a,n){return e*n+a*(1-n)}function premultiply(e,a){return Number.isNaN(a)?e:Number.isNaN(e)?NaN:e*a}function un_premultiply(e,a){return 0===a||Number.isNaN(a)?e:Number.isNaN(e)?NaN:e/a}function hex(e){const a=toLowerCaseAZ(e[4].value);if(a.match(/[^a-f0-9]/))return!1;const n={colorNotation:se.HEX,channels:[0,0,0],alpha:1,syntaxFlags:new Set([ue.Hex])},r=a.length;if(3===r){const e=a[0],r=a[1],o=a[2];return n.channels=[parseInt(e+e,16)/255,parseInt(r+r,16)/255,parseInt(o+o,16)/255],n}if(6===r){const e=a[0]+a[1],r=a[2]+a[3],o=a[4]+a[5];return n.channels=[parseInt(e,16)/255,parseInt(r,16)/255,parseInt(o,16)/255],n}if(4===r){const e=a[0],r=a[1],o=a[2],t=a[3];return n.channels=[parseInt(e+e,16)/255,parseInt(r+r,16)/255,parseInt(o+o,16)/255],n.alpha=parseInt(t+t,16)/255,n.syntaxFlags.add(ue.HasAlpha),n}if(8===r){const e=a[0]+a[1],r=a[2]+a[3],o=a[4]+a[5],t=a[6]+a[7];return n.channels=[parseInt(e,16)/255,parseInt(r,16)/255,parseInt(o,16)/255],n.alpha=parseInt(t,16)/255,n.syntaxFlags.add(ue.HasAlpha),n}return!1}function normalizeHue(n){if(o(n))return n[4].value=n[4].value%360,n[1]=n[4].value.toString(),n;if(u(n)){let r=n[4].value;switch(toLowerCaseAZ(n[4].unit)){case"deg":break;case"rad":r=180*n[4].value/Math.PI;break;case"grad":r=.9*n[4].value;break;case"turn":r=360*n[4].value;break;default:return!1}return r%=360,[e.Number,r.toString(),n[2],n[3],{value:r,type:a.Number}]}return!1}function normalize_legacy_HSL_ChannelValues(n,t,l){if(0===t){const e=normalizeHue(n);return!1!==e&&(u(n)&&l.syntaxFlags.add(ue.HasDimensionValues),e)}if(r(n)){3===t?l.syntaxFlags.add(ue.HasPercentageAlpha):l.syntaxFlags.add(ue.HasPercentageValues);let r=normalize(n[4].value,1,0,100);return 3===t&&(r=normalize(n[4].value,100,0,1)),[e.Number,r.toString(),n[2],n[3],{value:r,type:a.Number}]}if(o(n)){if(3!==t)return!1;let r=normalize(n[4].value,1,0,100);return 3===t&&(r=normalize(n[4].value,1,0,1)),[e.Number,r.toString(),n[2],n[3],{value:r,type:a.Number}]}return!1}function normalize_modern_HSL_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(0===l){const e=normalizeHue(t);return!1!==e&&(u(t)&&s.syntaxFlags.add(ue.HasDimensionValues),e)}if(r(t)){3===l?s.syntaxFlags.add(ue.HasPercentageAlpha):s.syntaxFlags.add(ue.HasPercentageValues);let n=t[4].value;return 3===l?n=normalize(t[4].value,100,0,1):1===l&&(n=normalize(t[4].value,1,0,2147483647)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=t[4].value;return 3===l?n=normalize(t[4].value,1,0,1):1===l&&(n=normalize(t[4].value,1,0,2147483647)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function threeChannelLegacySyntax(e,a,n,r){const t=[],u=[],c=[],i=[],h={colorNotation:n,channels:[0,0,0],alpha:1,syntaxFlags:new Set(r)};let m=t;for(let a=0;aee(e)&&s(e.value)))){const a=hslCommaSeparated(e);if(!1!==a)return a}{const n=hslSpaceSeparated(e,a);if(!1!==n)return n}return!1}function hslCommaSeparated(e){return threeChannelLegacySyntax(e,normalize_legacy_HSL_ChannelValues,se.HSL,[ue.LegacyHSL])}function hslSpaceSeparated(e,a){return threeChannelSpaceSeparated(e,normalize_modern_HSL_ChannelValues,se.HSL,[],a)}function normalize_HWB_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(0===l){const e=normalizeHue(t);return!1!==e&&(u(t)&&s.syntaxFlags.add(ue.HasDimensionValues),e)}if(r(t)){3===l?s.syntaxFlags.add(ue.HasPercentageAlpha):s.syntaxFlags.add(ue.HasPercentageValues);let n=t[4].value;return 3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=t[4].value;return 3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function normalize_Lab_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(r(t)){3!==l&&s.syntaxFlags.add(ue.HasPercentageValues);let n=normalize(t[4].value,1,0,100);return 1===l||2===l?n=normalize(t[4].value,.8,-2147483647,2147483647):3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=normalize(t[4].value,1,0,100);return 1===l||2===l?n=normalize(t[4].value,1,-2147483647,2147483647):3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function lab(e,a){return threeChannelSpaceSeparated(e,normalize_Lab_ChannelValues,se.Lab,[],a)}function normalize_LCH_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(2===l){const e=normalizeHue(t);return!1!==e&&(u(t)&&s.syntaxFlags.add(ue.HasDimensionValues),e)}if(r(t)){3!==l&&s.syntaxFlags.add(ue.HasPercentageValues);let n=normalize(t[4].value,1,0,100);return 1===l?n=normalize(t[4].value,100/150,0,2147483647):3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=normalize(t[4].value,1,0,100);return 1===l?n=normalize(t[4].value,1,0,2147483647):3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function lch(e,a){return threeChannelSpaceSeparated(e,normalize_LCH_ChannelValues,se.LCH,[],a)}const be=new Map;for(const[e,a]of Object.entries(G))be.set(e,a);function namedColor(e){const a=be.get(toLowerCaseAZ(e));return!!a&&{colorNotation:se.RGB,channels:[a[0]/255,a[1]/255,a[2]/255],alpha:1,syntaxFlags:new Set([ue.ColorKeyword,ue.NamedColor])}}function normalize_OKLab_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(r(t)){3!==l&&s.syntaxFlags.add(ue.HasPercentageValues);let n=normalize(t[4].value,100,0,1);return 1===l||2===l?n=normalize(t[4].value,250,-2147483647,2147483647):3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=normalize(t[4].value,1,0,1);return 1===l||2===l?n=normalize(t[4].value,1,-2147483647,2147483647):3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function oklab(e,a){return threeChannelSpaceSeparated(e,normalize_OKLab_ChannelValues,se.OKLab,[],a)}function normalize_OKLCH_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(2===l){const e=normalizeHue(t);return!1!==e&&(u(t)&&s.syntaxFlags.add(ue.HasDimensionValues),e)}if(r(t)){3!==l&&s.syntaxFlags.add(ue.HasPercentageValues);let n=normalize(t[4].value,100,0,1);return 1===l?n=normalize(t[4].value,250,0,2147483647):3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=normalize(t[4].value,1,0,1);return 1===l?n=normalize(t[4].value,1,0,2147483647):3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function oklch(e,a){return threeChannelSpaceSeparated(e,normalize_OKLCH_ChannelValues,se.OKLCH,[],a)}function normalize_legacy_sRGB_ChannelValues(n,t,l){if(r(n)){3===t?l.syntaxFlags.add(ue.HasPercentageAlpha):l.syntaxFlags.add(ue.HasPercentageValues);const r=normalize(n[4].value,100,0,1);return[e.Number,r.toString(),n[2],n[3],{value:r,type:a.Number}]}if(o(n)){3!==t&&l.syntaxFlags.add(ue.HasNumberValues);let r=normalize(n[4].value,255,0,1);return 3===t&&(r=normalize(n[4].value,1,0,1)),[e.Number,r.toString(),n[2],n[3],{value:r,type:a.Number}]}return!1}function normalize_modern_sRGB_ChannelValues(t,l,s){if(n(t)&&"none"===t[4].value.toLowerCase())return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(r(t)){3!==l&&s.syntaxFlags.add(ue.HasPercentageValues);let n=normalize(t[4].value,100,-2147483647,2147483647);return 3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=normalize(t[4].value,255,-2147483647,2147483647);return 3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function rgb(e,a){if(e.value.some((e=>ee(e)&&s(e.value)))){const a=rgbCommaSeparated(e);if(!1!==a)return(!a.syntaxFlags.has(ue.HasNumberValues)||!a.syntaxFlags.has(ue.HasPercentageValues))&&a}else{const n=rgbSpaceSeparated(e,a);if(!1!==n)return n}return!1}function rgbCommaSeparated(e){return threeChannelLegacySyntax(e,normalize_legacy_sRGB_ChannelValues,se.RGB,[ue.LegacyRGB])}function rgbSpaceSeparated(e,a){return threeChannelSpaceSeparated(e,normalize_modern_sRGB_ChannelValues,se.RGB,[],a)}function XYZ_D50_to_sRGB_Gamut(e){const a=B(e);if(T(a))return V(a);let n=e;return n=L(n),n[0]<1e-6&&(n=[0,0,0]),n[0]>.999999&&(n=[1,0,0]),A(X(n,oklch_to_lin_srgb,lin_srgb_to_oklch))}function oklch_to_lin_srgb(e){return e=Y(e),e=K(e),I(e)}function lin_srgb_to_oklch(e){return e=O(e),e=E(e),W(e)}function contrastColor(e,a){let r=!1,o=!1;for(let t=0;ts?[1,1,1]:[0,0,0],t}function XYZ_D50_to_P3_Gamut(e){const a=M(e);if(T(a))return V(a);let n=e;return n=L(n),n[0]<1e-6&&(n=[0,0,0]),n[0]>.999999&&(n=[1,0,0]),$(X(n,oklch_to_lin_p3,lin_p3_to_oklch))}function oklch_to_lin_p3(e){return e=Y(e),e=K(e),j(e)}function lin_p3_to_oklch(e){return e=q(e),e=E(e),W(e)}function toPrecision(e,a=7){e=+e,a=+a;const n=(Math.floor(e)+"").length;if(a>n)return+e.toFixed(a-n);{const r=10**(n-a);return Math.round(e/r)*r}}function serializeWithAlpha(n,r,o,t){const l=[e.CloseParen,")",-1,-1,void 0];if("number"==typeof n.alpha){const s=Math.min(1,Math.max(0,toPrecision(Number.isNaN(n.alpha)?0:n.alpha)));return 1===toPrecision(s,4)?new re(r,l,t):new re(r,l,[...t,new oe([o]),new ne([e.Delim,"/",-1,-1,{value:"/"}]),new oe([o]),new ne([e.Number,toPrecision(s,4).toString(),-1,-1,{value:n.alpha,type:a.Integer}])])}return new re(r,l,[...t,new oe([o]),new ne([e.Delim,"/",-1,-1,{value:"/"}]),new oe([o]),n.alpha])}function serializeP3(n,r=!0){n.channels=convertPowerlessComponentsToZeroValuesForDisplay(n.channels,n.colorNotation);let o=n.channels.map((e=>Number.isNaN(e)?0:e));r?o=XYZ_D50_to_P3_Gamut(colorData_to_XYZ_D50(n).channels):n.colorNotation!==se.Display_P3&&(o=M(colorData_to_XYZ_D50(n).channels));const t=r?Math.min(1,Math.max(0,toPrecision(o[0],6))):toPrecision(o[0],6),l=r?Math.min(1,Math.max(0,toPrecision(o[1],6))):toPrecision(o[1],6),s=r?Math.min(1,Math.max(0,toPrecision(o[2],6))):toPrecision(o[2],6),u=[e.Function,"color(",-1,-1,{value:"color"}],c=[e.Whitespace," ",-1,-1,void 0];return serializeWithAlpha(n,u,c,[new ne([e.Ident,"display-p3",-1,-1,{value:"display-p3"}]),new oe([c]),new ne([e.Number,t.toString(),-1,-1,{value:o[0],type:a.Number}]),new oe([c]),new ne([e.Number,l.toString(),-1,-1,{value:o[1],type:a.Number}]),new oe([c]),new ne([e.Number,s.toString(),-1,-1,{value:o[2],type:a.Number}])])}function serializeRGB(n,r=!0){n.channels=convertPowerlessComponentsToZeroValuesForDisplay(n.channels,n.colorNotation);let o=n.channels.map((e=>Number.isNaN(e)?0:e));o=r?XYZ_D50_to_sRGB_Gamut(colorData_to_XYZ_D50(n).channels):B(colorData_to_XYZ_D50(n).channels);const t=Math.min(255,Math.max(0,Math.round(255*toPrecision(o[0])))),l=Math.min(255,Math.max(0,Math.round(255*toPrecision(o[1])))),s=Math.min(255,Math.max(0,Math.round(255*toPrecision(o[2])))),u=[e.CloseParen,")",-1,-1,void 0],c=[e.Whitespace," ",-1,-1,void 0],i=[e.Comma,",",-1,-1,void 0],h=[new ne([e.Number,t.toString(),-1,-1,{value:Math.min(255,255*Math.max(0,o[0])),type:a.Integer}]),new ne(i),new oe([c]),new ne([e.Number,l.toString(),-1,-1,{value:Math.min(255,255*Math.max(0,o[1])),type:a.Integer}]),new ne(i),new oe([c]),new ne([e.Number,s.toString(),-1,-1,{value:Math.min(255,255*Math.max(0,o[2])),type:a.Integer}])];if("number"==typeof n.alpha){const r=Math.min(1,Math.max(0,toPrecision(Number.isNaN(n.alpha)?0:n.alpha)));return 1===toPrecision(r,4)?new re([e.Function,"rgb(",-1,-1,{value:"rgb"}],u,h):new re([e.Function,"rgba(",-1,-1,{value:"rgba"}],u,[...h,new ne(i),new oe([c]),new ne([e.Number,toPrecision(r,4).toString(),-1,-1,{value:n.alpha,type:a.Number}])])}return new re([e.Function,"rgba(",-1,-1,{value:"rgba"}],u,[...h,new ne(i),new oe([c]),n.alpha])}function serializeHSL(n,r=!0){n.channels=convertPowerlessComponentsToZeroValuesForDisplay(n.channels,n.colorNotation);let o=n.channels.map((e=>Number.isNaN(e)?0:e));o=z(r?w(XYZ_D50_to_sRGB_Gamut(colorData_to_XYZ_D50(n).channels)):colorData_to_XYZ_D50(n).channels),o=o.map((e=>Number.isNaN(e)?0:e));const t=Math.min(360,Math.max(0,Math.round(toPrecision(o[0])))),l=Math.min(100,Math.max(0,Math.round(toPrecision(o[1])))),s=Math.min(100,Math.max(0,Math.round(toPrecision(o[2])))),u=[e.CloseParen,")",-1,-1,void 0],c=[e.Whitespace," ",-1,-1,void 0],i=[e.Comma,",",-1,-1,void 0],h=[new ne([e.Number,t.toString(),-1,-1,{value:o[0],type:a.Integer}]),new ne(i),new oe([c]),new ne([e.Percentage,l.toString()+"%",-1,-1,{value:o[1]}]),new ne(i),new oe([c]),new ne([e.Percentage,s.toString()+"%",-1,-1,{value:o[2]}])];if("number"==typeof n.alpha){const r=Math.min(1,Math.max(0,toPrecision(Number.isNaN(n.alpha)?0:n.alpha)));return 1===toPrecision(r,4)?new re([e.Function,"hsl(",-1,-1,{value:"hsl"}],u,h):new re([e.Function,"hsla(",-1,-1,{value:"hsla"}],u,[...h,new ne(i),new oe([c]),new ne([e.Number,toPrecision(r,4).toString(),-1,-1,{value:n.alpha,type:a.Number}])])}return new re([e.Function,"hsla(",-1,-1,{value:"hsla"}],u,[...h,new ne(i),new oe([c]),n.alpha])}function serializeOKLCH(n){n.channels=convertPowerlessComponentsToZeroValuesForDisplay(n.channels,n.colorNotation);let r=n.channels.map((e=>Number.isNaN(e)?0:e));n.colorNotation!==se.OKLCH&&(r=L(colorData_to_XYZ_D50(n).channels));const o=toPrecision(r[0],6),t=toPrecision(r[1],6),l=toPrecision(r[2],6),s=[e.Function,"oklch(",-1,-1,{value:"oklch"}],u=[e.Whitespace," ",-1,-1,void 0];return serializeWithAlpha(n,s,u,[new ne([e.Number,o.toString(),-1,-1,{value:r[0],type:a.Number}]),new oe([u]),new ne([e.Number,t.toString(),-1,-1,{value:r[1],type:a.Number}]),new oe([u]),new ne([e.Number,l.toString(),-1,-1,{value:r[2],type:a.Number}])])}function color(e){if(ae(e)){switch(toLowerCaseAZ(e.getName())){case"rgb":case"rgba":return rgb(e,color);case"hsl":case"hsla":return hsl(e,color);case"hwb":return a=color,threeChannelSpaceSeparated(e,normalize_HWB_ChannelValues,se.HWB,[],a);case"lab":return lab(e,color);case"lch":return lch(e,color);case"oklab":return oklab(e,color);case"oklch":return oklch(e,color);case"color":return color$1(e,color);case"color-mix":return colorMix(e,color);case"contrast-color":return contrastColor(e,color)}}var a;if(ee(e)){if(c(e.value))return hex(e.value);if(n(e.value)){const a=namedColor(e.value[4].value);return!1!==a?a:"transparent"===toLowerCaseAZ(e.value[4].value)&&{colorNotation:se.RGB,channels:[0,0,0],alpha:0,syntaxFlags:new Set([ue.ColorKeyword])}}}return!1}export{se as ColorNotation,ue as SyntaxFlag,color,colorDataFitsDisplayP3_Gamut,colorDataFitsRGB_Gamut,serializeHSL,serializeOKLCH,serializeP3,serializeRGB}; +import{TokenType as e,NumberType as a,isTokenIdent as n,isTokenPercentage as r,isTokenNumber as o,isTokenDelim as t,isTokenNumeric as l,isTokenComma as s,isTokenDimension as u,isTokenHash as c}from"@csstools/css-tokenizer";import{XYZ_D65_to_XYZ_D50 as i,XYZ_D50_to_XYZ_D50 as h,OKLCH_to_XYZ_D50 as m,LCH_to_XYZ_D50 as p,OKLab_to_XYZ_D50 as N,Lab_to_XYZ_D50 as b,HWB_to_XYZ_D50 as v,HSL_to_XYZ_D50 as g,ProPhoto_RGB_to_XYZ_D50 as f,a98_RGB_to_XYZ_D50 as d,rec_2020_to_XYZ_D50 as y,P3_to_XYZ_D50 as _,lin_sRGB_to_XYZ_D50 as C,sRGB_to_XYZ_D50 as w,XYZ_D50_to_XYZ_D65 as x,XYZ_D50_to_OKLab as H,XYZ_D50_to_OKLCH as L,XYZ_D50_to_LCH as k,XYZ_D50_to_Lab as P,XYZ_D50_to_HWB as S,XYZ_D50_to_HSL as z,XYZ_D50_to_a98_RGB as D,XYZ_D50_to_ProPhoto as F,XYZ_D50_to_rec_2020 as Z,XYZ_D50_to_P3 as M,XYZ_D50_to_lin_sRGB as R,XYZ_D50_to_sRGB as B,namedColors as G,inGamut as T,clip as V,gam_sRGB as A,mapGamutRayTrace as X,OKLCH_to_OKLab as Y,OKLab_to_XYZ as K,XYZ_to_lin_sRGB as I,lin_sRGB_to_XYZ as O,XYZ_to_OKLab as E,OKLab_to_OKLCH as W,contrast_ratio_wcag_2_1 as U,gam_P3 as $,XYZ_to_lin_P3 as j,lin_P3_to_XYZ as q}from"@csstools/color-helpers";import{isWhitespaceNode as J,isCommentNode as Q,isTokenNode as ee,isFunctionNode as ae,TokenNode as ne,FunctionNode as re,WhitespaceNode as oe}from"@csstools/css-parser-algorithms";import{mathFunctionNames as te,calcFromComponentValues as le}from"@csstools/css-calc";var se,ue;function convertNaNToZero(e){return[Number.isNaN(e[0])?0:e[0],Number.isNaN(e[1])?0:e[1],Number.isNaN(e[2])?0:e[2]]}function colorData_to_XYZ_D50(e){switch(e.colorNotation){case se.HEX:case se.RGB:case se.sRGB:return{...e,colorNotation:se.XYZ_D50,channels:w(convertNaNToZero(e.channels))};case se.Linear_sRGB:return{...e,colorNotation:se.XYZ_D50,channels:C(convertNaNToZero(e.channels))};case se.Display_P3:return{...e,colorNotation:se.XYZ_D50,channels:_(convertNaNToZero(e.channels))};case se.Rec2020:return{...e,colorNotation:se.XYZ_D50,channels:y(convertNaNToZero(e.channels))};case se.A98_RGB:return{...e,colorNotation:se.XYZ_D50,channels:d(convertNaNToZero(e.channels))};case se.ProPhoto_RGB:return{...e,colorNotation:se.XYZ_D50,channels:f(convertNaNToZero(e.channels))};case se.HSL:return{...e,colorNotation:se.XYZ_D50,channels:g(convertNaNToZero(e.channels))};case se.HWB:return{...e,colorNotation:se.XYZ_D50,channels:v(convertNaNToZero(e.channels))};case se.Lab:return{...e,colorNotation:se.XYZ_D50,channels:b(convertNaNToZero(e.channels))};case se.OKLab:return{...e,colorNotation:se.XYZ_D50,channels:N(convertNaNToZero(e.channels))};case se.LCH:return{...e,colorNotation:se.XYZ_D50,channels:p(convertNaNToZero(e.channels))};case se.OKLCH:return{...e,colorNotation:se.XYZ_D50,channels:m(convertNaNToZero(e.channels))};case se.XYZ_D50:return{...e,colorNotation:se.XYZ_D50,channels:h(convertNaNToZero(e.channels))};case se.XYZ_D65:return{...e,colorNotation:se.XYZ_D50,channels:i(convertNaNToZero(e.channels))};default:throw new Error("Unsupported color notation")}}!function(e){e.A98_RGB="a98-rgb",e.Display_P3="display-p3",e.HEX="hex",e.HSL="hsl",e.HWB="hwb",e.LCH="lch",e.Lab="lab",e.Linear_sRGB="srgb-linear",e.OKLCH="oklch",e.OKLab="oklab",e.ProPhoto_RGB="prophoto-rgb",e.RGB="rgb",e.sRGB="srgb",e.Rec2020="rec2020",e.XYZ_D50="xyz-d50",e.XYZ_D65="xyz-d65"}(se||(se={})),function(e){e.ColorKeyword="color-keyword",e.HasAlpha="has-alpha",e.HasDimensionValues="has-dimension-values",e.HasNoneKeywords="has-none-keywords",e.HasNumberValues="has-number-values",e.HasPercentageAlpha="has-percentage-alpha",e.HasPercentageValues="has-percentage-values",e.HasVariableAlpha="has-variable-alpha",e.Hex="hex",e.LegacyHSL="legacy-hsl",e.LegacyRGB="legacy-rgb",e.NamedColor="named-color",e.RelativeColorSyntax="relative-color-syntax",e.ColorMix="color-mix",e.ContrastColor="contrast-color",e.Experimental="experimental"}(ue||(ue={}));const ce=new Set([se.A98_RGB,se.Display_P3,se.HEX,se.Linear_sRGB,se.ProPhoto_RGB,se.RGB,se.sRGB,se.Rec2020,se.XYZ_D50,se.XYZ_D65]);function colorDataTo(e,a){const n={...e};if(e.colorNotation!==a){const e=colorData_to_XYZ_D50(n);switch(a){case se.HEX:case se.RGB:n.colorNotation=se.RGB,n.channels=B(e.channels);break;case se.sRGB:n.colorNotation=se.sRGB,n.channels=B(e.channels);break;case se.Linear_sRGB:n.colorNotation=se.Linear_sRGB,n.channels=R(e.channels);break;case se.Display_P3:n.colorNotation=se.Display_P3,n.channels=M(e.channels);break;case se.Rec2020:n.colorNotation=se.Rec2020,n.channels=Z(e.channels);break;case se.ProPhoto_RGB:n.colorNotation=se.ProPhoto_RGB,n.channels=F(e.channels);break;case se.A98_RGB:n.colorNotation=se.A98_RGB,n.channels=D(e.channels);break;case se.HSL:n.colorNotation=se.HSL,n.channels=z(e.channels);break;case se.HWB:n.colorNotation=se.HWB,n.channels=S(e.channels);break;case se.Lab:n.colorNotation=se.Lab,n.channels=P(e.channels);break;case se.LCH:n.colorNotation=se.LCH,n.channels=k(e.channels);break;case se.OKLCH:n.colorNotation=se.OKLCH,n.channels=L(e.channels);break;case se.OKLab:n.colorNotation=se.OKLab,n.channels=H(e.channels);break;case se.XYZ_D50:n.colorNotation=se.XYZ_D50,n.channels=h(e.channels);break;case se.XYZ_D65:n.colorNotation=se.XYZ_D65,n.channels=x(e.channels);break;default:throw new Error("Unsupported color notation")}}else n.channels=convertNaNToZero(e.channels);if(a===e.colorNotation)n.channels=carryForwardMissingComponents(e.channels,[0,1,2],n.channels,[0,1,2]);else if(ce.has(a)&&ce.has(e.colorNotation))n.channels=carryForwardMissingComponents(e.channels,[0,1,2],n.channels,[0,1,2]);else switch(a){case se.HSL:switch(e.colorNotation){case se.HWB:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[0]);break;case se.Lab:case se.OKLab:n.channels=carryForwardMissingComponents(e.channels,[2],n.channels,[0]);break;case se.LCH:case se.OKLCH:n.channels=carryForwardMissingComponents(e.channels,[0,1,2],n.channels,[2,1,0])}break;case se.HWB:switch(e.colorNotation){case se.HSL:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[0]);break;case se.LCH:case se.OKLCH:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[2])}break;case se.Lab:case se.OKLab:switch(e.colorNotation){case se.HSL:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[2]);break;case se.Lab:case se.OKLab:n.channels=carryForwardMissingComponents(e.channels,[0,1,2],n.channels,[0,1,2]);break;case se.LCH:case se.OKLCH:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[0])}break;case se.LCH:case se.OKLCH:switch(e.colorNotation){case se.HSL:n.channels=carryForwardMissingComponents(e.channels,[0,1,2],n.channels,[2,1,0]);break;case se.HWB:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[2]);break;case se.Lab:case se.OKLab:n.channels=carryForwardMissingComponents(e.channels,[0],n.channels,[0]);break;case se.LCH:case se.OKLCH:n.channels=carryForwardMissingComponents(e.channels,[0,1,2],n.channels,[0,1,2])}}return n.channels=convertPowerlessComponentsToMissingComponents(n.channels,a),n}function convertPowerlessComponentsToMissingComponents(e,a){const n=[...e];switch(a){case se.HSL:!Number.isNaN(n[1])&&reducePrecision(n[1],4)<=0&&(n[0]=NaN);break;case se.HWB:Math.max(0,reducePrecision(n[1],4))+Math.max(0,reducePrecision(n[2],4))>=100&&(n[0]=NaN);break;case se.LCH:!Number.isNaN(n[1])&&reducePrecision(n[1],4)<=0&&(n[2]=NaN);break;case se.OKLCH:!Number.isNaN(n[1])&&reducePrecision(n[1],6)<=0&&(n[2]=NaN)}return n}function convertPowerlessComponentsToZeroValuesForDisplay(e,a){const n=[...e];switch(a){case se.HSL:(reducePrecision(n[2])<=0||reducePrecision(n[2])>=100)&&(n[0]=NaN,n[1]=NaN),reducePrecision(n[1])<=0&&(n[0]=NaN);break;case se.HWB:Math.max(0,reducePrecision(n[1]))+Math.max(0,reducePrecision(n[2]))>=100&&(n[0]=NaN);break;case se.Lab:(reducePrecision(n[0])<=0||reducePrecision(n[0])>=100)&&(n[1]=NaN,n[2]=NaN);break;case se.LCH:reducePrecision(n[1])<=0&&(n[2]=NaN),(reducePrecision(n[0])<=0||reducePrecision(n[0])>=100)&&(n[1]=NaN,n[2]=NaN);break;case se.OKLab:(reducePrecision(n[0])<=0||reducePrecision(n[0])>=1)&&(n[1]=NaN,n[2]=NaN);break;case se.OKLCH:reducePrecision(n[1])<=0&&(n[2]=NaN),(reducePrecision(n[0])<=0||reducePrecision(n[0])>=1)&&(n[1]=NaN,n[2]=NaN)}return n}function carryForwardMissingComponents(e,a,n,r){const o=[...n];for(const n of a)Number.isNaN(e[a[n]])&&(o[r[n]]=NaN);return o}function normalizeRelativeColorDataChannels(e){const a=new Map;switch(e.colorNotation){case se.RGB:case se.HEX:a.set("r",dummyNumberToken(255*e.channels[0])),a.set("g",dummyNumberToken(255*e.channels[1])),a.set("b",dummyNumberToken(255*e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha));break;case se.HSL:a.set("h",dummyNumberToken(e.channels[0])),a.set("s",dummyNumberToken(e.channels[1])),a.set("l",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha));break;case se.HWB:a.set("h",dummyNumberToken(e.channels[0])),a.set("w",dummyNumberToken(e.channels[1])),a.set("b",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha));break;case se.Lab:case se.OKLab:a.set("l",dummyNumberToken(e.channels[0])),a.set("a",dummyNumberToken(e.channels[1])),a.set("b",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha));break;case se.LCH:case se.OKLCH:a.set("l",dummyNumberToken(e.channels[0])),a.set("c",dummyNumberToken(e.channels[1])),a.set("h",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha));break;case se.sRGB:case se.A98_RGB:case se.Display_P3:case se.Rec2020:case se.Linear_sRGB:case se.ProPhoto_RGB:a.set("r",dummyNumberToken(e.channels[0])),a.set("g",dummyNumberToken(e.channels[1])),a.set("b",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha));break;case se.XYZ_D50:case se.XYZ_D65:a.set("x",dummyNumberToken(e.channels[0])),a.set("y",dummyNumberToken(e.channels[1])),a.set("z",dummyNumberToken(e.channels[2])),"number"==typeof e.alpha&&a.set("alpha",dummyNumberToken(e.alpha))}return a}function noneToZeroInRelativeColorDataChannels(e){const a=new Map(e);for(const[n,r]of e)Number.isNaN(r[4].value)&&a.set(n,dummyNumberToken(0));return a}function dummyNumberToken(n){return Number.isNaN(n)?[e.Number,"none",-1,-1,{value:Number.NaN,type:a.Number}]:[e.Number,n.toString(),-1,-1,{value:n,type:a.Number}]}function reducePrecision(e,a=7){if(Number.isNaN(e))return 0;const n=Math.pow(10,a);return Math.round(e*n)/n}function colorDataFitsRGB_Gamut(e){const a={...e,channels:[...e.channels]};a.channels=convertPowerlessComponentsToZeroValuesForDisplay(a.channels,a.colorNotation);return!colorDataTo(a,se.RGB).channels.find((e=>e<-1e-5||e>1.00001))}function colorDataFitsDisplayP3_Gamut(e){const a={...e,channels:[...e.channels]};a.channels=convertPowerlessComponentsToZeroValuesForDisplay(a.channels,a.colorNotation);return!colorDataTo(a,se.Display_P3).channels.find((e=>e<-1e-5||e>1.00001))}function normalize(e,a,n,r){return Math.min(Math.max(e/a,n),r)}const ie=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(ie,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}function normalize_Color_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(r(t)){3!==l&&s.syntaxFlags.add(ue.HasPercentageValues);let n=normalize(t[4].value,100,-2147483647,2147483647);return 3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=normalize(t[4].value,1,-2147483647,2147483647);return 3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}const he=new Set(["srgb","srgb-linear","display-p3","a98-rgb","prophoto-rgb","rec2020","xyz","xyz-d50","xyz-d65"]);function color$1(e,a){const r=[],s=[],u=[],c=[];let i,h,m=!1,p=!1;const N={colorNotation:se.sRGB,channels:[0,0,0],alpha:1,syntaxFlags:new Set([])};let b=r;for(let o=0;o=0){u=c.value[4].value;continue}}return!1}if(!t)return!1;n.push({color:t,percentage:u}),t=!1,u=!1}}if(t&&n.push({color:t,percentage:u}),2!==n.length)return!1;let c=n[0].percentage,i=n[1].percentage;return(!1===c||!(c<0||c>100))&&((!1===i||!(i<0||i>100))&&(!1===c&&!1===i?(c=50,i=50):!1!==c&&!1===i?i=100-c:!1===c&&!1!==i&&(c=100-i),(0!==c||0!==i)&&(!1!==c&&!1!==i&&(c+i>100&&(c=c/(c+i)*100,i=i/(c+i)*100),c+i<100&&(o=(c+i)/100,c=c/(c+i)*100,i=i/(c+i)*100),{a:{color:n[0].color,percentage:c},b:{color:n[1].color,percentage:i},alphaMultiplier:o}))))}function colorMixRectangular(e,a){if(!a)return!1;const n=a.a.color,r=a.b.color,o=a.a.percentage/100;let t=n.channels,l=r.channels,s=se.RGB,u=n.alpha;if("number"!=typeof u)return!1;let c=r.alpha;if("number"!=typeof c)return!1;switch(u=Number.isNaN(u)?c:u,c=Number.isNaN(c)?u:c,e){case"srgb":s=se.RGB;break;case"srgb-linear":s=se.Linear_sRGB;break;case"display-p3":s=se.Display_P3;break;case"a98-rgb":s=se.A98_RGB;break;case"prophoto-rgb":s=se.ProPhoto_RGB;break;case"rec2020":s=se.Rec2020;break;case"lab":s=se.Lab;break;case"oklab":s=se.OKLab;break;case"xyz-d50":s=se.XYZ_D50;break;case"xyz":case"xyz-d65":s=se.XYZ_D65}t=colorDataTo(n,s).channels,l=colorDataTo(r,s).channels,t[0]=fillInMissingComponent(t[0],l[0]),l[0]=fillInMissingComponent(l[0],t[0]),t[1]=fillInMissingComponent(t[1],l[1]),l[1]=fillInMissingComponent(l[1],t[1]),t[2]=fillInMissingComponent(t[2],l[2]),l[2]=fillInMissingComponent(l[2],t[2]),t[0]=premultiply(t[0],u),t[1]=premultiply(t[1],u),t[2]=premultiply(t[2],u),l[0]=premultiply(l[0],c),l[1]=premultiply(l[1],c),l[2]=premultiply(l[2],c);const i=interpolate(u,c,o),h={colorNotation:s,channels:[un_premultiply(interpolate(t[0],l[0],o),i),un_premultiply(interpolate(t[1],l[1],o),i),un_premultiply(interpolate(t[2],l[2],o),i)],alpha:i*a.alphaMultiplier,syntaxFlags:new Set([ue.ColorMix])};return(a.a.color.syntaxFlags.has(ue.Experimental)||a.b.color.syntaxFlags.has(ue.Experimental))&&h.syntaxFlags.add(ue.Experimental),h}function colorMixPolar(e,a,n){if(!n)return!1;const r=n.a.color,o=n.b.color,t=n.a.percentage/100;let l=r.channels,s=o.channels,u=0,c=0,i=0,h=0,m=0,p=0,N=se.RGB,b=r.alpha;if("number"!=typeof b)return!1;let v=o.alpha;if("number"!=typeof v)return!1;switch(b=Number.isNaN(b)?v:b,v=Number.isNaN(v)?b:v,e){case"hsl":N=se.HSL;break;case"hwb":N=se.HWB;break;case"lch":N=se.LCH;break;case"oklch":N=se.OKLCH}switch(l=colorDataTo(r,N).channels,s=colorDataTo(o,N).channels,e){case"hsl":case"hwb":u=l[0],c=s[0],i=l[1],h=s[1],m=l[2],p=s[2];break;case"lch":case"oklch":i=l[0],h=s[0],m=l[1],p=s[1],u=l[2],c=s[2]}u=fillInMissingComponent(u,c),Number.isNaN(u)&&(u=0),c=fillInMissingComponent(c,u),Number.isNaN(c)&&(c=0),i=fillInMissingComponent(i,h),h=fillInMissingComponent(h,i),m=fillInMissingComponent(m,p),p=fillInMissingComponent(p,m);const g=c-u;switch(a){case"shorter":g>180?u+=360:g<-180&&(c+=360);break;case"longer":-1800?u+=360:c+=360);break;case"increasing":g<0&&(c+=360);break;case"decreasing":g>0&&(u+=360);break;default:throw new Error("Unknown hue interpolation method")}i=premultiply(i,b),m=premultiply(m,b),h=premultiply(h,v),p=premultiply(p,v);let f=[0,0,0];const d=interpolate(b,v,t);switch(e){case"hsl":case"hwb":f=[interpolate(u,c,t),un_premultiply(interpolate(i,h,t),d),un_premultiply(interpolate(m,p,t),d)];break;case"lch":case"oklch":f=[un_premultiply(interpolate(i,h,t),d),un_premultiply(interpolate(m,p,t),d),interpolate(u,c,t)]}const y={colorNotation:N,channels:f,alpha:d*n.alphaMultiplier,syntaxFlags:new Set([ue.ColorMix])};return(n.a.color.syntaxFlags.has(ue.Experimental)||n.b.color.syntaxFlags.has(ue.Experimental))&&y.syntaxFlags.add(ue.Experimental),y}function fillInMissingComponent(e,a){return Number.isNaN(e)?a:e}function interpolate(e,a,n){return e*n+a*(1-n)}function premultiply(e,a){return Number.isNaN(a)?e:Number.isNaN(e)?NaN:e*a}function un_premultiply(e,a){return 0===a||Number.isNaN(a)?e:Number.isNaN(e)?NaN:e/a}function hex(e){const a=toLowerCaseAZ(e[4].value);if(a.match(/[^a-f0-9]/))return!1;const n={colorNotation:se.HEX,channels:[0,0,0],alpha:1,syntaxFlags:new Set([ue.Hex])},r=a.length;if(3===r){const e=a[0],r=a[1],o=a[2];return n.channels=[parseInt(e+e,16)/255,parseInt(r+r,16)/255,parseInt(o+o,16)/255],n}if(6===r){const e=a[0]+a[1],r=a[2]+a[3],o=a[4]+a[5];return n.channels=[parseInt(e,16)/255,parseInt(r,16)/255,parseInt(o,16)/255],n}if(4===r){const e=a[0],r=a[1],o=a[2],t=a[3];return n.channels=[parseInt(e+e,16)/255,parseInt(r+r,16)/255,parseInt(o+o,16)/255],n.alpha=parseInt(t+t,16)/255,n.syntaxFlags.add(ue.HasAlpha),n}if(8===r){const e=a[0]+a[1],r=a[2]+a[3],o=a[4]+a[5],t=a[6]+a[7];return n.channels=[parseInt(e,16)/255,parseInt(r,16)/255,parseInt(o,16)/255],n.alpha=parseInt(t,16)/255,n.syntaxFlags.add(ue.HasAlpha),n}return!1}function normalizeHue(n){if(o(n))return n[4].value=n[4].value%360,n[1]=n[4].value.toString(),n;if(u(n)){let r=n[4].value;switch(toLowerCaseAZ(n[4].unit)){case"deg":break;case"rad":r=180*n[4].value/Math.PI;break;case"grad":r=.9*n[4].value;break;case"turn":r=360*n[4].value;break;default:return!1}return r%=360,[e.Number,r.toString(),n[2],n[3],{value:r,type:a.Number}]}return!1}function normalize_legacy_HSL_ChannelValues(n,t,l){if(0===t){const e=normalizeHue(n);return!1!==e&&(u(n)&&l.syntaxFlags.add(ue.HasDimensionValues),e)}if(r(n)){3===t?l.syntaxFlags.add(ue.HasPercentageAlpha):l.syntaxFlags.add(ue.HasPercentageValues);let r=normalize(n[4].value,1,0,100);return 3===t&&(r=normalize(n[4].value,100,0,1)),[e.Number,r.toString(),n[2],n[3],{value:r,type:a.Number}]}if(o(n)){if(3!==t)return!1;let r=normalize(n[4].value,1,0,100);return 3===t&&(r=normalize(n[4].value,1,0,1)),[e.Number,r.toString(),n[2],n[3],{value:r,type:a.Number}]}return!1}function normalize_modern_HSL_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(0===l){const e=normalizeHue(t);return!1!==e&&(u(t)&&s.syntaxFlags.add(ue.HasDimensionValues),e)}if(r(t)){3===l?s.syntaxFlags.add(ue.HasPercentageAlpha):s.syntaxFlags.add(ue.HasPercentageValues);let n=t[4].value;return 3===l?n=normalize(t[4].value,100,0,1):1===l&&(n=normalize(t[4].value,1,0,2147483647)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=t[4].value;return 3===l?n=normalize(t[4].value,1,0,1):1===l&&(n=normalize(t[4].value,1,0,2147483647)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function threeChannelLegacySyntax(e,a,n,r){const t=[],u=[],c=[],i=[],h={colorNotation:n,channels:[0,0,0],alpha:1,syntaxFlags:new Set(r)};let m=t;for(let a=0;aee(e)&&s(e.value)))){const a=hslCommaSeparated(e);if(!1!==a)return a}{const n=hslSpaceSeparated(e,a);if(!1!==n)return n}return!1}function hslCommaSeparated(e){return threeChannelLegacySyntax(e,normalize_legacy_HSL_ChannelValues,se.HSL,[ue.LegacyHSL])}function hslSpaceSeparated(e,a){return threeChannelSpaceSeparated(e,normalize_modern_HSL_ChannelValues,se.HSL,[],a)}function normalize_HWB_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(0===l){const e=normalizeHue(t);return!1!==e&&(u(t)&&s.syntaxFlags.add(ue.HasDimensionValues),e)}if(r(t)){3===l?s.syntaxFlags.add(ue.HasPercentageAlpha):s.syntaxFlags.add(ue.HasPercentageValues);let n=t[4].value;return 3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=t[4].value;return 3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function normalize_Lab_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(r(t)){3!==l&&s.syntaxFlags.add(ue.HasPercentageValues);let n=normalize(t[4].value,1,0,100);return 1===l||2===l?n=normalize(t[4].value,.8,-2147483647,2147483647):3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=normalize(t[4].value,1,0,100);return 1===l||2===l?n=normalize(t[4].value,1,-2147483647,2147483647):3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function lab(e,a){return threeChannelSpaceSeparated(e,normalize_Lab_ChannelValues,se.Lab,[],a)}function normalize_LCH_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(2===l){const e=normalizeHue(t);return!1!==e&&(u(t)&&s.syntaxFlags.add(ue.HasDimensionValues),e)}if(r(t)){3!==l&&s.syntaxFlags.add(ue.HasPercentageValues);let n=normalize(t[4].value,1,0,100);return 1===l?n=normalize(t[4].value,100/150,0,2147483647):3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=normalize(t[4].value,1,0,100);return 1===l?n=normalize(t[4].value,1,0,2147483647):3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function lch(e,a){return threeChannelSpaceSeparated(e,normalize_LCH_ChannelValues,se.LCH,[],a)}const be=new Map;for(const[e,a]of Object.entries(G))be.set(e,a);function namedColor(e){const a=be.get(toLowerCaseAZ(e));return!!a&&{colorNotation:se.RGB,channels:[a[0]/255,a[1]/255,a[2]/255],alpha:1,syntaxFlags:new Set([ue.ColorKeyword,ue.NamedColor])}}function normalize_OKLab_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(r(t)){3!==l&&s.syntaxFlags.add(ue.HasPercentageValues);let n=normalize(t[4].value,100,0,1);return 1===l||2===l?n=normalize(t[4].value,250,-2147483647,2147483647):3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=normalize(t[4].value,1,0,1);return 1===l||2===l?n=normalize(t[4].value,1,-2147483647,2147483647):3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function oklab(e,a){return threeChannelSpaceSeparated(e,normalize_OKLab_ChannelValues,se.OKLab,[],a)}function normalize_OKLCH_ChannelValues(t,l,s){if(n(t)&&"none"===toLowerCaseAZ(t[4].value))return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(2===l){const e=normalizeHue(t);return!1!==e&&(u(t)&&s.syntaxFlags.add(ue.HasDimensionValues),e)}if(r(t)){3!==l&&s.syntaxFlags.add(ue.HasPercentageValues);let n=normalize(t[4].value,100,0,1);return 1===l?n=normalize(t[4].value,250,0,2147483647):3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=normalize(t[4].value,1,0,1);return 1===l?n=normalize(t[4].value,1,0,2147483647):3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function oklch(e,a){return threeChannelSpaceSeparated(e,normalize_OKLCH_ChannelValues,se.OKLCH,[],a)}function normalize_legacy_sRGB_ChannelValues(n,t,l){if(r(n)){3===t?l.syntaxFlags.add(ue.HasPercentageAlpha):l.syntaxFlags.add(ue.HasPercentageValues);const r=normalize(n[4].value,100,0,1);return[e.Number,r.toString(),n[2],n[3],{value:r,type:a.Number}]}if(o(n)){3!==t&&l.syntaxFlags.add(ue.HasNumberValues);let r=normalize(n[4].value,255,0,1);return 3===t&&(r=normalize(n[4].value,1,0,1)),[e.Number,r.toString(),n[2],n[3],{value:r,type:a.Number}]}return!1}function normalize_modern_sRGB_ChannelValues(t,l,s){if(n(t)&&"none"===t[4].value.toLowerCase())return s.syntaxFlags.add(ue.HasNoneKeywords),[e.Number,"none",t[2],t[3],{value:NaN,type:a.Number}];if(r(t)){3!==l&&s.syntaxFlags.add(ue.HasPercentageValues);let n=normalize(t[4].value,100,-2147483647,2147483647);return 3===l&&(n=normalize(t[4].value,100,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}if(o(t)){3!==l&&s.syntaxFlags.add(ue.HasNumberValues);let n=normalize(t[4].value,255,-2147483647,2147483647);return 3===l&&(n=normalize(t[4].value,1,0,1)),[e.Number,n.toString(),t[2],t[3],{value:n,type:a.Number}]}return!1}function rgb(e,a){if(e.value.some((e=>ee(e)&&s(e.value)))){const a=rgbCommaSeparated(e);if(!1!==a)return(!a.syntaxFlags.has(ue.HasNumberValues)||!a.syntaxFlags.has(ue.HasPercentageValues))&&a}else{const n=rgbSpaceSeparated(e,a);if(!1!==n)return n}return!1}function rgbCommaSeparated(e){return threeChannelLegacySyntax(e,normalize_legacy_sRGB_ChannelValues,se.RGB,[ue.LegacyRGB])}function rgbSpaceSeparated(e,a){return threeChannelSpaceSeparated(e,normalize_modern_sRGB_ChannelValues,se.RGB,[],a)}function XYZ_D50_to_sRGB_Gamut(e){const a=B(e);if(T(a))return V(a);let n=e;return n=L(n),n[0]<1e-6&&(n=[0,0,0]),n[0]>.999999&&(n=[1,0,0]),A(X(n,oklch_to_lin_srgb,lin_srgb_to_oklch))}function oklch_to_lin_srgb(e){return e=Y(e),e=K(e),I(e)}function lin_srgb_to_oklch(e){return e=O(e),e=E(e),W(e)}function contrastColor(e,a){let r=!1,o=!1;for(let t=0;ts?[1,1,1]:[0,0,0],t}function XYZ_D50_to_P3_Gamut(e){const a=M(e);if(T(a))return V(a);let n=e;return n=L(n),n[0]<1e-6&&(n=[0,0,0]),n[0]>.999999&&(n=[1,0,0]),$(X(n,oklch_to_lin_p3,lin_p3_to_oklch))}function oklch_to_lin_p3(e){return e=Y(e),e=K(e),j(e)}function lin_p3_to_oklch(e){return e=q(e),e=E(e),W(e)}function toPrecision(e,a=7){e=+e,a=+a;const n=(Math.floor(e)+"").length;if(a>n)return+e.toFixed(a-n);{const r=10**(n-a);return Math.round(e/r)*r}}function serializeWithAlpha(n,r,o,t){const l=[e.CloseParen,")",-1,-1,void 0];if("number"==typeof n.alpha){const s=Math.min(1,Math.max(0,toPrecision(Number.isNaN(n.alpha)?0:n.alpha)));return 1===toPrecision(s,4)?new re(r,l,t):new re(r,l,[...t,new oe([o]),new ne([e.Delim,"/",-1,-1,{value:"/"}]),new oe([o]),new ne([e.Number,toPrecision(s,4).toString(),-1,-1,{value:n.alpha,type:a.Integer}])])}return new re(r,l,[...t,new oe([o]),new ne([e.Delim,"/",-1,-1,{value:"/"}]),new oe([o]),n.alpha])}function serializeP3(n,r=!0){n.channels=convertPowerlessComponentsToZeroValuesForDisplay(n.channels,n.colorNotation);let o=n.channels.map((e=>Number.isNaN(e)?0:e));r?o=XYZ_D50_to_P3_Gamut(colorData_to_XYZ_D50(n).channels):n.colorNotation!==se.Display_P3&&(o=M(colorData_to_XYZ_D50(n).channels));const t=r?Math.min(1,Math.max(0,toPrecision(o[0],6))):toPrecision(o[0],6),l=r?Math.min(1,Math.max(0,toPrecision(o[1],6))):toPrecision(o[1],6),s=r?Math.min(1,Math.max(0,toPrecision(o[2],6))):toPrecision(o[2],6),u=[e.Function,"color(",-1,-1,{value:"color"}],c=[e.Whitespace," ",-1,-1,void 0];return serializeWithAlpha(n,u,c,[new ne([e.Ident,"display-p3",-1,-1,{value:"display-p3"}]),new oe([c]),new ne([e.Number,t.toString(),-1,-1,{value:o[0],type:a.Number}]),new oe([c]),new ne([e.Number,l.toString(),-1,-1,{value:o[1],type:a.Number}]),new oe([c]),new ne([e.Number,s.toString(),-1,-1,{value:o[2],type:a.Number}])])}function serializeRGB(n,r=!0){n.channels=convertPowerlessComponentsToZeroValuesForDisplay(n.channels,n.colorNotation);let o=n.channels.map((e=>Number.isNaN(e)?0:e));o=r?XYZ_D50_to_sRGB_Gamut(colorData_to_XYZ_D50(n).channels):B(colorData_to_XYZ_D50(n).channels);const t=Math.min(255,Math.max(0,Math.round(255*toPrecision(o[0])))),l=Math.min(255,Math.max(0,Math.round(255*toPrecision(o[1])))),s=Math.min(255,Math.max(0,Math.round(255*toPrecision(o[2])))),u=[e.CloseParen,")",-1,-1,void 0],c=[e.Whitespace," ",-1,-1,void 0],i=[e.Comma,",",-1,-1,void 0],h=[new ne([e.Number,t.toString(),-1,-1,{value:Math.min(255,255*Math.max(0,o[0])),type:a.Integer}]),new ne(i),new oe([c]),new ne([e.Number,l.toString(),-1,-1,{value:Math.min(255,255*Math.max(0,o[1])),type:a.Integer}]),new ne(i),new oe([c]),new ne([e.Number,s.toString(),-1,-1,{value:Math.min(255,255*Math.max(0,o[2])),type:a.Integer}])];if("number"==typeof n.alpha){const r=Math.min(1,Math.max(0,toPrecision(Number.isNaN(n.alpha)?0:n.alpha)));return 1===toPrecision(r,4)?new re([e.Function,"rgb(",-1,-1,{value:"rgb"}],u,h):new re([e.Function,"rgba(",-1,-1,{value:"rgba"}],u,[...h,new ne(i),new oe([c]),new ne([e.Number,toPrecision(r,4).toString(),-1,-1,{value:n.alpha,type:a.Number}])])}return new re([e.Function,"rgba(",-1,-1,{value:"rgba"}],u,[...h,new ne(i),new oe([c]),n.alpha])}function serializeHSL(n,r=!0){n.channels=convertPowerlessComponentsToZeroValuesForDisplay(n.channels,n.colorNotation);let o=n.channels.map((e=>Number.isNaN(e)?0:e));o=z(r?w(XYZ_D50_to_sRGB_Gamut(colorData_to_XYZ_D50(n).channels)):colorData_to_XYZ_D50(n).channels),o=o.map((e=>Number.isNaN(e)?0:e));const t=Math.min(360,Math.max(0,Math.round(toPrecision(o[0])))),l=Math.min(100,Math.max(0,Math.round(toPrecision(o[1])))),s=Math.min(100,Math.max(0,Math.round(toPrecision(o[2])))),u=[e.CloseParen,")",-1,-1,void 0],c=[e.Whitespace," ",-1,-1,void 0],i=[e.Comma,",",-1,-1,void 0],h=[new ne([e.Number,t.toString(),-1,-1,{value:o[0],type:a.Integer}]),new ne(i),new oe([c]),new ne([e.Percentage,l.toString()+"%",-1,-1,{value:o[1]}]),new ne(i),new oe([c]),new ne([e.Percentage,s.toString()+"%",-1,-1,{value:o[2]}])];if("number"==typeof n.alpha){const r=Math.min(1,Math.max(0,toPrecision(Number.isNaN(n.alpha)?0:n.alpha)));return 1===toPrecision(r,4)?new re([e.Function,"hsl(",-1,-1,{value:"hsl"}],u,h):new re([e.Function,"hsla(",-1,-1,{value:"hsla"}],u,[...h,new ne(i),new oe([c]),new ne([e.Number,toPrecision(r,4).toString(),-1,-1,{value:n.alpha,type:a.Number}])])}return new re([e.Function,"hsla(",-1,-1,{value:"hsla"}],u,[...h,new ne(i),new oe([c]),n.alpha])}function serializeOKLCH(n){n.channels=convertPowerlessComponentsToZeroValuesForDisplay(n.channels,n.colorNotation);let r=n.channels.map((e=>Number.isNaN(e)?0:e));n.colorNotation!==se.OKLCH&&(r=L(colorData_to_XYZ_D50(n).channels));const o=toPrecision(r[0],6),t=toPrecision(r[1],6),l=toPrecision(r[2],6),s=[e.Function,"oklch(",-1,-1,{value:"oklch"}],u=[e.Whitespace," ",-1,-1,void 0];return serializeWithAlpha(n,s,u,[new ne([e.Number,o.toString(),-1,-1,{value:r[0],type:a.Number}]),new oe([u]),new ne([e.Number,t.toString(),-1,-1,{value:r[1],type:a.Number}]),new oe([u]),new ne([e.Number,l.toString(),-1,-1,{value:r[2],type:a.Number}])])}function color(e){if(ae(e)){switch(toLowerCaseAZ(e.getName())){case"rgb":case"rgba":return rgb(e,color);case"hsl":case"hsla":return hsl(e,color);case"hwb":return a=color,threeChannelSpaceSeparated(e,normalize_HWB_ChannelValues,se.HWB,[],a);case"lab":return lab(e,color);case"lch":return lch(e,color);case"oklab":return oklab(e,color);case"oklch":return oklch(e,color);case"color":return color$1(e,color);case"color-mix":return colorMix(e,color);case"contrast-color":return contrastColor(e,color)}}var a;if(ee(e)){if(c(e.value))return hex(e.value);if(n(e.value)){const a=namedColor(e.value[4].value);return!1!==a?a:"transparent"===toLowerCaseAZ(e.value[4].value)&&{colorNotation:se.RGB,channels:[0,0,0],alpha:0,syntaxFlags:new Set([ue.ColorKeyword])}}}return!1}export{se as ColorNotation,ue as SyntaxFlag,color,colorDataFitsDisplayP3_Gamut,colorDataFitsRGB_Gamut,serializeHSL,serializeOKLCH,serializeP3,serializeRGB}; diff --git a/packages/css-color-parser/src/functions/color-mix.ts b/packages/css-color-parser/src/functions/color-mix.ts index 73f4ab57f..106199e29 100644 --- a/packages/css-color-parser/src/functions/color-mix.ts +++ b/packages/css-color-parser/src/functions/color-mix.ts @@ -175,6 +175,7 @@ function colorMixComponents(componentValues: Array, colorParser: censorIntoStandardRepresentableValues: true, precision: -1, toCanonicalUnits: true, + rawPercentages: true, }); if (!node || !isTokenNode(node) || !isTokenNumeric(node.value)) { diff --git a/packages/css-color-parser/src/functions/color.ts b/packages/css-color-parser/src/functions/color.ts index 0c298b39e..6873c629b 100644 --- a/packages/css-color-parser/src/functions/color.ts +++ b/packages/css-color-parser/src/functions/color.ts @@ -77,6 +77,7 @@ export function color(colorFunctionNode: FunctionNode, colorParser: ColorParser) globals: relativeColorChannelsWithoutNone, precision: -1, toCanonicalUnits: true, + rawPercentages: true, }); if (!result || !isTokenNode(result) || !isTokenNumeric(result.value)) { return false; diff --git a/packages/css-color-parser/src/functions/three-channel-legacy-syntax.ts b/packages/css-color-parser/src/functions/three-channel-legacy-syntax.ts index 65a9f420c..0249b76e5 100644 --- a/packages/css-color-parser/src/functions/three-channel-legacy-syntax.ts +++ b/packages/css-color-parser/src/functions/three-channel-legacy-syntax.ts @@ -70,6 +70,7 @@ export function threeChannelLegacySyntax( censorIntoStandardRepresentableValues: true, precision: -1, toCanonicalUnits: true, + rawPercentages: true, }); if (!result || !isTokenNode(result) || !isTokenNumeric(result.value)) { return false; diff --git a/packages/css-color-parser/src/functions/three-channel-space-separated.ts b/packages/css-color-parser/src/functions/three-channel-space-separated.ts index af3f629c5..00a773abe 100644 --- a/packages/css-color-parser/src/functions/three-channel-space-separated.ts +++ b/packages/css-color-parser/src/functions/three-channel-space-separated.ts @@ -79,6 +79,7 @@ export function threeChannelSpaceSeparated( globals: relativeColorChannelsWithoutNone, precision: -1, toCanonicalUnits: true, + rawPercentages: true, }); if (!result || !isTokenNode(result) || !isTokenNumeric(result.value)) { return false; diff --git a/sites/postcss-preset-env/src/static/js/blog_calc_2023_02_21.js b/sites/postcss-preset-env/src/static/js/blog_calc_2023_02_21.js index 772fdfa5b..39d66d151 100644 --- a/sites/postcss-preset-env/src/static/js/blog_calc_2023_02_21.js +++ b/sites/postcss-preset-env/src/static/js/blog_calc_2023_02_21.js @@ -17,7 +17,7 @@ function renderResult() { } const wrappedValue = `calc(${value})`; - const solved = calc(wrappedValue); + const solved = calc(wrappedValue, { rawPercentages: true }); if (solved === wrappedValue) { outputEl.value = value; return;