Little Known, Invaluable Methods and Properties in the .NET Framework Base Class Library : Working with ColorsBy Scott Mitchell
In an earlier 4Guys article - Little Known, Invaluable Methods and Properties in the .NET Framework Base Class Library : Working with File Paths - I introduced a number of little known, but highly useful methods for working with file paths. In the previous article I promised to continue forward by introducing additional "little known, invaluable methods and properties in the .NET Framework Base Class Library," asking for suggestions on what methods/properties to touch on. Over the past week I have received numerous suggestions from both the 4Guys feedback form and an associated blog entry. Thanks, and keep those suggestions coming in!
Today's installment will look at methods and properties useful for programmatically working with colors. ASP.NET is a technology that blends together the front-end design (HTML) with the back-end guts (the data store and server-side data models, custom objects, business logic, and so on). Not surprisingly, there are times when we need to programmatically work with the colors that are to be rendered in the user's browser. For example, the Conditional Formatting with the DataGrid FAQ illustrates how to programmatically set the background color for a DataGrid row based on the database values bound to that particular row.
This article examines various methods and properties for programmatically working with colors in a Web application. As always, if you know of any .NET Framework methods or properties you consider not in every ASP.NET developer's lexicon, but should be, please let me know, as each week I plan to add additional useful methods and properties focused in a particular area. This week's tips and tricks focus on working with colors. Read on to learn more!
I plan on adding additional topics to this list. The first topic was Working with File Paths. Today's topic covers working with colors.
|Little Known, Invaluable Methods and Properties in the .NET Framework BCL|
|<-- Previous (Working with File Paths)||Working with Colors|
Representing Colors 101
If you recall from your elementary school days, the three additive primary colors are red, green, and blue. These three colors, when combined in equal quantities, display white. Each pixel in a computer monitor emits a degree of red, green, and blue colors to display a particular color. On web pages, colors are defined as a mix of red, green, and blue values (the legal values for each of the three additive primary colors being 0 through 255). For example, the color white can be defined as a value of 255 from both red, green, and blue; black is 0 for these three components. Bright red would be a red value of 255, and 0 for green and blue, yellow is 255 for red and green, and 0 for blue... and so on. (There are numerous sites across the web that show RGB definitions of common colors as well as various aesthetically-pleasing color combinations, defined in their RGB syntax; see the VisiBone Color Laboratory for one such example.)
In web pages a color can be defined using one of three techniques:
Programmatically Working with Colors
A number of ASP.NET Web controls have properties that dictate the color of the rendered control in the user's browser. For example, all Web controls have the
The static properties of the
|Color.FromArgb(red, green, blue)||
If you know the red, green, and blue components for the color you want to create, you can generate a
A couple things to note: by default, ASP.NET will not render a background color for a TextBox for down-level browsers
(non-IE browsers), although you can redefine modern, non-IE browsers to be considered uplevel. See A
Look at ASP.NET's Adaptive Rendering for more details. For uplevel browsers the TextBox's rendered HTML will be:
Also realize that the
There may be times where you have a color represented in either the hexidecimal format or as a named color,
and you want to work with this color as a
In your ASP.NET pages you would want to read in these unique color settings and apply them. When assigning these
colors to Web controls, however, we have the challenge of converting the string to a
Fortunately we don't have to tinker around at this level. The
The following code snippet shows how to use
There are a slew of methods and properties in the .NET Framework Base Class Library that, while quite useful and versatile, are not well as well-known among the ASP.NET developer community as they ought to be. This article is an attempt in providing information about methods and properties that most ASP.NET developers will find quite useful in real-world situations with regards to working with colors. An earlier article looked at such methods/properties for working with file paths. If you have any recommendations on methods or properties that you find invaluable, but that you noticed not many folks know about, please don't hesitate to let me know!