 |
|
 |
public Color IdealTextColor(Color bg)
{
return (bg.GetBrightness() > 0.5) ? Color.Black : Color.White;
}
The framework calculates the brightness for you, so you don't have to reinvent the wheel.
Moreover, this way you don't have to empirically determine the threshold, but simply use the mean brightness of black and of white colors (0.5).
|
|
|
|
 |
|
 |
Yes, that takes a lot less effort.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
 |
|
 |
Just had a need for exactly this code.
|
|
|
|
 |
|
 |
I live but to serve.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
 |
|
 |
Hi,
Using a third party library I have managed to get the color which will suit as a background color(Which nothing but major/average color of bitmap) for an image.
I want to get a color to write a text on the background color which is more appropriate than Black or white.
Currently I am using following technique
private Color GetTextColor(Color color)
{
const int maxColorComponentVal = 255;
return Color.FromArgb(maxColorComponentVal - color.R,
maxColorComponentVal - color.G,
maxColorComponentVal - color.B);
}
Is there any better technique than this?
Thanks !
Amit.
|
|
|
|
 |
|
 |
That's well outside the scope of this article. I think you should ask that question in the C# forum.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
 |
|
 |
Thanks John!
I am not expecting any language specific coding/answer, but some suggestion/logic which might guide me in right direction.
|
|
|
|
 |
|
 |
What happens if you have a grey (128,128,128) background?
I have no blog...
|
|
|
|
 |
|
 |
Hi, nice article. How can I do the same in C++? I don't know C# but what to borough the ideea. It's OK? Thanks in advance.
|
|
|
|
 |
|
|
 |
|
 |
double grey = 0.299 * GetRValue(Farbe) + 0.587 * GetGValue(Farbe) + 0.114 * GetBValue(Farbe);
if (grey < 127.)
return RGB(255,255,255);
else
return RGB(0,0,0);
that's usefull, but no new invention.
|
|
|
|
 |
|
 |
I never claimed it was new. I had to spend an unreasonable amount of time to find a solution, and when I did, I posted it here so that others here could find it on their favorite programming website.
I also posted a vbscript version of it last week.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
 |
|
 |
IMHO, injecting politics of any ilk into a CP article is unprofessional. However, it is also unprofessional to grade technical work on political content. Five points to the article.
|
|
|
|
 |
|
 |
Yeah, it's a shame that some folks can't separate what's said in the forums with what's posted for articles.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
 |
|
 |
I just thought you got it backwards -- you're boss couldn't be that bad .....
|
|
|
|
 |
|
 |
A nice article. Very useful code for a website I am currently working on. Had faced a lot of problems with the colors (not much a web developer)
Tarakeshwar Reddy
MCP, CCIE Q(R&S)
Experience is like a comb that life gives you when you are bald - Navjot Singh Sidhu
|
|
|
|
 |
|
 |
I have a similar snippet I've been using for years that does something similar with colours so you can always view a selected coloured text against it's background. Actually I have zillions of snippets that aren't really a full article, maybe if more of us start posting them they'll stop garnering automatic low votes from people who have their heads quite firmly up their...
|
|
|
|
 |
|
 |
You gotta start posting them, or I'll beat you to platinum status.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
 |
|
 |
I've never seen this trick before, but I'm sure I'll use it eventually. Very nice work, indeed! BTW, I don't like your politics; you need to stop getting your news exclusively from The Comedy Channel. I did vote your article a "4", because it *is* good work.
Atilla
|
|
|
|
 |
|
 |
But politics shouldn't enter into article voting - at all. It wasn't *my* idea to include my user profile in the articles, and if I could, I'd turn it off. BTW, the Comedy Channel gets all its news from me. Someone voted your message a 1 (uncalled for IMHO), but I voted it a 5 to balance things out.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
 |
|
 |
...where I have seen that code before. The algorithm you used is the same one used to convert a colour image to grayscale. You are just taking the grayscale value of the background colour and testing against that.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
 |
|
 |
In the currently-in-development enhancement to my ongoing project, I had been thinking that I'd want to be able to do something like this. The task was a low priority item that I figured I'd get to eventually.
Fortunately for me, I checked into CP this morning and saw this article, which doubtless saved me some future time.
Thanks.
|
|
|
|
 |
|
 |
Even though it can be found on the web, it's nice to have it documented as an article on CP.
We made the buttons on the screen look so good you'll want to lick them. Steve Jobs
|
|
|
|
 |
|
 |
That was my view. It took us three hours of googling and trying various approaches before we found it. I created the article to save everyone else from having to spend time looking for a similar solution.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
 |
|
 |
..this will come in handy some day, good work.
Last modified: 18hrs 30mins after originally posted -- Forgot to vote
My sins are bloody red, my coffee is a black hole.
|
|
|
|
 |