UI, Xamarin Essentials

Converting Colors in Xamarin Forms

This time, I want to share with you this great feature. Let me tell you something, when I saw this functionality I really LOVED IT ?  !!! Because I can play with the colors! ?

In this post, we will be learning about the different functionalities that Xamarin Essentials bring to in the System.Drawing.Color library. This explanation will be divided in the following topics:

? Using color formats

? Transforming color aspects


First of all… What do I need?

  • Add from NuGet Package the plugin: Xamarin.Essentials

You can read more information about Xamarin Essential here.


Let’s start!!!

Using color formats

Xamarin Essential created the ColorConverts class for us. With this class we can convert the colors to different types. We can use the following:

FromHex      ➡ Receives a string as a Hexadecimal value.

FromHls       ➡ Receives three float values as parameters: Hue, Saturation  and Luminosity.

FromHsla     ➡ Receives three float values as parameters: Hue, Saturation  and Luminosity. And one parameters as an Int value: Alpha.


Transforming color aspects

This is my favorite topic ?. System.Drawing.Color has the extension methods that allow us to play with our color appearance with no other plugin required?. We can handle some functionality named color extensions. Let’s see my favorites!

? WithAlpha                                 ? WithLuminosity

? WithHue                                    ? WithSaturation

? AddLuminosity

To use it, you just must call the color extension type you just add the color next to the extension wished.

Let’s see an example in code

Let’s see a simple example

In the following example, I used the same color with difference changes of color extensions but with the same value as parameter ( 30 ), with this way we can appreciate the difference.

XAML

C# code

Tagged , , ,

3 thoughts on “Converting Colors in Xamarin Forms

  1. You saved my life, I needed this to fix some labels in dark / light mode.
    I come from angular / react JS, I am new to xamarin.
    Thanks a lot!!

Leave a Reply

Your email address will not be published. Required fields are marked *