-
-
Save jed/983535 to your computer and use it in GitHub Desktop.
convert RGB to HEX
This file contains 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( | |
a, // red, as a number from 0 to 255 | |
b, // green, as a number from 0 to 255 | |
c // blue, as a number from 0 to 255 | |
){ | |
return "#" + // return a number sign, and | |
( // combine the octets into a 32-bit integer as: [1][a][b][c] | |
( // operator precedence is (+) > (<<) > (|) | |
256 // [1][0] | |
+ a // [1][a] | |
<< 8 // [1][a][0] | |
| b // [1][a][b] | |
) | |
<< 8 // [1][a][b][0] | |
| c // [1][a][b][c] | |
) | |
.toString(16) // then serialize to a hex string, and | |
.slice(1) // remove the 1 to get the number with 0s intact. | |
} |
This file contains 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(a,b,c){return"#"+((256+a<<8|b)<<8|c).toString(16).slice(1)} |
This file contains 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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Jed Schmidt <http://jed.is> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains 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
{ | |
"name": "rgb2hex", | |
"description": "Converts RGB colors to a hexadecimal string.", | |
"keywords": [ | |
"color", | |
"css", | |
"hex", | |
"rgb" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@GarrettS
IMHO it would make more sense to declare the functions in the preparation/setup code section on jsPerf, and then have the actual tests themselves only consist of a single function call. That seems like the fairest comparison. Something like this: http://jsperf.com/rgbtohex/6