@tbela99/css-parser
Version:
CSS parser for node and the browser
1,457 lines (1,446 loc) • 790 kB
JavaScript
'use strict';
var process = require('node:process');
var promises = require('node:fs/promises');
var SyntaxValidationResult;
(function (SyntaxValidationResult) {
SyntaxValidationResult[SyntaxValidationResult["Valid"] = 0] = "Valid";
SyntaxValidationResult[SyntaxValidationResult["Drop"] = 1] = "Drop";
SyntaxValidationResult[SyntaxValidationResult["Lenient"] = 2] = "Lenient"; /* preserve unknown at-rules, declarations and pseudo-classes */
})(SyntaxValidationResult || (SyntaxValidationResult = {}));
/**
* validation level enum
*/
exports.ValidationLevel = void 0;
(function (ValidationLevel) {
/**
* disable validation
*/
ValidationLevel[ValidationLevel["None"] = 0] = "None";
/**
* validate selectors and at-rules
*/
ValidationLevel[ValidationLevel["Default"] = 1] = "Default";
/**
* validate selectors, at-rules and declarations
*/
ValidationLevel[ValidationLevel["All"] = 2] = "All"; // selectors + at-rules + declarations
})(exports.ValidationLevel || (exports.ValidationLevel = {}));
/**
* token types enum
*/
exports.EnumToken = void 0;
(function (EnumToken) {
EnumToken[EnumToken["CommentTokenType"] = 0] = "CommentTokenType";
EnumToken[EnumToken["CDOCOMMTokenType"] = 1] = "CDOCOMMTokenType";
EnumToken[EnumToken["StyleSheetNodeType"] = 2] = "StyleSheetNodeType";
EnumToken[EnumToken["AtRuleNodeType"] = 3] = "AtRuleNodeType";
EnumToken[EnumToken["RuleNodeType"] = 4] = "RuleNodeType";
EnumToken[EnumToken["DeclarationNodeType"] = 5] = "DeclarationNodeType";
EnumToken[EnumToken["LiteralTokenType"] = 6] = "LiteralTokenType";
EnumToken[EnumToken["IdenTokenType"] = 7] = "IdenTokenType";
EnumToken[EnumToken["DashedIdenTokenType"] = 8] = "DashedIdenTokenType";
EnumToken[EnumToken["CommaTokenType"] = 9] = "CommaTokenType";
EnumToken[EnumToken["ColonTokenType"] = 10] = "ColonTokenType";
EnumToken[EnumToken["SemiColonTokenType"] = 11] = "SemiColonTokenType";
EnumToken[EnumToken["NumberTokenType"] = 12] = "NumberTokenType";
EnumToken[EnumToken["AtRuleTokenType"] = 13] = "AtRuleTokenType";
EnumToken[EnumToken["PercentageTokenType"] = 14] = "PercentageTokenType";
EnumToken[EnumToken["FunctionTokenType"] = 15] = "FunctionTokenType";
EnumToken[EnumToken["TimelineFunctionTokenType"] = 16] = "TimelineFunctionTokenType";
EnumToken[EnumToken["TimingFunctionTokenType"] = 17] = "TimingFunctionTokenType";
EnumToken[EnumToken["UrlFunctionTokenType"] = 18] = "UrlFunctionTokenType";
EnumToken[EnumToken["ImageFunctionTokenType"] = 19] = "ImageFunctionTokenType";
EnumToken[EnumToken["StringTokenType"] = 20] = "StringTokenType";
EnumToken[EnumToken["UnclosedStringTokenType"] = 21] = "UnclosedStringTokenType";
EnumToken[EnumToken["DimensionTokenType"] = 22] = "DimensionTokenType";
EnumToken[EnumToken["LengthTokenType"] = 23] = "LengthTokenType";
EnumToken[EnumToken["AngleTokenType"] = 24] = "AngleTokenType";
EnumToken[EnumToken["TimeTokenType"] = 25] = "TimeTokenType";
EnumToken[EnumToken["FrequencyTokenType"] = 26] = "FrequencyTokenType";
EnumToken[EnumToken["ResolutionTokenType"] = 27] = "ResolutionTokenType";
EnumToken[EnumToken["HashTokenType"] = 28] = "HashTokenType";
EnumToken[EnumToken["BlockStartTokenType"] = 29] = "BlockStartTokenType";
EnumToken[EnumToken["BlockEndTokenType"] = 30] = "BlockEndTokenType";
EnumToken[EnumToken["AttrStartTokenType"] = 31] = "AttrStartTokenType";
EnumToken[EnumToken["AttrEndTokenType"] = 32] = "AttrEndTokenType";
EnumToken[EnumToken["StartParensTokenType"] = 33] = "StartParensTokenType";
EnumToken[EnumToken["EndParensTokenType"] = 34] = "EndParensTokenType";
EnumToken[EnumToken["ParensTokenType"] = 35] = "ParensTokenType";
EnumToken[EnumToken["WhitespaceTokenType"] = 36] = "WhitespaceTokenType";
EnumToken[EnumToken["IncludeMatchTokenType"] = 37] = "IncludeMatchTokenType";
EnumToken[EnumToken["DashMatchTokenType"] = 38] = "DashMatchTokenType";
EnumToken[EnumToken["EqualMatchTokenType"] = 39] = "EqualMatchTokenType";
EnumToken[EnumToken["LtTokenType"] = 40] = "LtTokenType";
EnumToken[EnumToken["LteTokenType"] = 41] = "LteTokenType";
EnumToken[EnumToken["GtTokenType"] = 42] = "GtTokenType";
EnumToken[EnumToken["GteTokenType"] = 43] = "GteTokenType";
EnumToken[EnumToken["PseudoClassTokenType"] = 44] = "PseudoClassTokenType";
EnumToken[EnumToken["PseudoClassFuncTokenType"] = 45] = "PseudoClassFuncTokenType";
EnumToken[EnumToken["DelimTokenType"] = 46] = "DelimTokenType";
EnumToken[EnumToken["UrlTokenTokenType"] = 47] = "UrlTokenTokenType";
EnumToken[EnumToken["EOFTokenType"] = 48] = "EOFTokenType";
EnumToken[EnumToken["ImportantTokenType"] = 49] = "ImportantTokenType";
EnumToken[EnumToken["ColorTokenType"] = 50] = "ColorTokenType";
EnumToken[EnumToken["AttrTokenType"] = 51] = "AttrTokenType";
EnumToken[EnumToken["BadCommentTokenType"] = 52] = "BadCommentTokenType";
EnumToken[EnumToken["BadCdoTokenType"] = 53] = "BadCdoTokenType";
EnumToken[EnumToken["BadUrlTokenType"] = 54] = "BadUrlTokenType";
EnumToken[EnumToken["BadStringTokenType"] = 55] = "BadStringTokenType";
EnumToken[EnumToken["BinaryExpressionTokenType"] = 56] = "BinaryExpressionTokenType";
EnumToken[EnumToken["UnaryExpressionTokenType"] = 57] = "UnaryExpressionTokenType";
EnumToken[EnumToken["FlexTokenType"] = 58] = "FlexTokenType";
/* catch all */
EnumToken[EnumToken["ListToken"] = 59] = "ListToken";
/* arithmetic tokens */
EnumToken[EnumToken["Add"] = 60] = "Add";
EnumToken[EnumToken["Mul"] = 61] = "Mul";
EnumToken[EnumToken["Div"] = 62] = "Div";
EnumToken[EnumToken["Sub"] = 63] = "Sub";
/* new tokens */
EnumToken[EnumToken["ColumnCombinatorTokenType"] = 64] = "ColumnCombinatorTokenType";
EnumToken[EnumToken["ContainMatchTokenType"] = 65] = "ContainMatchTokenType";
EnumToken[EnumToken["StartMatchTokenType"] = 66] = "StartMatchTokenType";
EnumToken[EnumToken["EndMatchTokenType"] = 67] = "EndMatchTokenType";
EnumToken[EnumToken["MatchExpressionTokenType"] = 68] = "MatchExpressionTokenType";
EnumToken[EnumToken["NameSpaceAttributeTokenType"] = 69] = "NameSpaceAttributeTokenType";
EnumToken[EnumToken["FractionTokenType"] = 70] = "FractionTokenType";
EnumToken[EnumToken["IdenListTokenType"] = 71] = "IdenListTokenType";
EnumToken[EnumToken["GridTemplateFuncTokenType"] = 72] = "GridTemplateFuncTokenType";
EnumToken[EnumToken["KeyFrameRuleNodeType"] = 73] = "KeyFrameRuleNodeType";
EnumToken[EnumToken["ClassSelectorTokenType"] = 74] = "ClassSelectorTokenType";
EnumToken[EnumToken["UniversalSelectorTokenType"] = 75] = "UniversalSelectorTokenType";
EnumToken[EnumToken["ChildCombinatorTokenType"] = 76] = "ChildCombinatorTokenType";
EnumToken[EnumToken["DescendantCombinatorTokenType"] = 77] = "DescendantCombinatorTokenType";
EnumToken[EnumToken["NextSiblingCombinatorTokenType"] = 78] = "NextSiblingCombinatorTokenType";
EnumToken[EnumToken["SubsequentSiblingCombinatorTokenType"] = 79] = "SubsequentSiblingCombinatorTokenType";
EnumToken[EnumToken["NestingSelectorTokenType"] = 80] = "NestingSelectorTokenType";
EnumToken[EnumToken["InvalidRuleTokenType"] = 81] = "InvalidRuleTokenType";
EnumToken[EnumToken["InvalidClassSelectorTokenType"] = 82] = "InvalidClassSelectorTokenType";
EnumToken[EnumToken["InvalidAttrTokenType"] = 83] = "InvalidAttrTokenType";
EnumToken[EnumToken["InvalidAtRuleTokenType"] = 84] = "InvalidAtRuleTokenType";
EnumToken[EnumToken["MediaQueryConditionTokenType"] = 85] = "MediaQueryConditionTokenType";
EnumToken[EnumToken["MediaFeatureTokenType"] = 86] = "MediaFeatureTokenType";
EnumToken[EnumToken["MediaFeatureOnlyTokenType"] = 87] = "MediaFeatureOnlyTokenType";
EnumToken[EnumToken["MediaFeatureNotTokenType"] = 88] = "MediaFeatureNotTokenType";
EnumToken[EnumToken["MediaFeatureAndTokenType"] = 89] = "MediaFeatureAndTokenType";
EnumToken[EnumToken["MediaFeatureOrTokenType"] = 90] = "MediaFeatureOrTokenType";
EnumToken[EnumToken["PseudoPageTokenType"] = 91] = "PseudoPageTokenType";
EnumToken[EnumToken["PseudoElementTokenType"] = 92] = "PseudoElementTokenType";
EnumToken[EnumToken["KeyframeAtRuleNodeType"] = 93] = "KeyframeAtRuleNodeType";
EnumToken[EnumToken["InvalidDeclarationNodeType"] = 94] = "InvalidDeclarationNodeType";
/* aliases */
EnumToken[EnumToken["Time"] = 25] = "Time";
EnumToken[EnumToken["Iden"] = 7] = "Iden";
EnumToken[EnumToken["EOF"] = 48] = "EOF";
EnumToken[EnumToken["Hash"] = 28] = "Hash";
EnumToken[EnumToken["Flex"] = 58] = "Flex";
EnumToken[EnumToken["Angle"] = 24] = "Angle";
EnumToken[EnumToken["Color"] = 50] = "Color";
EnumToken[EnumToken["Comma"] = 9] = "Comma";
EnumToken[EnumToken["String"] = 20] = "String";
EnumToken[EnumToken["Length"] = 23] = "Length";
EnumToken[EnumToken["Number"] = 12] = "Number";
EnumToken[EnumToken["Perc"] = 14] = "Perc";
EnumToken[EnumToken["Literal"] = 6] = "Literal";
EnumToken[EnumToken["Comment"] = 0] = "Comment";
EnumToken[EnumToken["UrlFunc"] = 18] = "UrlFunc";
EnumToken[EnumToken["Dimension"] = 22] = "Dimension";
EnumToken[EnumToken["Frequency"] = 26] = "Frequency";
EnumToken[EnumToken["Resolution"] = 27] = "Resolution";
EnumToken[EnumToken["Whitespace"] = 36] = "Whitespace";
EnumToken[EnumToken["IdenList"] = 71] = "IdenList";
EnumToken[EnumToken["DashedIden"] = 8] = "DashedIden";
EnumToken[EnumToken["GridTemplateFunc"] = 72] = "GridTemplateFunc";
EnumToken[EnumToken["ImageFunc"] = 19] = "ImageFunc";
EnumToken[EnumToken["CommentNodeType"] = 0] = "CommentNodeType";
EnumToken[EnumToken["CDOCOMMNodeType"] = 1] = "CDOCOMMNodeType";
EnumToken[EnumToken["TimingFunction"] = 17] = "TimingFunction";
EnumToken[EnumToken["TimelineFunction"] = 16] = "TimelineFunction";
})(exports.EnumToken || (exports.EnumToken = {}));
// xyz-d65 is an alias for xyz
exports.ColorType = void 0;
(function (ColorType) {
ColorType[ColorType["SYS"] = 0] = "SYS";
ColorType[ColorType["DPSYS"] = 1] = "DPSYS";
ColorType[ColorType["LIT"] = 2] = "LIT";
ColorType[ColorType["HEX"] = 3] = "HEX";
ColorType[ColorType["RGBA"] = 4] = "RGBA";
ColorType[ColorType["HSLA"] = 5] = "HSLA";
ColorType[ColorType["HWB"] = 6] = "HWB";
ColorType[ColorType["CMYK"] = 7] = "CMYK";
ColorType[ColorType["OKLAB"] = 8] = "OKLAB";
ColorType[ColorType["OKLCH"] = 9] = "OKLCH";
ColorType[ColorType["LAB"] = 10] = "LAB";
ColorType[ColorType["LCH"] = 11] = "LCH";
ColorType[ColorType["COLOR"] = 12] = "COLOR";
ColorType[ColorType["SRGB"] = 13] = "SRGB";
ColorType[ColorType["PROPHOTO_RGB"] = 14] = "PROPHOTO_RGB";
ColorType[ColorType["A98_RGB"] = 15] = "A98_RGB";
ColorType[ColorType["REC2020"] = 16] = "REC2020";
ColorType[ColorType["DISPLAY_P3"] = 17] = "DISPLAY_P3";
ColorType[ColorType["SRGB_LINEAR"] = 18] = "SRGB_LINEAR";
ColorType[ColorType["XYZ_D50"] = 19] = "XYZ_D50";
ColorType[ColorType["XYZ_D65"] = 20] = "XYZ_D65";
ColorType[ColorType["LIGHT_DARK"] = 21] = "LIGHT_DARK";
ColorType[ColorType["COLOR_MIX"] = 22] = "COLOR_MIX";
ColorType[ColorType["RGB"] = 4] = "RGB";
ColorType[ColorType["HSL"] = 5] = "HSL";
ColorType[ColorType["XYZ"] = 20] = "XYZ";
ColorType[ColorType["DEVICE_CMYK"] = 7] = "DEVICE_CMYK";
})(exports.ColorType || (exports.ColorType = {}));
// from https://www.w3.org/TR/css-color-4/multiply-matrices.js
/**
* Simple matrix (and vector) multiplication
* Warning: No error handling for incompatible dimensions!
* @author Lea Verou 2020 MIT License
*/
// A is m x n. B is n x p. product is m x p.
function multiplyMatrices(A, B) {
let m = A.length;
if (!Array.isArray(A[0])) {
// A is vector, convert to [[a, b, c, ...]]
A = [A];
}
if (!Array.isArray(B[0])) {
// B is vector, convert to [[a], [b], [c], ...]]
B = B.map((x) => [x]);
}
let p = B[0].length;
let B_cols = B[0].map((_, i) => B.map((x) => x[i])); // transpose B
let product = A.map((row) => B_cols.map((col) => {
if (!Array.isArray(row)) {
return col.reduce((a, c) => a + c * row, 0);
}
return row.reduce((a, c, i) => a + c * (col[i] || 0), 0);
}));
if (m === 1) {
product = product[0]; // Avoid [[a, b, c, ...]]
}
if (p === 1) {
return product.map((x) => x[0]); // Avoid [[a], [b], [c], ...]]
}
return product;
}
const colorPrecision = 6;
const anglePrecision = .001;
const colorRange = {
lab: {
l: [0, 100],
a: [-125, 125],
b: [-125, 125]
},
lch: {
l: [0, 100],
c: [0, 150],
h: [0, 360]
},
oklab: {
l: [0, 1],
a: [-0.4, .4],
b: [-0.4, 0.4]
},
oklch: {
l: [0, 1],
a: [0, .4],
b: [0, 0.4]
}
};
const timelineFunc = ['view', 'scroll'];
const generalEnclosedFunc = ['selector', 'font-tech', 'font-format', 'media', 'supports'];
const timingFunc = ['ease', 'ease-in', 'ease-out', 'ease-in-out', 'linear', 'step-start', 'step-end', 'steps', 'cubic-bezier'];
const funcLike = [
exports.EnumToken.ParensTokenType,
exports.EnumToken.FunctionTokenType,
exports.EnumToken.UrlFunctionTokenType,
exports.EnumToken.StartParensTokenType,
exports.EnumToken.ImageFunctionTokenType,
exports.EnumToken.TimingFunctionTokenType,
exports.EnumToken.TimingFunctionTokenType,
exports.EnumToken.PseudoClassFuncTokenType,
exports.EnumToken.GridTemplateFuncTokenType
];
const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk', 'color-mix', 'color', 'oklab', 'lab', 'oklch', 'lch', 'light-dark'];
const colorFuncColorSpace = ['srgb', 'srgb-linear', 'display-p3', 'prophoto-rgb', 'a98-rgb', 'rec2020', 'xyz', 'xyz-d65', 'xyz-d50'];
const D50 = [0.3457 / 0.3585, 1.00000, (1.0 - 0.3457 - 0.3585) / 0.3585];
const k = Math.pow(29, 3) / Math.pow(3, 3);
const e = Math.pow(6, 3) / Math.pow(29, 3);
// color module v4
const systemColors = new Set(['ActiveText', 'ButtonBorder', 'ButtonFace', 'ButtonText', 'Canvas', 'CanvasText', 'Field', 'FieldText', 'GrayText', 'Highlight', 'HighlightText', 'LinkText', 'Mark', 'MarkText', 'VisitedText', '-webkit-focus-ring-color'].map(m => m.toLowerCase()));
// deprecated
const deprecatedSystemColors = new Set(['ActiveBorder', 'ActiveCaption', 'AppWorkspace', 'Background', 'ButtonFace', 'ButtonHighlight', 'ButtonShadow', 'ButtonText', 'CaptionText', 'GrayText', 'Highlight', 'HighlightText', 'InactiveBorder', 'InactiveCaption', 'InactiveCaptionText', 'InfoBackground', 'InfoText', 'Menu', 'MenuText', 'Scrollbar', 'ThreeDDarkShadow', 'ThreeDFace', 'ThreeDHighlight', 'ThreeDLightShadow', 'ThreeDShadow', 'Window', 'WindowFrame', 'WindowText'].map(t => t.toLowerCase()));
// name to color
const COLORS_NAMES = Object.seal({
'aliceblue': '#f0f8ff',
'antiquewhite': '#faebd7',
'aqua': '#00ffff',
'aquamarine': '#7fffd4',
'azure': '#f0ffff',
'beige': '#f5f5dc',
'bisque': '#ffe4c4',
'black': '#000000',
'blanchedalmond': '#ffebcd',
'blue': '#0000ff',
'blueviolet': '#8a2be2',
'brown': '#a52a2a',
'burlywood': '#deb887',
'cadetblue': '#5f9ea0',
'chartreuse': '#7fff00',
'chocolate': '#d2691e',
'coral': '#ff7f50',
'cornflowerblue': '#6495ed',
'cornsilk': '#fff8dc',
'crimson': '#dc143c',
'cyan': '#00ffff',
'darkblue': '#00008b',
'darkcyan': '#008b8b',
'darkgoldenrod': '#b8860b',
'darkgray': '#a9a9a9',
'darkgrey': '#a9a9a9',
'darkgreen': '#006400',
'darkkhaki': '#bdb76b',
'darkmagenta': '#8b008b',
'darkolivegreen': '#556b2f',
'darkorange': '#ff8c00',
'darkorchid': '#9932cc',
'darkred': '#8b0000',
'darksalmon': '#e9967a',
'darkseagreen': '#8fbc8f',
'darkslateblue': '#483d8b',
'darkslategray': '#2f4f4f',
'darkslategrey': '#2f4f4f',
'darkturquoise': '#00ced1',
'darkviolet': '#9400d3',
'deeppink': '#ff1493',
'deepskyblue': '#00bfff',
'dimgray': '#696969',
'dimgrey': '#696969',
'dodgerblue': '#1e90ff',
'firebrick': '#b22222',
'floralwhite': '#fffaf0',
'forestgreen': '#228b22',
'fuchsia': '#ff00ff',
'gainsboro': '#dcdcdc',
'ghostwhite': '#f8f8ff',
'gold': '#ffd700',
'goldenrod': '#daa520',
'gray': '#808080',
'grey': '#808080',
'green': '#008000',
'greenyellow': '#adff2f',
'honeydew': '#f0fff0',
'hotpink': '#ff69b4',
'indianred': '#cd5c5c',
'indigo': '#4b0082',
'ivory': '#fffff0',
'khaki': '#f0e68c',
'lavender': '#e6e6fa',
'lavenderblush': '#fff0f5',
'lawngreen': '#7cfc00',
'lemonchiffon': '#fffacd',
'lightblue': '#add8e6',
'lightcoral': '#f08080',
'lightcyan': '#e0ffff',
'lightgoldenrodyellow': '#fafad2',
'lightgray': '#d3d3d3',
'lightgrey': '#d3d3d3',
'lightgreen': '#90ee90',
'lightpink': '#ffb6c1',
'lightsalmon': '#ffa07a',
'lightseagreen': '#20b2aa',
'lightskyblue': '#87cefa',
'lightslategray': '#778899',
'lightslategrey': '#778899',
'lightsteelblue': '#b0c4de',
'lightyellow': '#ffffe0',
'lime': '#00ff00',
'limegreen': '#32cd32',
'linen': '#faf0e6',
'magenta': '#ff00ff',
'maroon': '#800000',
'mediumaquamarine': '#66cdaa',
'mediumblue': '#0000cd',
'mediumorchid': '#ba55d3',
'mediumpurple': '#9370d8',
'mediumseagreen': '#3cb371',
'mediumslateblue': '#7b68ee',
'mediumspringgreen': '#00fa9a',
'mediumturquoise': '#48d1cc',
'mediumvioletred': '#c71585',
'midnightblue': '#191970',
'mintcream': '#f5fffa',
'mistyrose': '#ffe4e1',
'moccasin': '#ffe4b5',
'navajowhite': '#ffdead',
'navy': '#000080',
'oldlace': '#fdf5e6',
'olive': '#808000',
'olivedrab': '#6b8e23',
'orange': '#ffa500',
'orangered': '#ff4500',
'orchid': '#da70d6',
'palegoldenrod': '#eee8aa',
'palegreen': '#98fb98',
'paleturquoise': '#afeeee',
'palevioletred': '#d87093',
'papayawhip': '#ffefd5',
'peachpuff': '#ffdab9',
'peru': '#cd853f',
'pink': '#ffc0cb',
'plum': '#dda0dd',
'powderblue': '#b0e0e6',
'purple': '#800080',
'red': '#ff0000',
'rosybrown': '#bc8f8f',
'royalblue': '#4169e1',
'saddlebrown': '#8b4513',
'salmon': '#fa8072',
'sandybrown': '#f4a460',
'seagreen': '#2e8b57',
'seashell': '#fff5ee',
'sienna': '#a0522d',
'silver': '#c0c0c0',
'skyblue': '#87ceeb',
'slateblue': '#6a5acd',
'slategray': '#708090',
'slategrey': '#708090',
'snow': '#fffafa',
'springgreen': '#00ff7f',
'steelblue': '#4682b4',
'tan': '#d2b48c',
'teal': '#008080',
'thistle': '#d8bfd8',
'tomato': '#ff6347',
'turquoise': '#40e0d0',
'violet': '#ee82ee',
'wheat': '#f5deb3',
'white': '#ffffff',
'whitesmoke': '#f5f5f5',
'yellow': '#ffff00',
'yellowgreen': '#9acd32',
'rebeccapurple': '#663399',
'transparent': '#00000000'
});
// color to name
const NAMES_COLORS = Object.seal(Object.entries(COLORS_NAMES).reduce((acc, [key, value]) => {
acc[value] = key;
return acc;
}, Object.create(null)));
function hex2lchToken(token) {
const values = hex2lchvalues(token);
if (values == null) {
return null;
}
return lchToken(values);
}
function rgb2lchToken(token) {
const values = rgb2lchvalues(token);
if (values == null) {
return null;
}
return lchToken(values);
}
function hsl2lchToken(token) {
const values = hsl2lchvalues(token);
if (values == null) {
return null;
}
return lchToken(values);
}
function hwb2lchToken(token) {
const values = hwb2lchvalues(token);
if (values == null) {
return null;
}
return lchToken(values);
}
function cmyk2lchToken(token) {
const values = cmyk2lchvalues(token);
if (values == null) {
return null;
}
return lchToken(values);
}
function lab2lchToken(token) {
const values = lab2lchvalues(token);
if (values == null) {
return null;
}
return lchToken(values);
}
function oklab2lchToken(token) {
const values = oklab2lchvalues(token);
if (values == null) {
return null;
}
return lchToken(values);
}
function oklch2lchToken(token) {
const values = oklch2lchvalues(token);
if (values == null) {
return null;
}
return lchToken(values);
}
function color2lchToken(token) {
const values = color2lchvalues(token);
if (values == null) {
return null;
}
return lchToken(values);
}
function lchToken(values) {
values[2] = toPrecisionAngle(values[2]);
const chi = [
{ typ: exports.EnumToken.NumberTokenType, val: String(values[0]) },
{ typ: exports.EnumToken.NumberTokenType, val: String(values[1]) },
{ typ: exports.EnumToken.NumberTokenType, val: String(values[2]) },
];
if (values.length == 4) {
chi.push({ typ: exports.EnumToken.LiteralTokenType, val: '/' }, { typ: exports.EnumToken.PercentageTokenType, val: (values[3] * 100).toFixed() });
}
return {
typ: exports.EnumToken.ColorTokenType,
val: 'lch',
chi,
kin: exports.ColorType.LCH
};
}
function hex2lchvalues(token) {
const values = hex2labvalues(token);
// @ts-ignore
return values == null ? null : labvalues2lchvalues(...values);
}
function rgb2lchvalues(token) {
const values = rgb2labvalues(token);
// @ts-ignore
return values == null ? null : labvalues2lchvalues(...values);
}
function hsl2lchvalues(token) {
const values = hsl2labvalues(token);
// @ts-ignore
return values == null ? null : labvalues2lchvalues(...values);
}
function hwb2lchvalues(token) {
const values = hwb2labvalues(token);
// @ts-ignore
return values == null ? null : labvalues2lchvalues(...values);
}
function lab2lchvalues(token) {
const values = getLABComponents(token);
// @ts-ignore
return values == null ? null : labvalues2lchvalues(...values);
}
function srgb2lch(r, g, blue, alpha) {
// @ts-ignore
return labvalues2lchvalues(...srgb2labvalues(r, g, blue, alpha));
}
function oklab2lchvalues(token) {
const values = oklab2labvalues(token);
// @ts-ignore
return values == null ? null : labvalues2lchvalues(...values);
}
function cmyk2lchvalues(token) {
const values = cmyk2srgbvalues(token);
// @ts-ignore
return values == null ? null : srgb2lch(...values);
}
function oklch2lchvalues(token) {
const values = oklch2labvalues(token);
if (values == null) {
return null;
}
// @ts-ignore
return labvalues2lchvalues(...values);
}
function color2lchvalues(token) {
const values = color2srgbvalues(token);
if (values == null) {
return null;
}
// @ts-ignore
return srgb2lch(...values);
}
function labvalues2lchvalues(l, a, b, alpha = null) {
let c = Math.sqrt(a * a + b * b);
let h = Math.atan2(b, a) * 180 / Math.PI;
if (h < 0) {
h += 360;
}
if (c < .0001) {
c = h = 0;
}
return alpha == null ? [l, c, h] : [l, c, h, alpha];
}
function xyz2lchvalues(x, y, z, alpha) {
// @ts-ignore(
const lch = labvalues2lchvalues(...xyz2lab(x, y, z));
return alpha == null || alpha == 1 ? lch : lch.concat(alpha);
}
function getLCHComponents(token) {
const components = getComponents(token);
if (components == null) {
return null;
}
for (let i = 0; i < components.length; i++) {
if (![exports.EnumToken.NumberTokenType, exports.EnumToken.PercentageTokenType, exports.EnumToken.AngleTokenType, exports.EnumToken.IdenTokenType].includes(components[i].typ)) {
return null;
}
}
// @ts-ignore
let t = components[0];
// @ts-ignore
const l = getNumber(t) * (t.typ == exports.EnumToken.PercentageTokenType ? 100 : 1);
// @ts-ignore
t = components[1];
// @ts-ignore
const c = getNumber(t) * (t.typ == exports.EnumToken.PercentageTokenType ? 150 : 1);
// @ts-ignore
t = components[2];
// @ts-ignore
const h = getAngle(t) * 360;
// @ts-ignore
t = components[3];
// @ts-ignore
const alpha = t == null ? 1 : getNumber(t);
return alpha == null ? [l, c, h] : [l, c, h, alpha];
}
/*
*/
function xyzd502lch(x, y, z, alpha) {
// @ts-ignore
const [l, a, b] = xyz2lab(...XYZ_D50_to_D65(x, y, z));
// L in range [0,100]. For use in CSS, add a percent
return labvalues2lchvalues(l, a, b, alpha);
}
function XYZ_D65_to_D50(x, y, z, alpha = null) {
// Bradford chromatic adaptation from D65 to D50
// The matrix below is the result of three operations:
// - convert from XYZ to retinal cone domain
// - scale components from one reference white to another
// - convert back to XYZ
// see https://github.com/LeaVerou/color.js/pull/354/files
let M = [
[1.0479297925449969, 0.022946870601609652, -0.05019226628920524],
[0.02962780877005599, 0.9904344267538799, -0.017073799063418826],
[-0.009243040646204504, 0.015055191490298152, 0.7518742814281371]
];
return multiplyMatrices(M, [x, y, z]).concat(alpha == null || alpha == 1 ? [] : [alpha]);
}
function xyzd502srgb(x, y, z, alpha = null) {
// @ts-ignore
return lsrgb2srgbvalues(
/* r: */
x * 3.1341359569958707 -
y * 1.6173863321612538 -
0.4906619460083532 * z,
/* g: */
x * -0.978795502912089 +
y * 1.916254567259524 +
0.03344273116131949 * z,
/* b: */
x * 0.07195537988411677 -
y * 0.2289768264158322 +
1.405386058324125 * z, alpha);
}
function XYZ_to_lin_sRGB(x, y, z, alpha = null) {
// convert XYZ to linear-light sRGB
const M = [
[12831 / 3959, -329 / 214, -1974 / 3959],
[-851781 / 878810, 1648619 / 878810, 36519 / 878810],
[705 / 12673, -2585 / 12673, 705 / 667],
];
const XYZ = [x, y, z]; // convert to XYZ
return multiplyMatrices(M, XYZ).map((v) => v).concat(alpha == null || alpha == 1 ? [] : [alpha]);
}
function XYZ_D50_to_D65(x, y, z) {
// Bradford chromatic adaptation from D50 to D65
const M = [
[0.9554734527042182, -0.023098536874261423, 0.0632593086610217],
[-0.028369706963208136, 1.0099954580058226, 0.021041398966943008],
[0.012314001688319899, -0.020507696433477912, 1.3303659366080753]
];
const XYZ = [x, y, z];
return multiplyMatrices(M, XYZ); //.map((v: number) => v);
}
// xyz d65
function srgb2xyz(r, g, b, alpha) {
[r, g, b] = srgb2lsrgbvalues(r, g, b);
// xyx d65
let rgb = [
0.4123907992659595 * r +
0.35758433938387796 * g +
0.1804807884018343 * b,
0.21263900587151036 * r +
0.7151686787677559 * g +
0.07219231536073371 * b,
0.01933081871559185 * r +
0.11919477979462599 * g +
0.9505321522496606 * b
];
if (alpha != null && alpha != 1) {
rgb.push(alpha);
}
return rgb;
}
// xyz d50
function srgb2xyz_d50(r, g, b, alpha) {
// xyx d65
// @ts-ignore
let rgb = XYZ_D65_to_D50(...srgb2xyz(r, g, b));
if (alpha != null && alpha != 1) {
rgb.push(alpha);
}
return rgb;
}
function hex2oklchToken(token) {
const values = hex2oklchvalues(token);
return oklchToken(values);
}
function rgb2oklchToken(token) {
const values = rgb2oklchvalues(token);
if (values == null) {
return null;
}
return oklchToken(values);
}
function hsl2oklchToken(token) {
const values = hsl2oklchvalues(token);
if (values == null) {
return null;
}
return oklchToken(values);
}
function hwb2oklchToken(token) {
const values = hwb2oklchvalues(token);
if (values == null) {
return null;
}
return oklchToken(values);
}
function cmyk2oklchToken(token) {
const values = cmyk2oklchvalues(token);
if (values == null) {
return null;
}
return oklchToken(values);
}
function lab2oklchToken(token) {
const values = lab2oklchvalues(token);
if (values == null) {
return null;
}
return oklchToken(values);
}
function oklab2oklchToken(token) {
const values = oklab2oklchvalues(token);
if (values == null) {
return null;
}
return oklchToken(values);
}
function lch2oklchToken(token) {
const values = lch2oklchvalues(token);
if (values == null) {
return null;
}
return oklchToken(values);
}
function color2oklchToken(token) {
const values = color2srgbvalues(token);
if (values == null) {
return null;
}
// @ts-ignore
return oklchToken(srgb2oklch(...values));
}
function oklchToken(values) {
values[2] = toPrecisionAngle(values[2]);
const chi = [
{ typ: exports.EnumToken.NumberTokenType, val: String(values[0]) },
{ typ: exports.EnumToken.NumberTokenType, val: String(values[1]) },
{ typ: exports.EnumToken.NumberTokenType, val: String(values[2]) },
];
if (values.length == 4) {
chi.push({ typ: exports.EnumToken.LiteralTokenType, val: '/' }, {
typ: exports.EnumToken.PercentageTokenType,
val: (values[3] * 100).toFixed()
});
}
return {
typ: exports.EnumToken.ColorTokenType,
val: 'oklch',
chi,
kin: exports.ColorType.OKLCH
};
}
function hex2oklchvalues(token) {
// @ts-ignore
return labvalues2lchvalues(...hex2oklabvalues(token));
}
function rgb2oklchvalues(token) {
const values = rgb2oklabvalues(token);
if (values == null) {
return null;
}
// @ts-ignore
return labvalues2lchvalues(...values);
}
function hsl2oklchvalues(token) {
// @ts-ignore
return labvalues2lchvalues(...hsl2oklabvalues(token));
}
function hwb2oklchvalues(token) {
// @ts-ignore
return labvalues2lchvalues(...hwb2oklabvalues(token));
}
function cmyk2oklchvalues(token) {
const values = cmyk2srgbvalues(token);
// @ts-ignore
return values == null ? null : srgb2oklch(...values);
}
function lab2oklchvalues(token) {
// @ts-ignore
return labvalues2lchvalues(...lab2oklabvalues(token));
}
function lch2oklchvalues(token) {
// @ts-ignore
return labvalues2lchvalues(...lch2oklabvalues(token));
}
function oklab2oklchvalues(token) {
// @ts-ignore
return labvalues2lchvalues(...getOKLABComponents(token));
}
function srgb2oklch(r, g, blue, alpha) {
// @ts-ignore
return labvalues2lchvalues(...srgb2oklab(r, g, blue, alpha));
}
function getOKLCHComponents(token) {
const components = getComponents(token);
if (components == null) {
return null;
}
for (let i = 0; i < components.length; i++) {
if (![exports.EnumToken.NumberTokenType, exports.EnumToken.PercentageTokenType, exports.EnumToken.AngleTokenType, exports.EnumToken.IdenTokenType].includes(components[i].typ)) {
return [];
}
}
// @ts-ignore
let t = components[0];
// @ts-ignore
const l = getNumber(t);
// @ts-ignore
t = components[1];
// @ts-ignore
const c = getNumber(t) * (t.typ == exports.EnumToken.PercentageTokenType ? .4 : 1);
// @ts-ignore
t = components[2];
// @ts-ignore
const h = getAngle(t) * 360;
// @ts-ignore
t = components[3];
// @ts-ignore
const alpha = t == null || (t.typ == exports.EnumToken.IdenTokenType && t.val == 'none') ? 1 : getNumber(t);
return [l, c, h, alpha];
}
function hex2oklabToken(token) {
const values = hex2oklabvalues(token);
if (values == null) {
return null;
}
return oklabToken(values);
}
function rgb2oklabToken(token) {
const values = rgb2oklabvalues(token);
if (values == null) {
return null;
}
return oklabToken(values);
}
function hsl2oklabToken(token) {
const values = hsl2oklabvalues(token);
if (values == null) {
return null;
}
return oklabToken(values);
}
function hwb2oklabToken(token) {
const values = hwb2oklabvalues(token);
if (values == null) {
return null;
}
return oklabToken(values);
}
function cmyk2oklabToken(token) {
const values = cmyk2oklabvalues(token);
if (values == null) {
return null;
}
return oklabToken(values);
}
function lab2oklabToken(token) {
const values = lab2oklabvalues(token);
if (values == null) {
return null;
}
return oklabToken(values);
}
function lch2oklabToken(token) {
const values = lch2oklabvalues(token);
if (values == null) {
return null;
}
return oklabToken(values);
}
function oklch2oklabToken(token) {
const values = oklch2oklabvalues(token);
if (values == null) {
return null;
}
return oklabToken(values);
}
function color2oklabToken(token) {
const values = color2oklabvalues(token);
if (values == null) {
return null;
}
return oklabToken(values);
}
function oklabToken(values) {
const chi = [
{ typ: exports.EnumToken.NumberTokenType, val: String(values[0]) },
{ typ: exports.EnumToken.NumberTokenType, val: String(values[1]) },
{ typ: exports.EnumToken.NumberTokenType, val: String(values[2]) },
];
if (values.length == 4) {
chi.push({ typ: exports.EnumToken.LiteralTokenType, val: '/' }, {
typ: exports.EnumToken.PercentageTokenType,
val: (values[3] * 100).toFixed()
});
}
return {
typ: exports.EnumToken.ColorTokenType,
val: 'oklab',
chi,
kin: exports.ColorType.OKLAB
};
}
function hex2oklabvalues(token) {
const values = hex2srgbvalues(token);
if (values == null) {
return null;
}
// @ts-ignore
return srgb2oklab(...values);
}
function rgb2oklabvalues(token) {
const values = rgb2srgb(token);
if (values == null) {
return null;
}
// @ts-ignore
return srgb2oklab(...values);
}
function hsl2oklabvalues(token) {
const values = hsl2srgb(token);
if (values == null) {
return null;
}
// @ts-ignore
return srgb2oklab(...values);
}
function hwb2oklabvalues(token) {
// @ts-ignore
return srgb2oklab(...hwb2srgbvalues(token));
}
function cmyk2oklabvalues(token) {
const values = cmyk2srgbvalues(token);
// @ts-ignore
return values == null ? null : srgb2oklab(...values);
}
function lab2oklabvalues(token) {
const values = lab2srgbvalues(token);
if (values == null) {
return null;
}
// @ts-ignore
return srgb2oklab(...values);
}
function lch2oklabvalues(token) {
const values = lch2srgbvalues(token);
// @ts-ignore
return values == null ? null : srgb2oklab(...values);
}
function oklch2oklabvalues(token) {
const values = getOKLCHComponents(token);
// @ts-ignore
return values == null ? null : lchvalues2labvalues(...values);
}
function color2oklabvalues(token) {
const values = color2srgbvalues(token);
// @ts-ignore
return values == null ? null : srgb2oklab(...values);
}
function srgb2oklab(r, g, blue, alpha) {
[r, g, blue] = srgb2lsrgbvalues(r, g, blue);
let L = Math.cbrt(0.41222147079999993 * r + 0.5363325363 * g + 0.0514459929 * blue);
let M = Math.cbrt(0.2119034981999999 * r + 0.6806995450999999 * g + 0.1073969566 * blue);
let S = Math.cbrt(0.08830246189999998 * r + 0.2817188376 * g + 0.6299787005000002 * blue);
const l = 0.2104542553 * L + 0.793617785 * M - 0.0040720468 * S;
const a = r == g && g == blue ? 0 : 1.9779984951 * L - 2.428592205 * M + 0.4505937099 * S;
const b = r == g && g == blue ? 0 : 0.0259040371 * L + 0.7827717662 * M - 0.808675766 * S;
return alpha == null || alpha == 1 ? [l, a, b] : [l, a, b, alpha];
}
function getOKLABComponents(token) {
const components = getComponents(token);
if (components == null || components.length < 3) {
return null;
}
// @ts-ignore
let t = components[0];
// @ts-ignore
const l = getNumber(t);
// @ts-ignore
t = components[1];
// @ts-ignore
const a = getNumber(t) * (t.typ == exports.EnumToken.PercentageTokenType ? .4 : 1);
// @ts-ignore
t = components[2];
// @ts-ignore
const b = getNumber(t) * (t.typ == exports.EnumToken.PercentageTokenType ? .4 : 1);
// @ts-ignore
let alpha = null;
if (components.length > 3) {
alpha = getNumber(components[3]);
}
const rgb = [l, a, b];
if (alpha != 1 && alpha != null) {
rgb.push(alpha);
}
return rgb;
}
function OKLab_to_XYZ(l, a, b, alpha = null) {
// Given OKLab, convert to XYZ relative to D65
const LMStoXYZ = [
[1.2268798758459243, -0.5578149944602171, 0.2813910456659647],
[-0.0405757452148008, 1.1122868032803170, -0.0717110580655164],
[-0.0763729366746601, -0.4214933324022432, 1.5869240198367816]
];
const OKLabtoLMS = [
[1.0000000000000000, 0.3963377773761749, 0.2158037573099136],
[1.0000000000000000, -0.1055613458156586, -0.0638541728258133],
[1.0000000000000000, -0.0894841775298119, -1.2914855480194092]
];
const LMSnl = multiplyMatrices(OKLabtoLMS, [l, a, b]);
const xyz = multiplyMatrices(LMStoXYZ, LMSnl.map((c) => c ** 3));
if (alpha != null) {
xyz.push(alpha);
}
return xyz;
}
// from https://www.w3.org/TR/css-color-4/#color-conversion-code
function OKLab_to_sRGB(l, a, b) {
let L = Math.pow(l * 0.99999999845051981432 +
0.39633779217376785678 * a +
0.21580375806075880339 * b, 3);
let M = Math.pow(l * 1.0000000088817607767 -
0.1055613423236563494 * a -
0.063854174771705903402 * b, 3);
let S = Math.pow(l * 1.0000000546724109177 -
0.089484182094965759684 * a -
1.2914855378640917399 * b, 3);
return lsrgb2srgbvalues(
/* r: */
4.076741661347994 * L -
3.307711590408193 * M +
0.230969928729428 * S,
/* g: */
-1.2684380040921763 * L +
2.6097574006633715 * M -
0.3413193963102197 * S,
/* b: */
-0.004196086541837188 * L -
0.7034186144594493 * M +
1.7076147009309444 * S);
}
function hex2labToken(token) {
const values = hex2labvalues(token);
return values == null ? null : labToken(values);
}
function rgb2labToken(token) {
const values = rgb2labvalues(token);
if (values == null) {
return null;
}
return labToken(values);
}
function hsl2labToken(token) {
const values = hsl2labvalues(token);
if (values == null) {
return null;
}
return labToken(values);
}
function hwb2labToken(token) {
const values = hwb2labvalues(token);
if (values == null) {
return null;
}
return labToken(values);
}
function cmyk2labToken(token) {
const values = cmyk2labvalues(token);
if (values == null) {
return null;
}
return labToken(values);
}
function lch2labToken(token) {
const values = lch2labvalues(token);
if (values == null) {
return null;
}
return labToken(values);
}
function oklab2labToken(token) {
const values = oklab2labvalues(token);
if (values == null) {
return null;
}
return labToken(values);
}
function oklch2labToken(token) {
const values = oklch2labvalues(token);
if (values == null) {
return null;
}
return labToken(values);
}
function color2labToken(token) {
const values = color2labvalues(token);
if (values == null) {
return null;
}
return labToken(values);
}
function labToken(values) {
const chi = [
{ typ: exports.EnumToken.NumberTokenType, val: String(values[0]) },
{ typ: exports.EnumToken.NumberTokenType, val: String(values[1]) },
{ typ: exports.EnumToken.NumberTokenType, val: String(values[2]) },
];
if (values.length == 4) {
chi.push({ typ: exports.EnumToken.LiteralTokenType, val: '/' }, {
typ: exports.EnumToken.PercentageTokenType,
val: (values[3] * 100).toFixed()
});
}
return {
typ: exports.EnumToken.ColorTokenType,
val: 'lab',
chi,
kin: exports.ColorType.LAB
};
}
// L: 0% = 0.0, 100% = 100.0
// for a and b: -100% = -125, 100% = 125
function hex2labvalues(token) {
const values = hex2srgbvalues(token);
// @ts-ignore
return values == null ? null : srgb2labvalues(...values);
}
function rgb2labvalues(token) {
const values = rgb2srgb(token);
// @ts-ignore
return values == null ? null : srgb2labvalues(...values);
}
function cmyk2labvalues(token) {
const values = cmyk2srgbvalues(token);
// @ts-ignore
return values == null ? null : srgb2labvalues(...values);
}
function hsl2labvalues(token) {
const values = hsl2srgb(token);
if (values == null) {
return null;
}
// @ts-ignore
return srgb2labvalues(...values);
}
function hwb2labvalues(token) {
const values = hwb2srgbvalues(token);
if (values == null) {
return null;
}
// @ts-ignore
return srgb2labvalues(...values);
}
function lch2labvalues(token) {
const values = getLCHComponents(token);
// @ts-ignore
return values == null ? null : lchvalues2labvalues(...values);
}
function oklab2labvalues(token) {
const values = getOKLABComponents(token);
if (values == null) {
return null;
}
// @ts-ignore
return xyz2lab(...XYZ_D65_to_D50(...OKLab_to_XYZ(...values)));
}
function oklch2labvalues(token) {
const values = oklch2srgbvalues(token);
if (values == null) {
return null;
}
// @ts-ignore
return srgb2labvalues(...values);
}
function color2labvalues(token) {
const val = color2srgbvalues(token);
if (val == null) {
return null;
}
// @ts-ignore
return srgb2labvalues(...val);
}
function srgb2labvalues(r, g, b, a) {
// @ts-ignore */
const result = xyz2lab(...srgb2xyz_d50(r, g, b));
// Fixes achromatic RGB colors having a _slight_ chroma due to floating-point errors
// and approximated computations in sRGB <-> CIELab.
// See: https://github.com/d3/d3-color/pull/46
if (r === b && b === g) {
result[1] = result[2] = 0;
}
if (a != null) {
result.push(a);
}
return result;
}
function xyz2lab(x, y, z, a = null) {
// Assuming XYZ is relative to D50, convert to CIE Lab
// from CIE standard, which now defines these as a rational fraction
// var e = 216/24389; // 6^3/29^3
// var k = 24389/27; // 29^3/3^3
// compute xyz, which is XYZ scaled relative to reference white
const xyz = [x, y, z].map((value, i) => value / D50[i]);
// now compute f
const f = xyz.map((value) => value > e ? Math.cbrt(value) : (k * value + 16) / 116);
const result = [
(116 * f[1]) - 16, // L
500 * (f[0] - f[1]), // a
200 * (f[1] - f[2]) // b
];
// L in range [0,100]. For use in CSS, add a percent
if (a != null && a != 1) {
result.push(a);
}
return result;
}
function lchvalues2labvalues(l, c, h, a = null) {
// l, c * Math.cos(360 * h * Math.PI / 180), c * Math.sin(360 * h * Math.PI / 180
const result = [l, c * Math.cos(h * Math.PI / 180), c * Math.sin(h * Math.PI / 180)];
if (a != null) {
result.push(a);
}
return result;
}
function getLABComponents(token) {
const components = getComponents(token);
if (components == null) {
return null;
}
for (let i = 0; i < components.length; i++) {
if (![exports.EnumToken.NumberTokenType, exports.EnumToken.PercentageTokenType, exports.EnumToken.AngleTokenType, exports.EnumToken.IdenTokenType].includes(components[i].typ)) {
return null;
}
}
// @ts-ignore
let t = components[0];
// @ts-ignore
const l = getNumber(t) * (t.typ == exports.EnumToken.PercentageTokenType ? 100 : 1);
// @ts-ignore
t = components[1];
// @ts-ignore
const a = getNumber(t) * (t.typ == exports.EnumToken.PercentageTokenType ? 125 : 1);
// @ts-ignore
t = components[2];
// @ts-ignore
const b = getNumber(t) * (t.typ == exports.EnumToken.PercentageTokenType ? 125 : 1);
// @ts-ignore
t = components[3];
// @ts-ignore
const alpha = t == null ? 1 : getNumber(t);
const result = [l, a, b];
if (alpha != null && alpha != 1) {
result.push(alpha);
}
return result;
}
// from https://www.w3.org/TR/css-color-4/#color-conversion-code
// D50 LAB
function Lab_to_sRGB(l, a, b) {
const xyz_d50 = Lab_to_XYZ(l, a, b);
// @ts-ignore
const xyz_d65 = XYZ_D50_to_D65(...xyz_d50);
// @ts-ignore
return xyz2srgb(...xyz_d65);
}
// from https://www.w3.org/TR/css-color-4/#color-conversion-code
function Lab_to_XYZ(l, a, b) {
// Convert Lab to D50-adapted XYZ
// http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
const k = 24389 / 27; // 29^3/3^3
const e = 216 / 24389; // 6^3/29^3
const f = [];
// compute f, starting with the luminance-related term
f[1] = (l + 16) / 116;
f[0] = a / 500 + f[1];
f[2] = f[1] - b / 200;
// compute xyz
const xyz = [
Math.pow(f[0], 3) > e ? Math.pow(f[0], 3) : (116 * f[0] - 16) / k,
l > k * e ? Math.pow((l + 16) / 116, 3) : l / k,
Math.pow(f[2], 3) > e ? Math.pow(f[2], 3) : (116 * f[2] - 16) / k
];
// Compute XYZ by scaling xyz by reference white
return xyz.map((value, i) => value * D50[i]);
}
// from https://www.w3.org/TR/css-color-4/#color-conversion-code
// srgb-linear -> srgb
// 0 <= r, g, b <= 1
function srgbvalues(token) {
switch (token.kin) {
case exports.ColorType.LIT:
case exports.ColorType.HEX:
return hex2srgbvalues(token);
case exports.ColorType.RGB:
case exports.ColorType.RGBA:
return rgb2srgb(token);
case exports.ColorType.HSL:
case exports.ColorType.HSLA:
return hsl2srgb(token);
case exports.ColorType.HWB:
return hwb2srgbvalues(token);
case exports.ColorType.LAB:
return lab2srgbvalues(token);
case exports.ColorType.LCH:
return lch2srgbvalues(token);
case exports.ColorType.OKLAB:
return oklab2srgbvalues(token);
case exports.ColorType.OKLCH:
return oklch2srgbvalues(token);
case exports.ColorType.COLOR:
return color2srgbvalues(token);
}
return null;
}
function rgb2srgb(token) {
return getComponents(token)?.map?.((t, index) => index == 3 ? ((t.typ == exports.EnumToken.IdenTokenType && t.val == 'none') ? 1 : getNumber(t)) : (t.typ == exports.EnumToken.PercentageTokenType ? 255 : 1) * getNumber(t) / 255) ?? null;
}
function rgb2srgbvalues(token) {
return getComponents(token)?.map?.((t, index) => index == 3 ? getNumber(t) : getNumber(t) / 255) ?? null;
}
function hex2srgbvalues(token) {
const value = expandHexValue(token.kin == exports.ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
const rgb = [];
for (let i = 1; i < value.length; i += 2) {
rgb.push(parseInt(value.slice(i, i + 2), 16) / 255);
}
if (rgb.length == 4) {
rgb[3] = +rgb[3].toFixed(2);
}
return rgb;
}
// xyz d65 input
function xyz2srgb(x, y, z, alpha = null) {
// @ts-ignore
return lsrgb2srgbvalues(...XYZ_to_lin_sRGB(x, y, z, alpha));
}
function hwb2srgbvalues(token) {
const { h: hue, s: white, l: black, a: alpha } = hslvalues(token) ?? {};
if (hue == null || white == null || black == null) {
return [];
}
const rgb = hslvalues2srgbvalues(hue, 1, .5);
for (let i = 0; i < 3; i++) {
rgb[i] *= (1 - white - black);
rgb[i] = rgb[i] + white;
}
if (alpha != null && alpha != 1) {
rgb.push(alpha);
}
return rgb;
}
function hsl2srgb(token) {
let { h, s, l, a } = hslvalues(token) ?? {};
if (h == null || s == null || l == null) {
return null;
}
return hslvalues2srgbvalues(h, s, l, a);
}
function cmyk2srgbvalues(token) {
const components = getComponents(token);
if (components == null) {
return null;
}
// @ts-ignore
let t = components[0];
// @ts-ignore
const c = getNumber(t);
// @ts-ignore
t = components[1];
// @ts-ignore
const m = getNumber(t);
// @ts-ignore
t = components[2];
// @ts-ignore
const y = getNumber(t);
// @ts-ignore
t = components[3];
// @ts-ignore
const k = getNumber(t);
const rgb = [
1 - Math.min(1, c * (1 - k) + k),
1 - Math.min(1, m * (1 - k) + k),
1 - Math.min(1, y * (1 - k) + k)
];
if (components.length == 5) {
rgb.push(getNumber(components[4]));
}
// @ts-ignore
// if (token.chi.length >= 9) {
//
// // @ts-ignore
// t = <NumberToken | PercentageToken>token.chi[8];
//
// // @ts-ignore
// rgb.push(getNumber(t));
// }
return rgb;
}
function oklab2srgbvalues(token) {
const [l, a, b, alpha] = getOKLABComponents(token) ?? [];
if (l == null || a == null || b == null) {
return null;
}
const rgb = OKLab_to_sRGB(l, a, b);
if (alpha != null && alpha != 1) {
rgb.push(alpha);
}
return rgb;
}
function oklch2srgbvalues(token) {
const [l, c, h, alpha] = getOKLCHComponents(token) ?? [];
if (l == null || c == null || h == null) {
return null;
}
// @ts-ignore
const rgb = OKLab_to_sRGB(...lchvalues2labvalues(l, c, h));
if (alpha != 1) {
rgb.push(alpha);
}
return rgb;
}
function hslvalues(token) {
const compo