gka / chroma.js

JavaScript library for all kinds of color manipulations
https://gka.github.io/chroma.js/
Other
10.08k stars 543 forks source link

rgba to rgb #268

Closed GuySpotnix closed 2 weeks ago

GuySpotnix commented 3 years ago

Hi every one,

I'm looking for a way to get rgb out of rgba color (with white as back color). Not sure how to do that using chroma js.

Ref: https://stackoverflow.com/questions/2049230/convert-rgba-color-to-rgb

Thanks!

NotoriousWebmaster commented 2 years ago

I'm disappointed no one has answered this question. I'm looking for the same information.

jdanford commented 1 year ago

There isn't a built-in solution, but you can do it with weighted averaging:

const color = chroma("rgba(255, 0, 0, 0.5)");
const alpha = color.alpha();
const colorWithFullAlpha = color.alpha(1);
chroma.average([colorWithFullAlpha, "white"], "rgb", [alpha, 1 - alpha]);
gka commented 2 weeks ago

Another easy way to get a non-transparent color that looks the same as if the color would be transparent against a background is to use chroma.mix. Here's a quick example for getting the equivalent color of red with 70% opacity against a white background:

const equivalentColor = chroma.mix('white', 'red', 0.7, 'rgb');

Here's an Observable notebook demonstrating this: https://observablehq.com/@gka/chroma-remove-alpha-from-color