Skip to content

Commit 41a66dd

Browse files
Provide DeltaE and contrast algorithms similar to color coords (i.e., method.algorithm)
1 parent 02630b6 commit 41a66dd

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

src/color-swatch/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ You can calculate the difference (delta) and contrast between the current color
161161
To do so, provide the new color via the `vs` attribute and specify one of the [supported algorithms for calculating the difference](https://colorjs.io/docs/color-difference#delta-e-e) ([contrast](https://colorjs.io/docs/contrast) or both) between two colors inside [the `info` attribute](./#the-info-attribute).
162162

163163
```html
164-
<color-swatch info="deltaE2000, WCAG21: WCAG21 contrast" vs="white" size="large">
164+
<color-swatch info="deltaE.2000, WCAG21: contrast.WCAG21" vs="white" size="large">
165165
oklch(70% 0.25 138)
166166
</color-swatch>
167167
```
168168

169169
If color coords are also specified, the deltas on a coord-by-coord basis will be shown:
170170

171171
```html
172-
<color-swatch info="oklch.l, oklch.c, oklch.h, deltaE2000, WCAG21: WCAG21 contrast" vs="white" size="large">
172+
<color-swatch info="oklch.l, oklch.c, oklch.h, deltaE.2000, WCAG21: contrast.WCAG21" vs="white" size="large">
173173
oklch(70% 0.25 138)
174174
</color-swatch>
175175
```

src/color-swatch/color-swatch.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,13 @@ const Self = class ColorSwatch extends ColorElement {
198198
values: {
199199
is: Object,
200200
defaultKey: (value, i) => {
201-
if (value.includes(".")) {
202-
return Self.Color.Space.resolveCoord(value)?.name;
201+
if (value.startsWith("deltaE.") || value.startsWith("contrast.")) {
202+
let [method, algorithm] = value.split(".");
203+
let label = method === "deltaE" ? `ΔE ${algorithm}` : `${algorithm} Contrast`;
204+
return label;
203205
}
204-
else if (value.startsWith("deltaE")) {
205-
let algorithm = value.replace("deltaE", "");
206-
return "ΔE " + algorithm;
206+
else if (value.includes(".")) {
207+
return Self.Color.Space.resolveCoord(value)?.name;
207208
}
208209
else {
209210
return value;
@@ -226,7 +227,7 @@ const Self = class ColorSwatch extends ColorElement {
226227
let ret = [];
227228
for (let data of this.info) {
228229
let [key, value] = Object.entries(data)[0];
229-
if (value.includes(".")) {
230+
if (value.includes(".") && !value.startsWith("deltaE") && !value.startsWith("contrast")) {
230231
ret.push(value);
231232
}
232233
}
@@ -263,7 +264,7 @@ const Self = class ColorSwatch extends ColorElement {
263264
let ret = [];
264265
for (let data of this.info) {
265266
let [key, value] = Object.entries(data)[0];
266-
if (!value.includes(".")) {
267+
if (!this.infoCoords.includes(value)) {
267268
ret.push(value);
268269
}
269270
}
@@ -292,7 +293,7 @@ const Self = class ColorSwatch extends ColorElement {
292293
},
293294
colorDeltas: {
294295
get () {
295-
if (!this.infoCoordsResolved || !this.vsInfo || !this.infoOther?.some(value => value.startsWith("deltaE"))) {
296+
if (!this.infoCoordsResolved || !this.vsInfo) {
296297
return;
297298
}
298299

@@ -343,11 +344,7 @@ const Self = class ColorSwatch extends ColorElement {
343344
}
344345

345346
for (let data of this.infoOther) {
346-
let regexp = /(^(?<deltaE>deltaE)(?<deltaE_algorithm>.+))|((?<contrast_algorithm>.+)\s+(?<contrast>contrast)$)/;
347-
let { deltaE, deltaE_algorithm, contrast, contrast_algorithm } = regexp.exec(data)?.groups ?? {};
348-
349-
let method = deltaE || contrast;
350-
let algorithm = deltaE_algorithm || contrast_algorithm;
347+
let [method, algorithm] = data.split(".");
351348

352349
if (method && algorithm) {
353350
try {

0 commit comments

Comments
 (0)