Click here to Skip to main content
11,932,048 members (54,878 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked

Calculating Textcolor Contrast

, 6 Feb 2011 CPOL
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";
return "white";

The code is based on the following Blogentry:[^].


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


About the Author

Web Developer
Switzerland Switzerland
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralFixed the link. Pin
Steve Maier3-Feb-11 9:09
memberSteve Maier3-Feb-11 9:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 7 Feb 2011
Article Copyright 2011 by stastka
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid