Luk*_* Vo 6 java algorithm android colors
我IntBuffer用来操作Bitmap的像素,但缓冲区中的值应该是AABBGGRR,而颜色常量是AARRGGBB.我知道我可以使用Color.argb,Color.a......反转,但我认为它并不完美.
我需要操作非常多的像素,所以我需要一种能够在短时间内执行此算子的算法.我想到了这个Bit Expression,但它不正确:
0xFFFFFFFF ^ pSourceColor
Run Code Online (Sandbox Code Playgroud)
如果没有更好的,也许我将使用位移操作符(执行Color.a,...)而不是调用函数来减少时间.
编辑:
这是我目前转换的函数,虽然我认为应该有更好的算法(更少的运算符)来执行它:
private int getBufferedColor(final int pSourceColor) {
return
((pSourceColor >> 24) << 24) | // Alpha
((pSourceColor >> 16) & 0xFF) | // Red -> Blue
((pSourceColor >> 8) & 0xFF) << 8 | // Green
((pSourceColor) & 0xFF) << 16; // Blue -> Red
}
Run Code Online (Sandbox Code Playgroud)
由于A和G已经到位,你可以通过屏蔽B和R然后再添加它们来做得更好.没有测试过,但应该是95%的权利:
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;
}
Run Code Online (Sandbox Code Playgroud)
在我看来,以下函数足够快,可以在传递ARGB颜色的同时返回ABGR颜色,反之亦然!
int argbToABGR(int argbColor) {
int r = (argbColor >> 16) & 0xFF;
int b = argbColor & 0xFF;
return (argbColor & 0xFF00FF00) | (b << 16) | r;
}
Run Code Online (Sandbox Code Playgroud)