I.Step
I.Step

Reputation: 779

Convert MaterialDesignColors.MaterialDesignColor to SolidColorBrush?

I am using this MaterialDesign library in my C# WPF application.

How can I programatically get the HEX color ID for example from the color MaterialDesignColors.MaterialDesignColor.LightBlue500 and then convert it to a SolidColorBrush?

Upvotes: 0

Views: 379

Answers (1)

thatguy
thatguy

Reputation: 22079

In general, you can get any MaterialDesignColor using the SwatchHelper.

var lightBlue500Color = SwatchHelper.Lookup[MaterialDesignColor.LightBlue500];

You can also get it directly from the corresponding swatch, here LightBlueSwatch.

var lightBlue500Color = LightBlueSwatch.LightBlue500;
var lightBlueSwatch = new LightBlueSwatch();
var lightBlue500Color = lightBlueSwatch.Lookup[MaterialDesignColor.LightBlue500];

From that color, you can create a SolidColorBrush using its constructor.

var lightBlue500SolidColorBrush = new SolidColorBrush(lightBlue500Color);

If you need the hex color string in #AARRGGBB format, you can use the ToString method of Color.

var lightBlue500HexString= LightBlueSwatch.LightBlue500.ToString(); // = "#FF03A9F4"

If you need the #RRGGBB format, you can use a custom format string.

var lightBlue500Color = LightBlueSwatch.LightBlue500;
var lightBlue500HexString = string.Format("#{0:X2}{1:X2}{2:X2}", lightBlue500Color.R, lightBlue500Color.G, lightBlue500Color.B); // = "#03A9F4"

In general, if you want to create a solid color brush from a hex string, you can do this.

var lightBlue500Color = (Color)ColorConverter.ConvertFromString(#FF03A9F4);
var lightBlue500SolidColorBrush = new SolidColorBrush(lightBlue500Color);

Upvotes: 1

Related Questions