Skip to content

Instantly share code, notes, and snippets.

@svgeesus
Last active November 4, 2020 18:04
Show Gist options
  • Save svgeesus/e69fe1ef4fa76071a34b2ee0596390a5 to your computer and use it in GitHub Desktop.
Save svgeesus/e69fe1ef4fa76071a34b2ee0596390a5 to your computer and use it in GitHub Desktop.

Premultiplied alpha Lab interpolation example

let color1 = new Color("rgb(76% 62% 03% / 0.4)");
let start1 = color1.to("lab");
let pm1 = start1.coords.map(c => c * color1.alpha);
let color2 = new Color("color(display-p3 0.84 0.19 0.72 / 0.6)");
let start2 = color2.to("lab");
let pm2 = start2.coords.map(c => c * color2.alpha);
let blend = color1.range(color2, {
    space: "lab", // interpolation space
    premultiplied: true,
    outputSpace: "lab"
});
let blend05 = [(pm1[0] + pm2[0]) /2, (pm1[1] + pm2[1]) /2, (pm1[2] + pm2[2]) /2];
blend(.5); // midpoint
blend(.5).to("srgb");

// same again without premultiplication
let blend2 = color1.range(color2, {
    space: "lab", // interpolation space
    outputSpace: "lab"
});
blend2(.5); // midpoint
blend2(.5).to("srgb");

blend(.5).chroma;  // with alpha premultiplication
blend2(.5).chroma; // without premultiplication
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment