Click here to Skip to main content
Click here to Skip to main content

Calculating Textcolor Contrast

, 6 Feb 2011
Rate this:
Please Sign up or sign in to vote.
How to finde maximum Contrast of a Webcolor
The simple snippets of code calculates the text color (contrast) of a background color.
 
string getContrast(string hexcolor)
{
int r = Convert.ToInt32("0x"+hexcolor.Substring(0,2), 16);
int g = Convert.ToInt32("0x"+hexcolor.Substring(2,2), 16);
int b = Convert.ToInt32("0x"+hexcolor.Substring(4,2), 16);
int yiq = ((r*299)+(g*587)+(b*114))/1000;
if(yiq >= 131.5)
return "black";
else
return "white";
}
 
The code is based on the following Blogentry:
http://24ways.org/2010/calculating-color-contrast[^].

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

stastka
Web Developer
Switzerland Switzerland
No Biography provided
Follow on   Twitter

Comments and Discussions

 
GeneralFixed the link. PinmemberSteve Maier3-Feb-11 8:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 7 Feb 2011
Article Copyright 2011 by stastka
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid