Skip to content

Commit 4cc594d

Browse files
committed
Add support for <contrast-color()>
It should serialize as defined in w3c/csswg-drafts#10328, ie. with preserved channel values.
1 parent cdf1164 commit 4cc594d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

__tests__/value.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3285,6 +3285,11 @@ describe('<color>', () => {
32853285
]
32863286
valid.forEach(([input, expected = input]) => expect(parse('<color>', input)).toBe(expected))
32873287
})
3288+
test('valid <contrast-color()>', () => {
3289+
// Preserve channel values except <hue> and <alpha-value>
3290+
expect(parse('<color>', 'contrast-color(rgba(-100% 200% 0 / 101%))')).toBe('contrast-color(rgb(-255 510 0))')
3291+
expect(parse('<color>', 'contrast-color(hsla(540deg -1% 0 / 50%))')).toBe('contrast-color(hsl(180 -1 0 / 0.5))')
3292+
})
32883293
})
32893294
describe('<combinator>', () => {
32903295
test('invalid', () => {

lib/serialize.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,11 @@ function serializeCalculationFunction({ range = {}, round, value }, forComputedO
287287

288288
/**
289289
* @param {object} color
290-
* @param {boolean} mixed
290+
* @param {boolean} specified
291291
* @returns {string}
292292
* @see {@link https://drafts.csswg.org/css-color-4/#serializing-color-values}
293293
*/
294-
function serializeColor({ name, types, value }, mixed = false) {
294+
function serializeColor({ name, types, value }, specified = false) {
295295
if (types.includes('<keyword>')) {
296296
return value
297297
}
@@ -337,6 +337,15 @@ function serializeColor({ name, types, value }, mixed = false) {
337337
if (types.includes('<legacy-device-cmyk-syntax>')) {
338338
return `device-cmyk(${serializeCSSComponentValueList(value, ' ')})`
339339
}
340+
if (types.includes('<contrast-color()>')) {
341+
const [color, max] = value
342+
let string = `contrast-color(${serializeColor(color, true)}`
343+
if (!isOmitted(max)) {
344+
string += ' max'
345+
}
346+
string += ')'
347+
return string
348+
}
340349
let alpha
341350
if (types.includes('<hex-color>')) {
342351
const [n1, n2, n3, n4, n5, n6, n7, n8] = value
@@ -451,7 +460,7 @@ function serializeColor({ name, types, value }, mixed = false) {
451460
break
452461
}
453462
}
454-
if (source || mixed) {
463+
if (source || specified) {
455464
channels.push(value)
456465
return channels
457466
}
@@ -491,7 +500,7 @@ function serializeColor({ name, types, value }, mixed = false) {
491500
return channels
492501
}, [])
493502

494-
if (!resolved || source || mixed) {
503+
if (!resolved || source || specified) {
495504
if (name === 'rgba' || name === 'hsla') {
496505
name = name.slice(0, -1)
497506
}

0 commit comments

Comments
 (0)