Introduction
The Gradient Enumerator is a simple utility to display resulting gradients from the combination of two colors.
The code is in C#.
I initially wrote the Gradient Enumerator as an aid in choosing gradient combinations for a larger application. It has proved to be quite useful, so I am sharing it in the hopes that some other folks will find it useful as well.
This version of the utility works with what Microsoft calls 'System-Defined' colors. As far as
code is concerned, these are the predefined colors in the Color structure of the System.Drawing namespace.
Code
The application consists of a single windows form.
The controls consist of a standard set of radio buttons allowing the user to change the direction of the gradient flow, two completely unnecessary buttons to display an about box and to quit the application, and two combo boxes allowing the user to select the two colors of the gradient.
The gradient itself is drawn as the background on the form, therefore, to have the controls be separate from the 'canvas', they sit on top of a panel which draws its own default background.
Upon initialization, the utility populates the combo boxes with the names of the available colors.
private void PopulateColors()
{
_firstColorCombo.Items.Add(Color.AliceBlue.Name);
_firstColorCombo.Items.Add(Color.AntiqueWhite.Name);
_firstColorCombo.Items.Add(Color.Aqua.Name);
...
_firstColorCombo.Items.Add(Color.Yellow.Name);
_firstColorCombo.Items.Add(Color.YellowGreen.Name);
foreach ( object color in _firstColorCombo.Items )
{
_secondColorCombo.Items.Add(color);
}
...
}
There are many colors, but since they are not defined as an 'enum', it isn't easy to iterate through the defined values within a loop, or to grab them all as a set. There may be ways to do this in a more elegant way, but since this was built to be more-or-less a throwaway application, the colors were added manually.
The only other piece of code of any interest is the method where the paining of the gradient is done. For this, we override the OnPaintBackground(...) method.
protected override void OnPaintBackground(PaintEventArgs pevent)
{
Graphics graphics = pevent.Graphics;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rectangle = ClientRectangle;
LinearGradientMode lgm = GetLinearGradientMode();
Color firstColor, secondColor;
GetChosenColors(out firstColor, out secondColor);
LinearGradientBrush brush = new LinearGradientBrush(
rectangle, firstColor, secondColor, lgm);
graphics.FillRectangle(brush, rectangle);
}
The code is very simple. A LinearGradientBrush is created to fill in the client area of the form, given by the ClientRectangle property, with the colors provided by the combo boxes, and the LinearGradientMode provided by the radio buttons.
The code returning the user chosen LinearGradientMode and colors is also simple. These are the two methods:
private LinearGradientMode GetLinearGradientMode()
{
if ( _radVertical.Checked ) return LinearGradientMode.Vertical;
else if ( _radHorizontal.Checked ) return LinearGradientMode.Horizontal;
else if ( _radForwardDiagonal.Checked ) return LinearGradientMode.ForwardDiagonal;
else if ( _radBackwardDiagonal.Checked ) return LinearGradientMode.BackwardDiagonal;
else return LinearGradientMode.Vertical;
}
private void GetChosenColors(out Color firstColor, out Color secondColor)
{
firstColor = Color.FromName((string)_firstColorCombo.SelectedItem);
secondColor = Color.FromName((string)_secondColorCombo.SelectedItem);
}
The ability of the Color structure to return a color given its name makes things very easy for us.
The only other code doing anything are the event handlers. Every time the user selects a new item in one of the color combo boxes, or chooses a different LinearGradientMode by clicking on a radio button, the form is forced to redraw with a call to Invalidate().
The utility can be extended by providing capability to render gradients from colors chosen via a color picker, or an arbitrary RGB value.
That's all...
Enjoy!
- Andrey Butov
- andreybutov@antair.com
- http://www.antair.com/andrey