Skip to content

Instantly share code, notes, and snippets.

@tobia
Created September 7, 2017 21:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobia/253a8da3469c3a24c1de1d82ef58fc30 to your computer and use it in GitHub Desktop.
Save tobia/253a8da3469c3a24c1de1d82ef58fc30 to your computer and use it in GitHub Desktop.
Fast luminance inversion (aka. color invert + hue rotate 180°)
// Fast luminance inversion.
int luminv(int rgb) {
int r = rgb >> 16,
g = rgb >> 8 & 0xff,
b = rgb & 0xff;
switch (((r > g) << 1 | (g > b)) << 1 | (b > r)) {
case 4: // r > b > g
case 3: // g > b > r
return rgb + (255 - r - g) * 0x10101;
case 2: // g > r > b
case 5: // b > r > g
return rgb + (255 - g - b) * 0x10101;
default:
return rgb + (255 - r - b) * 0x10101;
}
}
def luminv(r,g,b):
"""Fast luminance inversion."""
c = 255 - max(r,g,b) - min(r,g,b)
return r+c, g+c, b+c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment