Skip to content

Instantly share code, notes, and snippets.

@glooer
Created December 24, 2024 11:34
Show Gist options
  • Select an option

  • Save glooer/422d5c6058e09f1d65f4680309be9ee7 to your computer and use it in GitHub Desktop.

Select an option

Save glooer/422d5c6058e09f1d65f4680309be9ee7 to your computer and use it in GitHub Desktop.
RGB to RGBA over white
// https://stackoverflow.com/a/6672545/13246729
convert_rgb_to_rgba = (r, g, b) => {
const min = Math.min(r, g, b)
const alpha = (255 - min) / 255
const result = {
r: (r - min) / alpha,
g: (g - min) / alpha,
b: (b - min) / alpha,
a: alpha
}
return `rgba(${~~result.r}, ${~~result.g}, ${~~result.b}, ${result.a.toFixed(2)})`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment