Last active
March 28, 2018 21:10
-
-
Save carlosrberto/27d3ab597359916f47ca9e318e9b144d to your computer and use it in GitHub Desktop.
Functional RGB to Hex Color
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
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