Share:

Color Palette In C# | WPF | Saatody | Amit Padhiyar

There are three class(es) in color palette tool. 1] ColorPalette, 2] ColorCell, 3] AddColorCell. Today, I will write reference and example about color palette. This tool is available for private use only. And the API is written in WPF C#.

1] ColorPalette Class API

This is a main class of my color palette tool. This class provides functionalities for set current color, add new cell, remove exist cell, and remove all exist cells. Also has an event fire while current color change. We also set functionalities of importing and exporting ColorPalette.

Properties

This property stores color and also fire an event CurrentColorChanged.

Syntax

public Color CurrentColor

Constructors

This is one and default constructor.

Syntax

public ColorPalette()

Methods

These methods are used for adding and removing ColorCell from the ColorPalette. Also used for importing and exporting ColorPalette.

Syntax

public void AddColorCell(ColorCell ColorCell)
public void RemoveColorCell(ColorCell ColorCell)
public void ClearColorCells()

public void ImportColorPalette()
public void ExportColorPalette()

Event

Here is only an event CurrentColorChanged. This event fire when CurrentColor property value being changed.

Syntax

public event EventHandler CurrentColorChanged;

2] ColorCell Class API

This class hold ColorPalette object as owner (parent). There is a property to set and get color in cell. And also some advance functionalities to add color in cell and clear color from cell. Second, Remove cell from ColorPalette. These three options will appear while you right click on cell and context menu opened. When you left click on cell then the current color of ColorPalette will store in ColorCell. We also set shortcut keys for adding, removing color and removing cell.

Properties

There are two properties in ColorCell. One is used to set and get ColorPalette as owner. And second is to set and get color in ColorCell.

Syntax

public ColorPalette ColorPalette
public Color StoredColor

Constructors

This is one and default constructor.

Syntax

public ColorCell()

3] AddColorCell Class API

If you want add ColorCell in ColorPalette using GUI then this is a button which can add new ColorCell without API.

Properties

Here is only one property used to set and get ColorPalette as owner.

Syntax

public ColorPalette ColorPalette

Constructors

This is one and default constructor.

Syntax

public AddColorCell()

Example Of Color Palette Tool

ColorPalette ColorPalette = new ColorPalette();
ColorPalette.ClearColorCells();

// Add AddColorCell First.
// Used For Add ColorCell Using GUI.
// AddColorCell Button Is Already Added In ColorPalette By Default.
// But If You Call ColorPalette.ClearColorCells() Method Then All The ColorCell(s) Will Be Remove And The AddColorCell Will Be Also Remove.
// So Add Manually Again.
AddColorCell AddColorCell = new AddColorCell();
AddColorCell.ColorPalette = ColorPalette;
ColorPalette.Children.Add(AddColorCell);

// Predefined Color Cells.
// Used For Add ColorCell Using API.
for (int i = 0; i < 10; i++)
{
    Color Color = Colors.Transparent;
    if (i == 0) { Color = Colors.Red; }
    if (i == 1) { Color = Colors.Yellow; }
    if (i == 2) { Color = Color.FromRgb(240, 240, 240); }
    if (i == 3) { Color = Colors.GreenYellow; }
    if (i == 4) { Color = Colors.Green; }
    if (i == 5) { Color = Colors.Lime; }
    if (i == 6) { Color = Colors.Violet; }
    if (i == 7) { Color = Colors.Blue; }
    if (i == 8) { Color = Colors.CadetBlue; }
    if (i == 9) { Color = Colors.Cyan; }
    
    ColorCell ColorCell = new ColorCell();
    ColorCell.ColorPalette = ColorPalette;
    ColorCell.StoredColor = Color;
    ColorPalette.AddColorCell(ColorCell);
}

// Event Called When Current Color Changed.
ColorPalette.CurrentColorChanged += (sender, e) =>
{
    // Get Current Color Of ColorPalette.
    ColorViewer.Background = new SolidColorBrush(ColorPalette.CurrentColor);
};

Comments

Popular posts from this blog

Get Color From Pixel C# WPF | Saatody | Amit Padhiyar

Basic Audio Operations With MP3 And Wave Files Using NAudio C#

Create Drag And Drop Operation Between DevExpress GridControl And Custom WPF UI | Saatody | Amit Padhiyar