Last active
October 9, 2021 04:49
-
-
Save joeyred/d90737343b539ab12ac19f4611820e45 to your computer and use it in GitHub Desktop.
Revisions
-
joeyred revised this gist
Oct 9, 2021 . 1 changed file with 33 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1 +1,33 @@ interface RGBA { red: number green: number blue: number alpha?: number } const RGBAToHexA = ({ red, green, blue, alpha }: RGBA): string => { // We are using a system where 16 representations of value // exist (0-9, a-f) This will give us the hex representation // of 0-255, which is 256 total values. let redHex = red.toString(16) let greenHex = green.toString(16) let blueHex = blue.toString(16) let alphaHex // Handle instances where the hex value assigned // only has a single digit. if (redHex.length === 1) redHex = `0${redHex}` if (greenHex.length === 1) greenHex = `0${greenHex}` if (blueHex.length === 1) blueHex = `0${blueHex}` // If alpha is passed, then handle it and return a 9 digit hex if (alpha) { alphaHex = Math.round(alpha * 255).toString(16) if (alphaHex.length === 1) alphaHex = `0${alphaHex}` return `#${redHex}${greenHex}${blueHex}${alphaHex}` } // Else return a 6 digit hex return `#${redHex}${greenHex}${blueHex}` } -
joeyred created this gist
Oct 9, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@