|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Invert of background colorHi,
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" <MaheshNimbal***@discussions.microsoft.com> wrote in What about this: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. 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.
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 > > Peter Duniho schreef:
Show quote > "Mahesh Nimbalkar" <MaheshNimbal***@discussions.microsoft.com> wrote in Too much typing ;)> 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); > } return Color.FromArgb(int.MaxValue - colorIn.ToArgb()); Tim Van Wassenhove wrote:
Show quote > Peter Duniho schreef: That will produce a completely transparent color. I think that you want:>> "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()); > return Color.FromArgb(colorIn.ToArgb() ^ 0xffffff); Mahesh Nimbalkar wrote:
Show quote > Hi, The inverted color contrasts against most colors, but not all. The > > > 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 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); |
|||||||||||||||||||||||