Xamarin.Essentials: Color Converters

The ColorConverters class in Xamarin.Essentials provides several helper methods for System.Drawing.Color.

Get started

To start using this API, read the getting started guide for Xamarin.Essentials to ensure the library is properly installed and set up in your projects.

Using Color Converters

Add a reference to Xamarin.Essentials in your class:

using Xamarin.Essentials;

When working with System.Drawing.Color you can use the built in converters of Xamarin.Forms to create a color from Hsl, Hex, or UInt.

var blueHex = ColorConverters.FromHex("#3498db");
var blueHsl = ColorConverters.FromHsl(204, 70, 53);
var blueUInt = ColorConverers.FromUInt(3447003);

Using Color Extensions

Extension methods on System.Drawing.Color enable you to apply different properties:

var blue = ColorConverters.FromHex("#3498db");

// Multiplies the current alpha by 50%
var blueWithAlpha = blue.MultiplyAlpha(.5f);

There are several other extension methods including:

  • ToUInt
  • MultiplyAlpha
  • WithHue
  • WithAlpha
  • WithSaturation
  • WithLuminosity

Using Platform Extensions

Additionally, you can convert System.Drawing.Color to the platform specific color structure. These methods can only be called from the iOS, Android, and UWP projects.

var system = System.Drawing.Color.FromArgb(255, 52, 152, 219);
// Extension to convert to Android.Graphics.Color, UIKit.UIColor, or Windows.UI.Color
var platform = system.ToPlatformColor();
var platform = new Android.Graphics.Color(52, 152, 219, 255);
// Back to System.Drawing.Color
var system = platform.ToSystemColor();

The ToSystemColor method applies to Android.Graphics.Color, UIKit.UIColor, and Windows.UI.Color.