Skip to content

Commit d8b16a0

Browse files
committed
compute color distance #94
1 parent affcb4f commit d8b16a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3447
-1932
lines changed

dist/index-umd-web.js

Lines changed: 269 additions & 323 deletions
Large diffs are not rendered by default.

dist/index.cjs

Lines changed: 269 additions & 323 deletions
Large diffs are not rendered by default.

dist/index.d.ts

Lines changed: 33 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,3 @@
1-
/**
2-
* color types enum
3-
*/
4-
declare enum ColorType {
5-
/**
6-
* convert color to hex
7-
*/
8-
HEX = 0,
9-
/**
10-
* convert color to rgb
11-
*/
12-
RGBA = 1,
13-
/**
14-
* convert color to hwb
15-
*/
16-
HWB = 2,
17-
/**
18-
* convert color to hsl
19-
*/
20-
HSLA = 3,
21-
/**
22-
* convert color to cmyk
23-
*/
24-
DEVICE_CMYK = 4,
25-
/**
26-
* convert color to lch
27-
*/
28-
LCH = 5,
29-
/**
30-
* convert color to oklch
31-
*/
32-
OKLCH = 6,
33-
/**
34-
* convert color to lab
35-
*/
36-
LAB = 7,
37-
/**
38-
* convert color to oklab
39-
*/
40-
OKLAB = 8,
41-
/**
42-
* convert color to xyz
43-
*/
44-
XYZ_D65 = 9,
45-
/**
46-
* convert color to xyz-d50
47-
*/
48-
XYZ_D50 = 10,
49-
/**
50-
* convert color to srgb
51-
*/
52-
SRGB = 11,
53-
/**
54-
* convert color to srgb-linear
55-
*/
56-
SRGB_LINEAR = 12,
57-
/**
58-
* convert color to display-p3
59-
*/
60-
DISPLAY_P3 = 13,
61-
/**
62-
* convert color to a98-rgb
63-
*/
64-
A98_RGB = 14,
65-
/**
66-
* convert color to prophoto-rgb
67-
*/
68-
PROPHOTO_RGB = 15,
69-
/**
70-
* convert color to rec2020
71-
*/
72-
REC2020 = 16,
73-
/**
74-
* alias for hsl
75-
*/
76-
HSL = 3,
77-
/**
78-
* alias for rgba
79-
*/
80-
RGB = 1,
81-
/**
82-
* alias for xyz-d65
83-
*/
84-
XYZ = 9
85-
}
861
/**
872
* validation level enum
883
*/
@@ -227,6 +142,35 @@ declare enum EnumToken {
227142
TimingFunction = 17,
228143
TimelineFunction = 16
229144
}
145+
declare enum ColorType {
146+
SYS = 0,
147+
DPSYS = 1,
148+
LIT = 2,
149+
HEX = 3,
150+
RGBA = 4,
151+
HSLA = 5,
152+
HWB = 6,
153+
CMYK = 7,
154+
OKLAB = 8,
155+
OKLCH = 9,
156+
LAB = 10,
157+
LCH = 11,
158+
COLOR = 12,
159+
SRGB = 13,
160+
PROPHOTO_RGB = 14,
161+
A98_RGB = 15,
162+
REC2020 = 16,
163+
DISPLAY_P3 = 17,
164+
SRGB_LINEAR = 18,
165+
XYZ_D50 = 19,
166+
XYZ_D65 = 20,
167+
LIGHT_DARK = 21,
168+
COLOR_MIX = 22,
169+
RGB = 4,
170+
HSL = 5,
171+
XYZ = 20,
172+
DEVICE_CMYK = 7
173+
}
230174

231175
/**
232176
* minify ast
@@ -288,34 +232,8 @@ declare function renderToken(token: Token, options?: RenderOptions, cache?: {
288232
[key: string]: any;
289233
}, reducer?: (acc: string, curr: Token) => string, errors?: ErrorDescription[]): string;
290234

291-
declare enum ColorKind {
292-
SYS = 0,
293-
DPSYS = 1,
294-
LIT = 2,
295-
HEX = 3,
296-
RGBA = 4,
297-
HSLA = 5,
298-
HWB = 6,
299-
DEVICE_CMYK = 7,
300-
OKLAB = 8,
301-
OKLCH = 9,
302-
LAB = 10,
303-
LCH = 11,
304-
COLOR = 12,
305-
SRGB = 13,
306-
PROPHOTO_RGB = 14,
307-
A98_RGB = 15,
308-
REC2020 = 16,
309-
DISPLAY_P3 = 17,
310-
SRGB_LINEAR = 18,
311-
XYZ_D50 = 19,
312-
XYZ_D65 = 20,
313-
LIGHT_DARK = 21,
314-
COLOR_MIX = 22,
315-
RGB = 4,
316-
HSL = 5,
317-
XYZ = 20
318-
}
235+
declare function okLabDistance(okLab1: [number, number, number], okLab2: [number, number, number]): number;
236+
declare function isOkLabClose(color1: ColorToken, color2: ColorToken, threshold?: number): boolean;
319237

320238
/**
321239
* parse css string
@@ -710,7 +628,7 @@ export declare interface ColorToken extends BaseToken {
710628

711629
typ: EnumToken.ColorTokenType;
712630
val: string;
713-
kin: ColorKind;
631+
kin: ColorType;
714632
chi?: Token[];
715633
/* calculated */
716634
cal?: 'rel' | 'mix';
@@ -1441,4 +1359,4 @@ declare function parse(iterator: string, opt?: ParserOptions): Promise<ParseResu
14411359
*/
14421360
declare function transform(css: string, options?: TransformOptions): Promise<TransformResult>;
14431361

1444-
export { ColorType, EnumToken, ValidationLevel, dirname, expand, load, minify, parse, parseString, parseTokens, render, renderToken, resolve, transform, walk, walkValues };
1362+
export { ColorType, EnumToken, ValidationLevel, dirname, expand, isOkLabClose, load, minify, okLabDistance, parse, parseString, parseTokens, render, renderToken, resolve, transform, walk, walkValues };

dist/lib/ast/types.js

Lines changed: 31 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,92 +4,6 @@ var SyntaxValidationResult;
44
SyntaxValidationResult[SyntaxValidationResult["Drop"] = 1] = "Drop";
55
SyntaxValidationResult[SyntaxValidationResult["Lenient"] = 2] = "Lenient"; /* preserve unknown at-rules, declarations and pseudo-classes */
66
})(SyntaxValidationResult || (SyntaxValidationResult = {}));
7-
/**
8-
* color types enum
9-
*/
10-
var ColorType;
11-
(function (ColorType) {
12-
/**
13-
* convert color to hex
14-
*/
15-
ColorType[ColorType["HEX"] = 0] = "HEX";
16-
/**
17-
* convert color to rgb
18-
*/
19-
ColorType[ColorType["RGBA"] = 1] = "RGBA";
20-
/**
21-
* convert color to hwb
22-
*/
23-
ColorType[ColorType["HWB"] = 2] = "HWB";
24-
/**
25-
* convert color to hsl
26-
*/
27-
ColorType[ColorType["HSLA"] = 3] = "HSLA";
28-
/**
29-
* convert color to cmyk
30-
*/
31-
ColorType[ColorType["DEVICE_CMYK"] = 4] = "DEVICE_CMYK";
32-
/**
33-
* convert color to lch
34-
*/
35-
ColorType[ColorType["LCH"] = 5] = "LCH";
36-
/**
37-
* convert color to oklch
38-
*/
39-
ColorType[ColorType["OKLCH"] = 6] = "OKLCH";
40-
/**
41-
* convert color to lab
42-
*/
43-
ColorType[ColorType["LAB"] = 7] = "LAB";
44-
/**
45-
* convert color to oklab
46-
*/
47-
ColorType[ColorType["OKLAB"] = 8] = "OKLAB";
48-
/**
49-
* convert color to xyz
50-
*/
51-
ColorType[ColorType["XYZ_D65"] = 9] = "XYZ_D65";
52-
/**
53-
* convert color to xyz-d50
54-
*/
55-
ColorType[ColorType["XYZ_D50"] = 10] = "XYZ_D50";
56-
/**
57-
* convert color to srgb
58-
*/
59-
ColorType[ColorType["SRGB"] = 11] = "SRGB";
60-
/**
61-
* convert color to srgb-linear
62-
*/
63-
ColorType[ColorType["SRGB_LINEAR"] = 12] = "SRGB_LINEAR";
64-
/**
65-
* convert color to display-p3
66-
*/
67-
ColorType[ColorType["DISPLAY_P3"] = 13] = "DISPLAY_P3";
68-
/**
69-
* convert color to a98-rgb
70-
*/
71-
ColorType[ColorType["A98_RGB"] = 14] = "A98_RGB";
72-
/**
73-
* convert color to prophoto-rgb
74-
*/
75-
ColorType[ColorType["PROPHOTO_RGB"] = 15] = "PROPHOTO_RGB";
76-
/**
77-
* convert color to rec2020
78-
*/
79-
ColorType[ColorType["REC2020"] = 16] = "REC2020";
80-
/**
81-
* alias for hsl
82-
*/
83-
ColorType[ColorType["HSL"] = 3] = "HSL";
84-
/**
85-
* alias for rgba
86-
*/
87-
ColorType[ColorType["RGB"] = 1] = "RGB";
88-
/**
89-
* alias for xyz-d65
90-
*/
91-
ColorType[ColorType["XYZ"] = 9] = "XYZ";
92-
})(ColorType || (ColorType = {}));
937
/**
948
* validation level enum
959
*/
@@ -240,5 +154,36 @@ var EnumToken;
240154
EnumToken[EnumToken["TimingFunction"] = 17] = "TimingFunction";
241155
EnumToken[EnumToken["TimelineFunction"] = 16] = "TimelineFunction";
242156
})(EnumToken || (EnumToken = {}));
157+
// xyz-d65 is an alias for xyz
158+
var ColorType;
159+
(function (ColorType) {
160+
ColorType[ColorType["SYS"] = 0] = "SYS";
161+
ColorType[ColorType["DPSYS"] = 1] = "DPSYS";
162+
ColorType[ColorType["LIT"] = 2] = "LIT";
163+
ColorType[ColorType["HEX"] = 3] = "HEX";
164+
ColorType[ColorType["RGBA"] = 4] = "RGBA";
165+
ColorType[ColorType["HSLA"] = 5] = "HSLA";
166+
ColorType[ColorType["HWB"] = 6] = "HWB";
167+
ColorType[ColorType["CMYK"] = 7] = "CMYK";
168+
ColorType[ColorType["OKLAB"] = 8] = "OKLAB";
169+
ColorType[ColorType["OKLCH"] = 9] = "OKLCH";
170+
ColorType[ColorType["LAB"] = 10] = "LAB";
171+
ColorType[ColorType["LCH"] = 11] = "LCH";
172+
ColorType[ColorType["COLOR"] = 12] = "COLOR";
173+
ColorType[ColorType["SRGB"] = 13] = "SRGB";
174+
ColorType[ColorType["PROPHOTO_RGB"] = 14] = "PROPHOTO_RGB";
175+
ColorType[ColorType["A98_RGB"] = 15] = "A98_RGB";
176+
ColorType[ColorType["REC2020"] = 16] = "REC2020";
177+
ColorType[ColorType["DISPLAY_P3"] = 17] = "DISPLAY_P3";
178+
ColorType[ColorType["SRGB_LINEAR"] = 18] = "SRGB_LINEAR";
179+
ColorType[ColorType["XYZ_D50"] = 19] = "XYZ_D50";
180+
ColorType[ColorType["XYZ_D65"] = 20] = "XYZ_D65";
181+
ColorType[ColorType["LIGHT_DARK"] = 21] = "LIGHT_DARK";
182+
ColorType[ColorType["COLOR_MIX"] = 22] = "COLOR_MIX";
183+
ColorType[ColorType["RGB"] = 4] = "RGB";
184+
ColorType[ColorType["HSL"] = 5] = "HSL";
185+
ColorType[ColorType["XYZ"] = 20] = "XYZ";
186+
ColorType[ColorType["DEVICE_CMYK"] = 7] = "DEVICE_CMYK";
187+
})(ColorType || (ColorType = {}));
243188

244189
export { ColorType, EnumToken, SyntaxValidationResult, ValidationLevel };

dist/lib/parser/parse.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { isIdentStart, isIdent, isIdentColor, mathFuncs, isColor, parseColor, isPseudo, pseudoElements, isAtKeyword, isFunction, isNumber, isPercentage, isFlex, isDimension, parseDimension, isHexColor, isHash, mediaTypes } from '../syntax/syntax.js';
2-
import { EnumToken, ValidationLevel, SyntaxValidationResult } from '../ast/types.js';
2+
import { EnumToken, ColorType, ValidationLevel, SyntaxValidationResult } from '../ast/types.js';
33
import { minify, definedPropertySettings, combinators } from '../ast/minify.js';
44
import { walkValues, walk, WalkerOptionEnum } from '../ast/walk.js';
55
import { expand } from '../ast/expand.js';
66
import './utils/config.js';
77
import { parseDeclarationNode } from './utils/declaration.js';
88
import { renderToken } from '../renderer/render.js';
9-
import { funcLike, timingFunc, timelineFunc, COLORS_NAMES, ColorKind, systemColors, deprecatedSystemColors, colorsFunc } from '../syntax/color/utils/constants.js';
9+
import { funcLike, timingFunc, timelineFunc, COLORS_NAMES, systemColors, deprecatedSystemColors, colorsFunc } from '../syntax/color/utils/constants.js';
1010
import { buildExpression } from '../ast/math/expression.js';
1111
import { tokenize } from './tokenize.js';
1212
import '../validation/config.js';
@@ -674,7 +674,7 @@ function parseNode(results, context, options, errors, src, map, rawTokens) {
674674
if (name == null && [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType].includes(tokens[i].typ)) {
675675
name = tokens.slice(0, i + 1);
676676
}
677-
else if (name == null && tokens[i].typ == EnumToken.ColorTokenType && [ColorKind.SYS, ColorKind.DPSYS].includes(tokens[i].kin)) {
677+
else if (name == null && tokens[i].typ == EnumToken.ColorTokenType && [ColorType.SYS, ColorType.DPSYS].includes(tokens[i].kin)) {
678678
name = tokens.slice(0, i + 1);
679679
tokens[i].typ = EnumToken.IdenTokenType;
680680
}
@@ -960,7 +960,7 @@ function parseAtRulePrelude(tokens, atRule) {
960960
const node = value.chi.splice(nameIndex, 1)[0];
961961
// 'background'
962962
// @ts-ignore
963-
if (node.typ == EnumToken.ColorTokenType && node.kin == ColorKind.DPSYS) {
963+
if (node.typ == EnumToken.ColorTokenType && node.kin == ColorType.DPSYS) {
964964
// @ts-ignore
965965
delete node.kin;
966966
node.typ = EnumToken.IdenTokenType;
@@ -1096,8 +1096,8 @@ function parseSelector(tokens) {
10961096
}
10971097
}
10981098
else if (value.typ == EnumToken.ColorTokenType) {
1099-
if (value.kin == ColorKind.LIT || value.kin == ColorKind.HEX || value.kin == ColorKind.SYS || value.kin == ColorKind.DPSYS) {
1100-
if (value.kin == ColorKind.HEX) {
1099+
if (value.kin == ColorType.LIT || value.kin == ColorType.HEX || value.kin == ColorType.SYS || value.kin == ColorType.DPSYS) {
1100+
if (value.kin == ColorType.HEX) {
11011101
if (!isIdent(value.val.slice(1))) {
11021102
continue;
11031103
}
@@ -1276,22 +1276,22 @@ function getTokenType(val, hint) {
12761276
return {
12771277
typ: EnumToken.ColorTokenType,
12781278
val: v,
1279-
kin: ColorKind.LIT
1279+
kin: ColorType.LIT
12801280
};
12811281
}
12821282
if (isIdent(val)) {
12831283
if (systemColors.has(v)) {
12841284
return {
12851285
typ: EnumToken.ColorTokenType,
12861286
val,
1287-
kin: ColorKind.SYS
1287+
kin: ColorType.SYS
12881288
};
12891289
}
12901290
if (deprecatedSystemColors.has(v)) {
12911291
return {
12921292
typ: EnumToken.ColorTokenType,
12931293
val,
1294-
kin: ColorKind.DPSYS
1294+
kin: ColorType.DPSYS
12951295
};
12961296
}
12971297
return {
@@ -1303,7 +1303,7 @@ function getTokenType(val, hint) {
13031303
return {
13041304
typ: EnumToken.ColorTokenType,
13051305
val,
1306-
kin: ColorKind.HEX
1306+
kin: ColorType.HEX
13071307
};
13081308
}
13091309
if (val.charAt(0) == '#' && isHash(val)) {

0 commit comments

Comments
 (0)