Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I invert a colour?

I know that this won't directly invert a colour, it will just 'oppose' it. I was wondering if anyone knew a simple way (a few lines of code) to invert a colour from any given colour?

At the moment I have this (which isn't exactly the definition of an invert, because if I pass it a grey / gray colour it will return something extremely similar e.g. 127, 127, 127):

const int RGBMAX = 255;  Color InvertMeAColour(Color ColourToInvert) {     return Color.FromArgb(RGBMAX - ColourToInvert.R,        RGBMAX - ColourToInvert.G, RGBMAX - ColourToInvert.B); } 
like image 310
Lloyd Powell Avatar asked Jul 22 '09 13:07

Lloyd Powell


1 Answers

It depends on what do you mean by "inverting" a color

Your code provides a "negative" color.

Are you looking for transform red in cyan, green in purple, blue in yellow (and so on) ? If so, you need to convert your RGB color in HSV mode (you will find here to make the transformation).

Then you just need to invert the Hue value (change Hue by 360-Hue) and convert back to RGB mode.

EDIT: as Alex Semeniuk has mentioned, changing Hue by (Hue + 180) % 360 is a better solution (it does not invert the Hue, but find the opposite color on the color circle)

like image 166
ThibThib Avatar answered Sep 22 '22 09:09

ThibThib