Created
August 23, 2019 15:56
-
-
Save IOIO72/5a41786acb6dfadb09d432c3533a3b31 to your computer and use it in GitHub Desktop.
Convert decimal RGB values to HTML hex string
This file contains 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
// prepend a character to a string for a given result string length | |
const prependCharToLength = (str: string, char: string = '0', len: number = 2): string => { | |
let r: string[] = []; | |
let s: string[] = str.split(''); | |
for (let i: number = len - 1; i >= 0; i--) { | |
r[i] = (s.length - i > 0) ? s[i] : char; | |
} | |
return r.join(''); | |
}; | |
// convert decimal color rgb values to html hex color string | |
const convertColorsToHex = (r: number, g: number, b: number): string => `#${ | |
prependCharToLength(r.toString(16))}${ | |
prependCharToLength(g.toString(16))}${ | |
prependCharToLength(b.toString(16))}`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment