Home All Groups Group Topic Archive Search About

Invert of background color

Author
26 Apr 2007 5:38 PM
Mahesh Nimbalkar
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

Author
26 Apr 2007 5:51 PM
Peter Duniho
"Mahesh Nimbalkar" <MaheshNimbal***@discussions.microsoft.com> wrote in
message news:96255467-C914-44D0-B7CF-9114934B0E34@microsoft.com...
> 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
Author
26 Apr 2007 6:36 PM
Mahesh Nimbalkar
Thank you. It worked.



Show quote
"Peter Duniho" wrote:

> "Mahesh Nimbalkar" <MaheshNimbal***@discussions.microsoft.com> wrote in
> message news:96255467-C914-44D0-B7CF-9114934B0E34@microsoft.com...
> > 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
>
>
Author
26 Apr 2007 8:35 PM
Tim Van Wassenhove
Peter Duniho schreef:
Show quote
> "Mahesh Nimbalkar" <MaheshNimbal***@discussions.microsoft.com> wrote in
> message news:96255467-C914-44D0-B7CF-9114934B0E34@microsoft.com...
>> 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);
>    }

Too much typing ;)

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



--
Tim Van Wassenhove <url:http://www.timvw.be/>
Author
26 Apr 2007 8:53 PM
Göran Andersson
Tim Van Wassenhove wrote:
Show quote
> Peter Duniho schreef:
>> "Mahesh Nimbalkar" <MaheshNimbal***@discussions.microsoft.com> wrote
>> in message news:96255467-C914-44D0-B7CF-9114934B0E34@microsoft.com...
>>> 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);
>>    }
>
> 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);

--
Göran Andersson
_____
http://www.guffa.com
Author
26 Apr 2007 9:01 PM
Göran Andersson
Mahesh Nimbalkar wrote:
Show quote
> 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);

--
Göran Andersson
_____
http://www.guffa.com

AddThis Social Bookmark Button