Invert of background color

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,


I have color as System.Drawing.Color c1 object as background color. Now I
would like to get another System.Drawing.Color c2 object which is invert of
c1 color to be used as foreground color.


e.g if I have c1 as black and I should get c2 as white.


Thanks,

Mahesh
 
Mahesh Nimbalkar said:
I have color as System.Drawing.Color c1 object as background color. Now I
would like to get another System.Drawing.Color c2 object which is invert
of
c1 color to be used as foreground color.

What about this:

public Color ColorInvert(Color colorIn)
{
return Color.FromArgb(colorIn.A, Color.White.R - colorIn.R,
Color.White.G - colorIn.G, Color.White.B - colorIn.B);
}

Technically you could use "255" instead of the values from Color.White, but
I don't like hard-coding numbers if there's a named equivalent that makes
sense.

If the above isn't what you want, then you should probably clarify what you
mean by "invert". :)

Pete
 
Thank you. It worked.



Peter Duniho said:
What about this:

public Color ColorInvert(Color colorIn)
{
return Color.FromArgb(colorIn.A, Color.White.R - colorIn.R,
Color.White.G - colorIn.G, Color.White.B - colorIn.B);
}

Technically you could use "255" instead of the values from Color.White, but
I don't like hard-coding numbers if there's a named equivalent that makes
sense.

If the above isn't what you want, then you should probably clarify what you
mean by "invert". :)

Pete
 
Peter Duniho schreef:
What about this:

public Color ColorInvert(Color colorIn)
{
return Color.FromArgb(colorIn.A, Color.White.R - colorIn.R,
Color.White.G - colorIn.G, Color.White.B - colorIn.B);
}

Too much typing ;)

return Color.FromArgb(int.MaxValue - colorIn.ToArgb());
 
Tim said:
Peter Duniho schreef:

Too much typing ;)

return Color.FromArgb(int.MaxValue - colorIn.ToArgb());

That will produce a completely transparent color. I think that you want:

return Color.FromArgb(colorIn.ToArgb() ^ 0xffffff);
 
Mahesh said:
Hi,


I have color as System.Drawing.Color c1 object as background color. Now I
would like to get another System.Drawing.Color c2 object which is invert of
c1 color to be used as foreground color.


e.g if I have c1 as black and I should get c2 as white.


Thanks,

Mahesh

The inverted color contrasts against most colors, but not all. The
inverted color of 50% gray is 50% gray. If you want it to work for any
color, you will have better luck with (intensity + 50%) modulo 100%:

return Color.FromArgb(colorIn.A, (colorIn.R + 128) % 256, (colorIn.G +
128) % 256, (colorIn.B + 128) % 256);
 
Back
Top