This function is supposed to take an integer that contains an rgb value, extract the value of each channel, and then return that calculated luminance of the pixel.
As it stands, the performance of this function is not fast enough. Looking at it, I can't think of anything else that I can do to speed it up.
Also, I was thinking if there's an algorithm to calculate the luminance straight from the integer instead of converting to rgb first, and whether that would be faster.
Anyway, here it is:
def luminance(pixel):
"""Calculate and return the luminance of a pixel."""
r = pixel & 255
g = (pixel & (255 << 8)) >> 8
b = pixel >> 16
return 0.299 * r + 0.587 * g + 0.114 * b
Edit: What ever improvements you may have, however little they are, don't hesitate to suggest them. This function is called hundreds of thousands (sometimes even millions) of times by the script.