Created
January 29, 2018 21:50
-
-
Save rodu/e31d1ce71b277687dc5691e04fc20da0 to your computer and use it in GitHub Desktop.
A function to transform an hex color to its rgba equivalent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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