Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for calculating inverse color

I have a RGB color in the range of 0.0 to 1.0 for each fragment, I need an algorithm to get the inverse of the color, any ideas how can I do this?

like image 246
Ricardo Sanchez Avatar asked Aug 05 '11 19:08

Ricardo Sanchez


People also ask

How do you invert colors in an algorithm?

Colour inversion is achieved by subtracting each RGB colour value from the maximum possible value (usually 255). Another effect which is related to colour inversion is the solarise effect.

How does color inversion work RGB?

Inverting colors in images It does this by inverting the brightness value of each pixel in each color channel. For example, if a pixel is pure red, its brightness levels are 255, 0, 0 in RGB mode. When inverted, this pixel's brightness values become 0, 255, 255, changing it to pure blue-green, its opposite in hue.

Which type of operation is most adequate to invert a RGB colour?

xor ( ^ ) with 0 returns the original value unmodified. xor with 0xff flips the bits. so in the above case we have 0xaarrggbb we are flipping/inverting r, g and b. This should be the most efficient way to invert a color.


1 Answers

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 
like image 69
Mark Ransom Avatar answered Oct 14 '22 06:10

Mark Ransom