Jeff Johnson said:
I'm using VS2005, .NET Framework 2.0.
I'm drawing lines/rectangles/arrow etc. over a background image.
The background image might change it's colors so the
lines/rectangles/arrow
I'm drawing will bled in the background and the user may not see it
clear
enough.
I want draw the lines/rectangles/arrow so their color will very
according
to
the behind background image color.
Any idea how to do that?
[Adding microsoft.public.dotnet.framework.drawing to the newsgroups
list]
You might be able to construct some sort of color matrix that you can
apply to your graphics so that they will always have a good contrast to
the background color, but I can't suggest any particular implementation
to
you, just a concept. In fact, I don't know if there's a single matrix
that'll do what you want. I did something a while back where I was
generating a bunch of colors in Excel for cell backgrounds and I wanted
to
put the name of the color in the cell. When the color got "dark enough"
I
wanted to switch the cell text to white instead of black. I found a
formula to calculate the luminescence of a color and just picked a
threshhold. Above that threshhold I deemed the background to be "light
enough" and used black. Below, I used white. In other words, you'll
probably have to do some manual twiddling with whatever method you
ultimately choose.
So while driving home yesterday I thought about a potential method which
should give you some very different colors in most cases: you could flip
the
bits of the red, green, and blue components of the background color. The
only place where this won't give you colors with much contrast is
mid-range
grays. Take, for example, (128, 128, 128). Flipping these bits gives you
(127, 127, 127), which isn't very different. The further you get from
128,
though, the more contrast you'll get.
Here is something I came up with once... The idea was to find the inverse
color, but give it a bit of a white shift to overcome the 128,128,128
problem.
Seems to work ok...
Color ComputeBalancedInverse (Color original)
{
int red = 255 - original.R;
int green = 255 - original.G;
int blue = 255 - original.B;
double kr = (red != 0) ? 255.0 / red : 1.0;
double kg = (green != 0) ? 255.0 / green : 1.0;
double kb = (blue != 0) ? 255.0 / blue : 1.0;
return Color.FromArgb ((int)(red * kr), (int)(green * kg), (int)(blue *
kb));
}
Well, anyway that should be close - the original was VB.NET, so I just
converted it in my head...
But, the results look somehthing like:
Input Output
808080 FFFFFF
FFFFFF 000000
FF0000 00FFFF
FFFF00 0000FF
Basically, if you had text and you set the foreground and background
colors to
one of these pairs, the text should be readable...