Fast algorithm to invert an ARGB color value to ABGR?
Since A and G are in place, you can probably do a little better by masking off the B and R and then adding them back. Haven't tested it but ought to be 95% right:
private static final int EXCEPT_R_MASK = 0xFF00FFFF;
private static final int ONLY_R_MASK = ~EXCEPT_R_MASK;
private static final int EXCEPT_B_MASK = 0xFFFFFF00;
private static final int ONLY_B_MASK = ~EXCEPT_B_MASK;
private int getBufferedColor(final int pSourceColor) {
int r = (pSourceColor & ONLY_R_MASK) >> 16;
int b = pSourceColor & ONLY_B_MASK;
return
(pSourceColor & EXCEPT_R_MASK & EXCEPT_B_MASK) | (b << 16) | r;
}
In my opinion, the following function is fast enough to return the ABGR color while passing an ARGB color and vice-versa!
int argbToABGR(int argbColor) {
int r = (argbColor >> 16) & 0xFF;
int b = argbColor & 0xFF;
return (argbColor & 0xFF00FF00) | (b << 16) | r;
}