Functions to Convert ARGB/RGB to a Windows.UI.Xaml.Media.Brush


Below I will provide C# functions to convert a color defined by it's RGB or ARGB colors into a Brush that can be used in Windows Universal Apps (UWP).

C#

/// <summary>
/// Returns a Brush for the given ARGB values.
/// </summary>
/// <param name="a">Alpha</param>
/// <param name="r">Red</param>
/// <param name="g">Blue</param>
/// <param name="b">Green</param>
public static Brush ConvertColor(int a, int r, int g, int b)
{
    return new SolidColorBrush(Windows.UI.Color.FromArgb(Convert.ToByte(a), Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b)));            
}

/// <summary>
/// Returns a Brush for the given ARGB values.
/// </summary>
/// <param name="r">Red</param>
/// <param name="g">Blue</param>
/// <param name="b">Green</param>
public static Brush ConvertColor(int r, int g, int b)
{
    return new SolidColorBrush(Windows.UI.Color.FromArgb(Convert.ToByte(255), Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b)));
}

The code above will require the following Using statements at the top of your code file:

using System;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.