Last active
October 4, 2016 12:43
-
-
Save westonal/7f28f3312a91b5d3f359f2c163ce2458 to your computer and use it in GitHub Desktop.
Averaging colour
This file contains hidden or 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
| public static int average(int argb1, int argb2){ | |
| return (((argb1 & 0xFF) + (argb2 & 0xFF)) >> 1) | //b | |
| (((argb1 >> 8 & 0xFF) + (argb2 >> 8 & 0xFF)) >> 1) << 8 | //g | |
| (((argb1 >> 16 & 0xFF) + (argb2 >> 16 & 0xFF)) >> 1) << 16 | //r | |
| (((argb1 >> 24 & 0xFF) + (argb2 >> 24 & 0xFF)) >> 1) << 24; //a | |
| } | |
| overflow issues: | |
| public static int average2(int argb1, int argb2){ | |
| return (((argb1 & 0x000000ff) + (argb2 & 0x000000ff)) >> 1) | //b | |
| (((argb1 & 0x0000ff00) + (argb2 & 0x0000ff00)) >> 1) & 0x0000ff00 | //g | |
| (((argb1 & 0x00ff0000) + (argb2 & 0x00ff0000)) >> 1) & 0x00ff0000 | //r | |
| (((argb1 & 0xff000000) + (argb2 & 0xff000000)) >> 1) & 0xff000000; //a | |
| } | |
| Hybrid: | |
| public static int average2(int argb1, int argb2){ | |
| return (((argb1 & 0x000000ff) + (argb2 & 0x000000ff)) >> 1) | //b | |
| (((argb1 & 0x0000ff00) + (argb2 & 0x0000ff00)) >> 1) & 0x0000ff00 | //g | |
| (((argb1 & 0x00ff0000) + (argb2 & 0x00ff0000)) >> 1) & 0x00ff0000 | //r | |
| (((argb1 >> 24 & 0xff) + (argb2 >> 24 & 0xff)) >> 1) << 24; //a | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment