Last active
February 9, 2016 14:02
-
-
Save yoksel/8d11498b3a26ab4f9d43 to your computer and use it in GitHub Desktop.
hexToRgb and back
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 hexToRgb( color ) { | |
console.log('Input: ' + color ); | |
color = color.replace('#',''); | |
var rgb = ''; | |
var rgbList = []; | |
var colorList = [ | |
color.substr(0,2), | |
color.substr(2,2), | |
color.substr(4,2) | |
]; | |
for (var i = 0; i < colorList.length; i++) { | |
rgbList.push ( parseInt( colorList[i] , 16) ); | |
} | |
rgb = 'rgb(' + rgbList.join(',') + ')'; | |
console.log('Output: ' + rgb + '\n\n'); | |
return rgb; | |
} | |
function rgbToHex( color ){ | |
console.log('Input: ' + color ); | |
var colorBody = color.substring(4, color.length - 1); | |
var colorList = colorBody.split(','); | |
var hex = '#'; | |
for (var i = 0; i < colorList.length; i++) { | |
hex += (+colorList[i]).toString(16).toUpperCase(); | |
} | |
console.log('Output: ' + hex + '\n\n'); | |
return hex; | |
} | |
var hex = '#FF99CC'; | |
var rgb = hexToRgb( '#FF99CC' ); | |
var newHex = rgbToHex( rgb ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment