如何以整数形式反转RGB颜色? [英] How to invert an RGB color in integer form?

查看:275
本文介绍了如何以整数形式反转RGB颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个以32位无符号整数形式(例如0xFF00FF)的RGB颜色,如果不使用bitshift操作提取其各个组件,您将如何反转它(获得负面颜色)?

我想知道是否可能只使用按位运算(AND,OR,XOR)。

更确切地说,使用最少数目的指示?

解决方案

我认为它很简单。
您可以计算0xFFFFFF-YourColor。这将是倒置的颜色。

  int neg = 0xFFFFFF  -  originalRGB 

//可选:设置alpha到255:
int neg =(0xFFFFFF - originalRGB)| 0xFF000000;


Given an RGB color in 32-bit unsigned integer form (eg. 0xFF00FF), how would you invert it (get a negative color), without extracting its individual components using bitshift operations?

I wonder whether it's possible using just bitwise operations (AND, OR, XOR).

More precisely, what's the algorithm that uses the least number of instructions?

解决方案

I think it is so simple. You can just calculate 0xFFFFFF-YourColor. It will be the inverted color.

int neg = 0xFFFFFF - originalRGB

// optional: set alpha to 255:
int neg = (0xFFFFFF - originalRGB) | 0xFF000000;

这篇关于如何以整数形式反转RGB颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆