Skip to content

Instantly share code, notes, and snippets.

@christopherdebeer
Forked from 140bytes/LICENSE.txt
Created June 1, 2011 13:40
Show Gist options
  • Save christopherdebeer/1002308 to your computer and use it in GitHub Desktop.
Save christopherdebeer/1002308 to your computer and use it in GitHub Desktop.
RGB to Luminosity (140byt.es)
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"
]
}
@atesgoral
Copy link

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.

@christopherdebeer
Copy link
Author

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.

@atesgoral
Copy link

@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)}

@christopherdebeer
Copy link
Author

thanks :)

@Kambfhase
Copy link

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!

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