Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#
Tip/Trick

How To Use Any Color In Your C# Apps

Rate me:
Please Sign up or sign in to vote.
1.30/5 (6 votes)
8 Feb 2016CPOL1 min read 11K   2   2
Step-by-step instructions for an easy way to use any color in a C# app

Pick a Color, Any Color (or Several Colors, For That Matter)

If you want to use colors in your app other than the standard ones for which there are constants defined, you can create your own using Color.FromArgb(), which returns a color comprised of the combination of values you pass (the first arg is the amount of red in your custom color, the second is the amount of green, and the final is the amount of blue. e.g., passing 14, 56, and 46, respectively, gives you a forest green color.

If you want to mimic an existing color or color scheme, you first need an image that contains that or those colors. If you don't have one, you can take a screenshot from a web page that does, and save that as an image file.

Next, load the image file into http://html-color-codes.info/colors-from-image/.

Then, show the loaded image in that page, and click on the area of the image that contains the color of interest

Next, copy the hex value provided, and then convert it to its RGB value by pasting it into the "Hex" box at http://hex.colorrrs.com/.

Now, copy the RGB value from there, and create a constant for the color, such as:

C#
private static readonly Color GREEN_BAY_PACKERS_GREEN = Color.FromArgb(14,56,46); //#0E382E;

Finally, call it from your C# app like so:

C#
grandTotalsItemsRange.Interior.Color = GREEN_BAY_PACKERS_GREEN;

Of course, the usefulness of this is not limited to colorizing the background of Excel ranges; you can assign the color to any object's Color property, such as for text, etc. -- any place you assign to a Color property.

Like Sunlight Streaming Through Evergreens

As a bonus, here's another great color:

C#
private static readonly Color GREEN_BAY_PACKERS_GOLD = Color.FromArgb(240,184,1); //#F0B801;

License

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


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
GeneralNot just C# Pin
Middle Manager9-Feb-16 6:31
Middle Manager9-Feb-16 6:31 
GeneralRe: Not just C# Pin
B. Clay Shannon9-Feb-16 6:37
professionalB. Clay Shannon9-Feb-16 6:37 

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.