Skip to content

Commit d959832

Browse files
committed
[css-color-5] add premultiply, unpremultiply to sample code w3c#6833
1 parent e892d1f commit d959832

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

css-color-4/conversions.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,44 @@ function OKLCH_to_OKLab(OKLCH) {
448448
OKLCH[1] * Math.sin(OKLCH[2] * Math.PI / 180) // b
449449
];
450450
}
451+
452+
// Premultiplied alpha conversions
453+
454+
function rectangular_premultiply(color, alpha) {
455+
// given a color in a rectangular orthogonal colorspace
456+
// and an alpha value
457+
// return the premultiplied form
458+
return color.map((c) => c * alpha)
459+
}
460+
461+
function rectangular_un_premultiply(color, alpha) {
462+
// given a premultiplied color in a rectangular orthogonal colorspace
463+
// and an alpha value
464+
// return the actual color
465+
return color.map((c) => c / alpha)
466+
}
467+
468+
function polar_premultiply(color, alpha, hueIndex) {
469+
// given a color in a cylindicalpolar colorspace
470+
// and an alpha value
471+
// return the premultiplied form.
472+
// the index says which entry in the color array corresponds to hue angle
473+
// for example, in OKLCH it would be 2
474+
// while in HSL it would be 0
475+
return color.map((c, i) => c * (hueIndex === i? 1 : alpha))
476+
}
477+
478+
function polar_un_premultiply(color, alpha, hueIndex) {
479+
// given a color in a cylindicalpolar colorspace
480+
// and an alpha value
481+
// return the actual color.
482+
// the hueIndex says which entry in the color array corresponds to hue angle
483+
// for example, in OKLCH it would be 2
484+
// while in HSL it would be 0
485+
return color.map((c, i) => c / (hueIndex === i? 1 : alpha))
486+
}
487+
488+
// Convenience functions can easily be defined, such as
489+
function hsl_premultiply(color, alpha) {
490+
return polar_premultiply(color, alpha, 0);
491+
}

0 commit comments

Comments
 (0)