我是位操作新手,想用它执行以下操作:
我的程序需要反转颜色的 argb 代码,例如红色变为青色或白色变为黑色。使用 Photoshop 进行实验后,我发现您必须计算 255 - 红色、255 - 绿色和 255 - 蓝色。所以这可能看起来像这样:
int getInversed(int argb) {
Color old = new Color(argb);
Color negative = new Color(255 - old.getRed(), 255 - old.getGreen(), 255 - old.getBlue(), old.getAlpha());
return negative.getRGB();
}
现在我猜如果你将红色、绿色和蓝色值作为 8 位字节
,你可以简单地使用 ~
运算符将它们反转得到相同的结果。
但我无法反转整个 argb 代码,因为这也会反转 alpha 并且我希望 alpha 保持不变:
public int getInversed(int argb) {
return ~argb; //inverses alpha as well
}
那么如何使用位运算反转 argb 代码,忽略 alpha 部分?
最佳答案
您可以使用xor mask来做到这一点:
Therefore inversion of the values of bits is done by XORing them with a 1
Also note that XOR masking is bit-safe, meaning that it will not affect unmasked bits
return argb ^ 0x00FFFFFF;
关于java - 位操作: inverse argb ignoring alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46991486/