Skip to content

Instantly share code, notes, and snippets.

@renoinn
Created August 12, 2020 09:37
Show Gist options
  • Select an option

  • Save renoinn/e7d18aac6a16d4f66c501aea0f089125 to your computer and use it in GitHub Desktop.

Select an option

Save renoinn/e7d18aac6a16d4f66c501aea0f089125 to your computer and use it in GitHub Desktop.
hexをrgbaに変換するjs
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