Skip to content

Instantly share code, notes, and snippets.

@IOIO72
Created August 23, 2019 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IOIO72/5a41786acb6dfadb09d432c3533a3b31 to your computer and use it in GitHub Desktop.
Save IOIO72/5a41786acb6dfadb09d432c3533a3b31 to your computer and use it in GitHub Desktop.
Convert decimal RGB values to HTML hex string
// 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