开发者

Algorithm for calculating inverse color

I have a RGB color in the range of 0.0 to 1.0 for each开发者_如何学JAVA fragment, I need an algorithm to get the inverse of the color, any ideas how can I do this?


newR = 1.0 - r
newG = 1.0 - g
newB = 1.0 - b

If the color has a premultiplied Alpha value use the alpha instead of 1.0:

newR = a - r
newG = a - g
newB = a - b


If you are using RGB values to 255, you could do something like this:

newR = 255 - r;
newG = 255 - g;
newB = 255 - b;

To understand this concept, imagine each value as a number line going from 0 to 255. If you graph a number on that number line then the number is that distance away from the beginning of the number line. In order to negate it, the number must travel to the other end of the number line. This algorithm basically flips the number line without moving the beginning or the end. Our number line is from 0 to 255, so, if the number was 10 away from the beginning (10), now it will be 10 away from the end (245), thus negating the color.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜