Last active
February 4, 2022 04:29
-
-
Save A1rPun/b650b819f70942feb324 to your computer and use it in GitHub Desktop.
JavaScript convert base10 color to HEX color
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
/** | |
* Converts a BGR base10 color to an RGB Hex value. | |
* | |
* @method colorToHexString | |
* @param {Integer} dColor A color as integer. | |
* @returns {String} The RGB hex code as string. | |
*/ | |
function colorToHexString(dColor) { | |
return '#' + ("000000" + (((dColor & 0xFF) << 16) + (dColor & 0xFF00) + ((dColor >> 16) & 0xFF)).toString(16)).slice(-6); | |
} | |
/** | |
* Converts RGB Hex string to a BGR base10 color. | |
* | |
* @method hexStringToColor | |
* @param {String} hex A hex code as string. | |
* @returns {Integer} The integer in base10 format. | |
*/ | |
function hexStringToColor(hex) { | |
var r = parseInt(hex.slice(1, 3), 16), | |
g = parseInt(hex.slice(3, 5), 16), | |
b = parseInt(hex.slice(5, 7), 16); | |
return (r | g << 8 | b << 16); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did not work for me but check out https://gist.github.com/agirorn/0e740d012b620968225de58859ccef5c#gistcomment-2662867