Last active
November 14, 2018 09:57
-
-
Save gladchinda/22d03e5dd656d5df07922b9b3852da16 to your computer and use it in GitHub Desktop.
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 characters
function leftZeroPad(value, maxlength) { | |
while (value.length < maxlength) { | |
value = '0' + value; | |
} | |
return value; | |
} | |
function boundedValue(minvalue, maxvalue) { | |
return function(value) { | |
return Math.max(minvalue, Math.min(maxvalue, +value || 0)); | |
} | |
} | |
function colorValue(value) { | |
var hex = boundedValue(0, 255)(value).toString(16); | |
return leftZeroPad(hex, 2); | |
} | |
function rgbToHex(red, green, blue) { | |
red = colorValue(red); | |
green = colorValue(green); | |
blue = colorValue(blue); | |
return '#' + red + green + blue; | |
} | |
console.log(rgbToHex()); // "#000000" | |
console.log(rgbToHex(255)); // "#ff0000" | |
console.log(rgbToHex(255, 128)); // "#ff8000" | |
console.log(rgbToHex(255, 128, 64)); // "#ff8040" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment