Skip to content

Instantly share code, notes, and snippets.

@A1rPun
Last active February 4, 2022 04:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A1rPun/b650b819f70942feb324 to your computer and use it in GitHub Desktop.
Save A1rPun/b650b819f70942feb324 to your computer and use it in GitHub Desktop.
JavaScript convert base10 color to HEX color
/**
* 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);
}
@terenc3
Copy link

terenc3 commented Oct 5, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment