Skip to content

Instantly share code, notes, and snippets.

@Dammmien
Created December 14, 2015 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dammmien/1733eef8b06838b42fc4 to your computer and use it in GitHub Desktop.
Save Dammmien/1733eef8b06838b42fc4 to your computer and use it in GitHub Desktop.
RGB to Hex and Hex to RGB
var rgbToHex = ( r, g, b ) => "#" + ( ( 1 << 24 ) + ( r << 16 ) + ( g << 8 ) + b ).toString( 16 ).slice( 1 );
var hexToRgb = ( hex ) => [ parseInt( hex.substring( 1, 3 ), 16 ), parseInt( hex.substring( 3, 5 ), 16 ), parseInt( hex.substring( 5, 7 ), 16 ) ];
rgbToHex( 175, 25, 70 ); // #af1946
hexToRgb( "#af1946" ); // [ 175, 25, 70 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment