Click here to Skip to main content
15,881,852 members
Articles / Desktop Programming / Windows Forms
Article

Gradient Enumerator

Rate me:
Please Sign up or sign in to vote.
1.57/5 (5 votes)
31 Aug 20052 min read 24.4K   331   12   2
The Gradient Enumerator is a simple utility to display resulting gradients from the combination of two colors.

Sample Image - gradient_enumerator.jpg

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionImprovements? Pin
Axel Rietschin31-Aug-05 18:58
professionalAxel Rietschin31-Aug-05 18:58 
AnswerRe: Improvements? Pin
Andrey Butov1-Sep-05 3:26
Andrey Butov1-Sep-05 3:26 

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.