Skip to content

Instantly share code, notes, and snippets.

@rodu
Created January 29, 2018 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodu/e31d1ce71b277687dc5691e04fc20da0 to your computer and use it in GitHub Desktop.
Save rodu/e31d1ce71b277687dc5691e04fc20da0 to your computer and use it in GitHub Desktop.
A function to transform an hex color to its rgba equivalent
// Depends on LoDash chunk function
import { chunk } from 'lodash';
const hexToRgba = (hex = '000000', alpha = 1) => {
const hexChunkSize = hex.length / 3;
const rgb = _.chunk(Array.from(hex.replace('#', '')), hexChunkSize)
.map((chunk) => chunk.join(''))
.map((hex) => parseInt(hex, 16))
.join(',');
return `rgba(${rgb},${alpha})`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment