Skip to content

Instantly share code, notes, and snippets.

@nakajmg
Created August 3, 2024 03:26
Show Gist options
  • Save nakajmg/8232c887fe1e9e95401c63147ddee409 to your computer and use it in GitHub Desktop.
Save nakajmg/8232c887fe1e9e95401c63147ddee409 to your computer and use it in GitHub Desktop.
This function calculates the color value for a specified color code as it appears when an opacity is applied to it, matching the visual appearance of the color as if the opacity was not applied.
function adjustColorWithOpacity({r, g, b, alpha}) {
return [r,g,b].map((v) => {
const _alpha = Math.round((1 - alpha)* 10) / 10;
return Math.max(Math.round((v - (_alpha) * 255) / (alpha)), 0)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment