Created
September 7, 2017 21:43
-
-
Save tobia/253a8da3469c3a24c1de1d82ef58fc30 to your computer and use it in GitHub Desktop.
Fast luminance inversion (aka. color invert + hue rotate 180°)
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
// 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; | |
} | |
} |
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
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