-
-
Save christopherdebeer/1002308 to your computer and use it in GitHub Desktop.
function( | |
r, // red, as a number from 0 to 255 | |
g, // green, as a number from 0 to 255 | |
b, // blue, as a number from 0 to 255 | |
p // as goog a place as any to declare p | |
){ | |
p = Math.pow; // alias for "Math.pow" | |
return // return a luminosity value madeup of | |
0.2126 * p(r/255, 2.2) + // the red value plus | |
0.7152 * p(g/255, 2.2) + // the green value plus | |
0.0722 * p(b/255, 2.2) // the blue value | |
} |
function(r,g,b,p){p=Math.pow;return 0.2126*p(r/255,2.2)+0.7152*p(g/255,2.2)+0.0722*p(b/255,2.2)} |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
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. |
{ | |
"name": "RGBtoLum", | |
"description": "Calculate the luminosity of a colour based on its RGB values", | |
"keywords": [ | |
"RGB", | |
"colour", | |
"color", | |
"Luminosity", | |
"Calculation" | |
] | |
} |
That's fair enough. I wouldn't claim to know nearly enough about colour profiles as to be able to agree or disagree. I found that the gist above worked for calculating luminosity differences, in a specific project/instance, which even if not perfect was certainly useful. Thanks for the feedback though, it's much appreciated.
@christopherdebeer I'm no expert either. I just happened to know this particular thing due to some conversion work I did earlier :)
I think you can shave off some bytes if you alias Math.pow
:
function(r,g,b,p){p=Math.pow;return 0.2126*p(r/255,2.2)+0.7152*p(g/255,2.2)+0.0722*p(b/255,2.2)}
thanks :)
looks cool! by omitting the leading 0 on floats .34
you can save some more bytes! Plus that makes it possible to save another byte if you leave out the space after return!
One thing to note is that the coefficients used in this mapping are based on a particular color profile (observant, illuminant) and therefore this mapping cannot be assumed to be a universal one. There is no direct mapping between RGB <-> Lab.