Skip to content

Instantly share code, notes, and snippets.

@felix-lipski
Created May 22, 2022 21:52
Show Gist options
  • Save felix-lipski/e6cd94adaa449304018e665856d5a3a0 to your computer and use it in GitHub Desktop.
Save felix-lipski/e6cd94adaa449304018e665856d5a3a0 to your computer and use it in GitHub Desktop.
const numWithOpacity = (nums, by) =>
"#" +
nums.map((x) => Math.round(x + (255 - x) * (1 - by)).toString(16)).join("");
const hexWithOpacity = (rgbHex, by) =>
numWithOpacity(
[rgbHex.slice(1, 3), rgbHex.slice(3, 5), rgbHex.slice(5, 7)].map((hex) =>
parseInt(hex, 16)
),
by
);
// console.log(hexWithOpacity("#000000", 0.26));
console.log(numWithOpacity([0, 0, 0], 0.54));
// console.log(numWithOpacity([33, 33, 33], 0.26));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment