Created
August 12, 2020 09:37
-
-
Save renoinn/e7d18aac6a16d4f66c501aea0f089125 to your computer and use it in GitHub Desktop.
hexをrgbaに変換するjs
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
| const isValidHex = (hex) => /^#([A-Fa-f0-9]{3,4}){1,2}$/.test(hex) | |
| const getChunksFromString = (st, chunkSize) => st.match(new RegExp(`.{${chunkSize}}`, "g")) | |
| const convertHexUnitTo256 = (hexStr) => parseInt(hexStr.repeat(2 / hexStr.length), 16) | |
| const getAlphafloat = (a, alpha) => { | |
| if (typeof a !== "undefined") {return a / 255} | |
| if ((typeof alpha != "number") || alpha <0 || alpha >1){ | |
| return 1 | |
| } | |
| return alpha | |
| } | |
| export const hexToRGBA = (hex, alpha) => { | |
| if (!isValidHex(hex)) {throw new Error("Invalid HEX")} | |
| const chunkSize = Math.floor((hex.length - 1) / 3) | |
| const hexArr = getChunksFromString(hex.slice(1), chunkSize) | |
| const [r, g, b, a] = hexArr.map(convertHexUnitTo256) | |
| return `rgba(${r}, ${g}, ${b}, ${getAlphafloat(a, alpha)})` | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment