Skip to content

Commit e4dd28b

Browse files
jgerigmeyerardov
authored andcommitted
Parse both dashed-ident and ident versions, and add missing dashed-idents to color spaces. (color-js#439)
* Parse both dashed-ident and ident versions, and add missing dashed-idents to color spaces. * Warn when using incorrect dashed-ident/ident * refactor * Update warnings * Add trailing commas and make warning more descriptive. * Remove dashed-ident prefix for HDR colors.
1 parent 6421c58 commit e4dd28b

21 files changed

+122
-68
lines changed

src/parse.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ export default function parse (str, {meta} = {}) {
7878
if (name === "color") {
7979
// color() function
8080
let id = env.parsed.args.shift();
81-
let undashedId = id.startsWith("--") ? id.substring(2) : id;
82-
let ids = [id, undashedId];
81+
// Check against both <dashed-ident> and <ident> versions
82+
let alternateId = id.startsWith("--") ? id.substring(2) : `--${id}`;
83+
let ids = [id, alternateId];
8384
let alpha = env.parsed.rawArgs.indexOf("/") > 0 ? env.parsed.args.pop() : 1;
8485

8586
for (let space of ColorSpace.all) {
@@ -102,17 +103,26 @@ export default function parse (str, {meta} = {}) {
102103
Object.assign(meta, {formatId: "color", types});
103104
}
104105

106+
if (colorSpec.id.startsWith("--") && !id.startsWith("--")) {
107+
console.warn(`${space.name} is a non-standard space and not currently supported in the CSS spec. ` +
108+
`Use prefixed color(${colorSpec.id}) instead of color(${id}).`);
109+
}
110+
if (id.startsWith("--") && !colorSpec.id.startsWith("--")) {
111+
console.warn(`${space.name} is a standard space and supported in the CSS spec. ` +
112+
`Use color(${colorSpec.id}) instead of prefixed color(${id}).`);
113+
}
114+
105115
return {spaceId: space.id, coords, alpha};
106116
}
107117
}
108118
}
109119

110120
// Not found
111121
let didYouMean = "";
112-
let registryId = id in ColorSpace.registry ? id : undashedId;
122+
let registryId = id in ColorSpace.registry ? id : alternateId;
113123
if (registryId in ColorSpace.registry) {
114124
// Used color space id instead of color() id, these are often different
115-
let cssId = ColorSpace.registry[registryId].formats?.functions?.color?.id;
125+
let cssId = ColorSpace.registry[registryId].formats?.color?.id;
116126

117127
if (cssId) {
118128
didYouMean = `Did you mean color(${cssId})?`;

src/space.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ export default class ColorSpace {
4545
format.name ||= name;
4646
}
4747

48-
if (options.cssId && !this.formats.functions?.color) {
49-
this.formats.color = { id: options.cssId };
50-
Object.defineProperty(this, "cssId", {value: options.cssId});
51-
}
52-
else if (this.formats?.color && !this.formats?.color.id) {
53-
this.formats.color.id = this.id;
48+
if (!this.formats.color?.id) {
49+
this.formats.color = {
50+
...this.formats.color ?? {},
51+
id: options.cssId || this.id,
52+
};
5453
}
5554

5655
// Gamut space
@@ -122,7 +121,7 @@ export default class ColorSpace {
122121
}
123122

124123
get cssId () {
125-
return this.formats.functions?.color?.id || this.id;
124+
return this.formats?.color?.id || this.id;
126125
}
127126

128127
get isPolar () {

src/spaces/a98rgb-linear.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const fromXYZ_M = [
2020

2121
export default new RGBColorSpace({
2222
id: "a98rgb-linear",
23+
cssId: "--a98-rgb-linear",
2324
name: "Linear Adobe® 98 RGB compatible",
2425
white: "D65",
2526
toXYZ_M,

src/spaces/a98rgb.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ import A98Linear from "./a98rgb-linear.js";
33

44
export default new RGBColorSpace({
55
id: "a98rgb",
6+
cssId: "a98-rgb",
67
name: "Adobe® 98 RGB compatible",
78
base: A98Linear,
89
toBase: RGB => RGB.map(val => Math.pow(Math.abs(val), 563 / 256) * Math.sign(val)),
910
fromBase: RGB => RGB.map(val => Math.pow(Math.abs(val), 256 / 563) * Math.sign(val)),
10-
formats: {
11-
color: {
12-
id: "a98-rgb",
13-
},
14-
},
1511
});

src/spaces/acescc.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const ACES_cc_max = (Math.log2(65504) + 9.72) / 17.52; // 1.468
1313

1414
export default new RGBColorSpace({
1515
id: "acescc",
16+
cssId: "--acescc",
1617
name: "ACEScc",
1718
// see S-2014-003 ACEScc – A Logarithmic Encoding of ACES Data
1819
// https://docs.acescentral.com/specifications/acescc/
@@ -72,7 +73,4 @@ export default new RGBColorSpace({
7273
},
7374
// encoded media white (rgb 1,1,1) => linear [ 222.861, 222.861, 222.861 ]
7475
// encoded media black (rgb 0,0,0) => linear [ 0.0011857, 0.0011857, 0.0011857]
75-
formats: {
76-
color: {},
77-
},
7876
});

src/spaces/acescg.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const fromXYZ_M = [
2222

2323
export default new RGBColorSpace({
2424
id: "acescg",
25+
cssId: "--acescg",
2526
name: "ACEScg",
2627

2728
// ACEScg – A scene-referred, linear-light encoding of ACES Data
@@ -48,10 +49,6 @@ export default new RGBColorSpace({
4849

4950
toXYZ_M,
5051
fromXYZ_M,
51-
52-
formats: {
53-
color: {},
54-
},
5552
});
5653

5754
// export default Color;

src/spaces/cam16.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ export function toCam16 (xyzd65, env) {
329329
// Results compared against: https://github.com/colour-science/colour
330330
export default new ColorSpace({
331331
id: "cam16-jmh",
332+
cssId: "--cam16-jmh",
332333
name: "CAM16-JMh",
333334
coords: {
334335
j: {
@@ -358,9 +359,4 @@ export default new ColorSpace({
358359
viewingConditions,
359360
);
360361
},
361-
formats: {
362-
color: {
363-
id: "--cam16-jmh",
364-
},
365-
},
366362
});

src/spaces/ictcp.js

-3
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ export default new ColorSpace({
103103

104104
return multiplyMatrices(LMStoXYZ_M, LMS);
105105
},
106-
formats: {
107-
color: {},
108-
},
109106
});
110107

111108
function LMStoICtCp (LMS) {

src/spaces/jzczhz.js

-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,4 @@ export default new ColorSpace({
5050
jzczhz[1] * Math.sin(jzczhz[2] * Math.PI / 180), // bz
5151
];
5252
},
53-
formats: {
54-
color: {},
55-
},
5653
});

src/spaces/p3-linear.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const fromXYZ_M = [
1414

1515
export default new RGBColorSpace({
1616
id: "p3-linear",
17+
cssId: "--display-p3-linear",
1718
name: "Linear P3",
1819
white: "D65",
1920
toXYZ_M,

src/spaces/p3.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ import sRGB from "./srgb.js";
44

55
export default new RGBColorSpace({
66
id: "p3",
7+
cssId: "display-p3",
78
name: "P3",
89
base: P3Linear,
910
// Gamma encoding/decoding is the same as sRGB
1011
fromBase: sRGB.fromBase,
1112
toBase: sRGB.toBase,
12-
formats: {
13-
color: {
14-
id: "display-p3",
15-
},
16-
},
1713
});

src/spaces/prophoto-linear.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const fromXYZ_M = [
1919

2020
export default new RGBColorSpace({
2121
id: "prophoto-linear",
22+
cssId: "--prophoto-rgb-linear",
2223
name: "Linear ProPhoto",
2324
white: "D50",
2425
base: XYZ_D50,

src/spaces/prophoto.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Et2 = 16 / 512;
66

77
export default new RGBColorSpace({
88
id: "prophoto",
9+
cssId: "prophoto-rgb",
910
name: "ProPhoto",
1011
base: ProPhotoLinear,
1112
toBase (RGB) {
@@ -15,9 +16,4 @@ export default new RGBColorSpace({
1516
fromBase (RGB) {
1617
return RGB.map(v => v >= Et ? v ** (1 / 1.8) : 16 * v);
1718
},
18-
formats: {
19-
color: {
20-
id: "prophoto-rgb",
21-
},
22-
},
2319
});

src/spaces/rec2020-linear.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ const fromXYZ_M = [
1919

2020
export default new RGBColorSpace({
2121
id: "rec2020-linear",
22+
cssId: "--rec2020-linear",
2223
name: "Linear REC.2020",
2324
white: "D65",
2425
toXYZ_M,
2526
fromXYZ_M,
26-
formats: {
27-
color: {},
28-
},
2927
});

src/spaces/rec2020.js

-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,4 @@ export default new RGBColorSpace({
2828
return 4.5 * val;
2929
});
3030
},
31-
formats: {
32-
color: {},
33-
},
3431
});

src/spaces/rec2100-hlg.js

-5
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,4 @@ export default new RGBColorSpace({
4646
return a * Math.log(12 * val - b) + c;
4747
});
4848
},
49-
formats: {
50-
color: {
51-
id: "rec2100-hlg",
52-
},
53-
},
5449
});

src/spaces/rec2100-pq.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const c3 = 2392 / (2 ** 7);
1212

1313
export default new RGBColorSpace({
1414
id: "rec2100pq",
15+
cssId: "rec2100-pq",
1516
name: "REC.2100-PQ",
1617
base: REC2020Linear,
1718
toBase (RGB) {
@@ -33,9 +34,4 @@ export default new RGBColorSpace({
3334
return ((num / denom) ** m);
3435
});
3536
},
36-
formats: {
37-
color: {
38-
id: "rec2100-pq",
39-
},
40-
},
4137
});

src/spaces/srgb-linear.js

-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,4 @@ export default new RGBColorSpace({
2727
white: "D65",
2828
toXYZ_M,
2929
fromXYZ_M,
30-
formats: {
31-
color: {},
32-
},
3330
});

src/spaces/xyz-abs-d65.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default new ColorSpace({
99
// SDR spaces are converted per BT.2048
1010
// so that diffuse, media white is 203 cd/m²
1111
id: "xyz-abs-d65",
12+
cssId: "--xyz-abs-d65",
1213
name: "Absolute XYZ D65",
1314
coords: {
1415
x: {

src/spaces/xyz-d50.js

-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,4 @@ export default new ColorSpace({
99
base: XYZ_D65,
1010
fromBase: coords => adapt(XYZ_D65.white, "D50", coords),
1111
toBase: coords => adapt("D50", XYZ_D65.white, coords),
12-
formats: {
13-
color: {},
14-
},
1512
});

0 commit comments

Comments
 (0)