Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get a brush from the Brushes class?

Tags:

c#

brush

brushes

I have a property that allows the string name of a known colour to be sent to my control. The property only accepts proper known colour names, like "Red" or "Blue"

  private KnownColor _UseColor = KnownColor.Red;

    /// <summary>
    /// Gets or sets the name of the colour
    /// </summary>
    public string ColorName
    {
        get
        {
            return this._UseColor.ToString();
        }
        set
        {
            if (Enum.IsDefined(typeof(KnownColor), value))
                this._UseColour = (KnownColor)Enum.Parse(typeof(KnownColor), value);
        }
    }

And what I want to do is use this _UseColour enumeration to select an existing brush from the static Brushes class in .NET like this

Brush sysBrush = Brushes.FromKnownColor(this._UseColor);
e.Graphics.FillRectangle(sysBrush, 0, 0, 10, 10);

Instead of creating a new brush whenever the control is painted like this

using (SolidBrush brsh = new SolidBrush(Color.FromKnownColor(this._UseColor)))
    e.Graphics.FillRectangle(brsh, 0, 0, 10, 10);

Does anyone know if this is possible or will I have to create a new brush every time?

Brushes.FromKnownColor isn't a method in the Brushes class

like image 448
jcharlesworthuk Avatar asked Feb 28 '13 11:02

jcharlesworthuk


3 Answers

Why not create the brush once and cache it for later use?

In your main class:

private KnownColor _UseColor = KnownColor.Red;

/// <summary>
/// Gets or sets the name of the colour
/// </summary>
public string ColorName
{
    get
    {
        return this._UseColor.ToString();
    }
    set
    {
        if (Enum.IsDefined(typeof(KnownColor), value))
            this._UseColour = (KnownColor)Enum.Parse(typeof(KnownColor), value);
    }
}

private Dictionary<string, Brush> _knownBrushes = new Dictionary<string, Brush>();

public Brush ColorBrush
{
    get
    {
        if (!_knownBrushes.ContainsKey(_UseColor)) {
            _knownBrushes[_UseColor] = new SolidBrush(Color.FromKnownColor(this._UseColor));
        }

        return _knownBrushes[_UseColor];
    }
}

then use it like..

e.Graphics.FillRectangle(ColorBrush, 0, 0, 10, 10);
like image 54
Rudi Visser Avatar answered Sep 19 '22 15:09

Rudi Visser


The reflection approach

var properties = typeof (Brushes).GetProperties();
var property = properties.FirstOrDefault(p => p.Name == "Red");
var brush = property.GetValue(null, null); // Contains Brushes.Red

Your case

Field:

PropertyInfo[] _properties = typeof (Brushes).GetProperties();

Static method

static Brush GetKnownBrush(string knownColorName)
{
    var property = _properties.FirstOrDefault(p => p.Name == knownColorName);
    var brush = property.GetValue(null, null);
    return brush;
}

Usage:

var knownBrush = GetKnownBrush(ColorName);

Instance property

Brush KnownBrush
{
    get
    {
        var property = _properties.FirstOrDefault(p => p.Name == ColorName);
        var brush = property.GetValue(null, null);
        return brush;
    }
}

Usage:

var knownBrush = KnownBrush;

You can also store frequently utilized brushes in a dictionary in order to avoid reflection actions.

like image 40
Ryszard Dżegan Avatar answered Sep 21 '22 15:09

Ryszard Dżegan


If you want a solution that can look up the brush by a color, even if the color might not have a known name, you can create a dictionary that uses the color:

void Main()
{
    var brush = KnownBrush(Color.FromArgb(255, 0, 0));
    brush.Dump();
}

private static Dictionary<Tuple<byte, byte, byte, byte>, SolidBrush> _KnownBrushes;
public static SolidBrush KnownBrush(Color color)
{
    if (_KnownBrushes == null)
    {
        _KnownBrushes = new Dictionary<Tuple<byte, byte, byte, byte>, SolidBrush>();
        foreach (var propertyInfo in typeof(Brushes).GetProperties())
        {
            if (propertyInfo.PropertyType == typeof(Brush))
            {
                var brush = propertyInfo.GetValue(null) as SolidBrush; // not a typo
                if (brush != null)
                    _KnownBrushes[Tuple.Create(brush.Color.R, brush.Color.G, brush.Color.B, brush.Color.A)] = brush;
            }
        }
    }

    SolidBrush result;
    _KnownBrushes.TryGetValue(Tuple.Create(color.R, color.G, color.B, color.A), out result);
    return result;
}
like image 20
Lasse V. Karlsen Avatar answered Sep 18 '22 15:09

Lasse V. Karlsen