Skip to content

Instantly share code, notes, and snippets.

@carlosrberto
Last active March 28, 2018 21:10
Show Gist options
  • Save carlosrberto/27d3ab597359916f47ca9e318e9b144d to your computer and use it in GitHub Desktop.
Save carlosrberto/27d3ab597359916f47ca9e318e9b144d to your computer and use it in GitHub Desktop.
Functional RGB to Hex Color
const fixHex = v => v.length === 1 ? `0${v}` : v;
const decToHex = v => {
return fixHex(parseInt(v, 10).toString(16).toUpperCase());
};
// with map
const rgbToHexColor = function() {
return [...arguments].map(decToHex).join('');
}
// with reduce
const rgbToHexColor2 = function() {
return [...arguments].reduce((a, b) => a + decToHex(b), '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment