Make a standard color palette in C#

The MakeColorPalette method shown below builds an array of PictureBoxes with BackColor properties set to a standard color palette.

// Make a color palette on the given parent.
private void MakeColorPalette(Control parent,
    int x, int y, System.EventHandler event_handler)
{
    Color[] colors = 
    {
        Color.White,
        Color.FromArgb(255, 255, 192, 192),
        Color.FromArgb(255, 255, 224, 192),
        Color.FromArgb(255, 255, 255, 192),
        Color.FromArgb(255, 192, 255, 192),
        Color.FromArgb(255, 192, 255, 255),
        Color.FromArgb(255, 192, 192, 255),
        Color.FromArgb(255, 255, 192, 255),
        Color.FromArgb(255, 224, 224, 224),
        Color.FromArgb(255, 255, 128, 128),
        Color.FromArgb(255, 255, 192, 128),
        Color.FromArgb(255, 255, 255, 128),
        Color.FromArgb(255, 128, 255, 128),
        Color.FromArgb(255, 128, 255, 255),
        Color.FromArgb(255, 128, 128, 255),
        Color.FromArgb(255, 255, 128, 255),
        Color.Silver,
        Color.Red,
        Color.FromArgb(255, 255, 128, 0),
        Color.Yellow,
        Color.Lime,
        Color.Cyan,
        Color.Blue,
        Color.Fuchsia,
        Color.Gray,
        Color.FromArgb(255, 192, 0, 0),
        Color.FromArgb(255, 192, 64, 0),
        Color.FromArgb(255, 192, 192, 0),
        Color.FromArgb(255, 0, 192, 0),
        Color.FromArgb(255, 0, 192, 192),
        Color.FromArgb(255, 0, 0, 192),
        Color.FromArgb(255, 192, 0, 192),
        Color.FromArgb(255, 64, 64, 64),
        Color.Maroon,
        Color.FromArgb(255, 128, 64, 0),
        Color.Olive,
        Color.Green,
        Color.Teal,
        Color.Navy,
        Color.Purple,
        Color.Black,
        Color.FromArgb(255, 64, 0, 0),
        Color.FromArgb(255, 128, 64, 64),
        Color.FromArgb(255, 64, 64, 0),
        Color.FromArgb(255, 0, 64, 0),
        Color.FromArgb(255, 0, 64, 64),
        Color.FromArgb(255, 0, 0, 64),
        Color.FromArgb(255, 64, 0, 64),
    };
    const int num_rows = 6;
    const int num_columns = 8;
    const int pic_width = 20;
    const int pic_height = 20;
    const int spacing = 4;

    int row_y = y;
    for (int row = 0; row < num_rows; row++)
    {
        int column_x = x;
        for (int column = 0; column < num_columns; column++)
        {
            PictureBox pic = new PictureBox();
            pic.Parent = parent;
            pic.Click += event_handler;
            pic.BackColor = colors[row * num_columns + column];
            pic.Size = new Size(pic_width, pic_height);
            pic.Location = new Point(column_x, row_y);
            pic.BorderStyle = BorderStyle.Fixed3D;
            column_x += pic_width + spacing;
        }
        row_y += pic_height + spacing;
    }
}

The code starts by defining an array containing the colors that should be in the palette. It then loops through the array building PictureBoxes for the colors. The code sets each PictureBox‘s:

  • Parent to the control that should contain the PictureBox. (Note that a form is a type of control so the program can pass that for the method’s parent parameter. Alternatively you could put the PictureBoxes in a TabControl or some other container.)
  • Click event handler. This is the delegate passed in by the calling code.
  • BackColor
  • Size
  • Location
  • BorderStyle

The code then updates the position for the next PictureBox and continues the loop.

When the form loads, the program simply calls the MakeColorPalette method.

private void Form1_Load(object sender, EventArgs e)
{
    MakeColorPalette(this, 10, 10, Color_Click);
}

When the user clicks one of the color PictureBoxes, the following event handler executes.

// The user clicked a color. Apply it.
private void Color_Click(object sender, EventArgs e)
{
    PictureBox pic = sender as PictureBox;
    this.BackColor = pic.BackColor;
}

This event handler converts the sender parameter into the PictureBox that raised the Click event. It then sets the form’s BackColor equal to the BackColor of the PictureBox.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in controls, drawing, graphics and tagged , , , , , , , , , , , . Bookmark the permalink.

One Response to Make a standard color palette in C#

  1. Pingback: Make a persistent color palette in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *