More Xamarin.Forms.Color functions

Hi all! When designing en app and implementing styles it can be useful to control and transform your Color resources. Here are some extension functions to help you with that :

Extensions

This class contains all the extensions to manipulate your Xamarin.Forms.Color object.

https://github.com/framinosona/Xamarin-Developer-Sample/blob/master/DeveloperSample.Core/Helpers/ColorExtensions.cs

public static class ColorExtensions
{
    #region ToString
    public static string ToRgbString(this Color c)
    public static string ToRgbaString(this Color c)
    public static string ToHexRgbString(this Color c)
    public static string ToHexRgbaString(this Color c)
    public static string ToCmyString(this Color c)
    public static string ToCmykString(this Color c)
    #endregion

    #region Setters
    public static Color WithRed(this Color baseColor, double newR)
    public static Color WithGreen(this Color baseColor, double newG)
    public static Color WithBlue(this Color baseColor, double newB)
    public static Color WithAlpha(this Color baseColor, double newA)
    public static Color WithCyan(this Color baseColor, double newC)
    public static Color WithMagenta(this Color baseColor, double newM)
    public static Color WithYellow(this Color baseColor, double newY)
    #endregion

    #region Getters Byte
    public static byte GetByteRed(this Color c)
    public static byte GetByteGreen(this Color c)
    public static byte GetByteBlue(this Color c)
    public static byte GetByteAlpha(this Color c)
    public static byte GetByteCyan(this Color c)
    public static byte GetByteMagenta(this Color c)
    public static byte GetByteYellow(this Color c)
    #endregion

    #region Getters Hex
    public static int GetHexR(this Color c)
    public static int GetHexG(this Color c)
    public static int GetHexB(this Color c)
    public static int GetHexA(this Color c)
    #endregion

    #region Getters double
    public static double GetBlackKey(this Color c)
    public static double GetCyan(this Color c)
    public static double GetMagenta(this Color c)
    public static double GetYellow(this Color c)
    #endregion

    #region Converters
    public static Color Inverse(this Color baseColor)
    public static Color ToBlackOrWhite(this Color baseColor)
    public static Color ToBlackOrWhiteForText(this Color baseColor)
    public static Color ToGrayScale(this Color baseColor)
    #endregion

    #region Booleans
    public static bool IsDarkForTheEye(this Color c)
    public static bool IsDark(this Color c)
    #endregion
}

Helper

When manipulating colors it is important to be able to transform/convert a double into a Byte :

public static byte EnsureByte(double input)
{
    if (input < 0) return 0;
    if (input > 255) return 255;
    return (byte) Math.Floor(input);
}

Converters

ColorToColorComponentConverter

https://github.com/framinosona/Xamarin-Developer-Sample/blob/master/DeveloperSample.Core/Converters/ColorToColorComponentConverter.cs

ColorToCyanConverter
ColorToMagentaConverter
ColorToYellowConverter
ColorToBlackKeyConverter

ColorToColorConverter

https://github.com/framinosona/Xamarin-Developer-Sample/blob/master/DeveloperSample.Core/Converters/ColorToColorConverter.cs

ColorToBlackOrWhiteConverter
ColorToColorForTextConverter
ColorToGrayScaleColorConverter
ColorToInverseColorConverter

ColorToStringConverter

https://github.com/framinosona/Xamarin-Developer-Sample/blob/master/DeveloperSample.Core/Converters/ColorToStringConverter.cs

ColorToRgbStringConverter
ColorToRgbaStringConverter
ColorToHexRgbStringConverter
ColorToHexRgbaStringConverter
ColorToCmyStringConverter
ColorToCmykStringConverter

The XAML ResourceDictionary

<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    xmlns:converters="clr-namespace:DeveloperSample.Core.Converters;assembly=DeveloperSample.Core"
                    x:Class="DeveloperSample.Core.Styles.ConvertersResourceDictionary">

    <converters:ColorToInverseColorConverter x:Key="ColorToInverseColorConverter" />
    <converters:ColorToBlackOrWhiteConverter x:Key="ColorToBlackOrWhiteConverter" />
    <converters:ColorToGrayScaleColorConverter x:Key="ColorToGrayScaleColorConverter" />
    <converters:ColorToColorForTextConverter x:Key="ColorToColorForTextConverter" />

    <converters:ColorToRgbaStringConverter x:Key="ColorToRgbaStringConverter" />
    <converters:ColorToRgbStringConverter x:Key="ColorToRgbStringConverter" />
    <converters:ColorToHexRgbaStringConverter x:Key="ColorToHexRgbaStringConverter" />
    <converters:ColorToHexRgbStringConverter x:Key="ColorToHexRgbStringConverter" />
    <converters:ColorToCmyStringConverter x:Key="ColorToCmyStringConverter" />
    <converters:ColorToCmykStringConverter x:Key="ColorToCmykStringConverter" />

    <converters:ColorToCyanConverter x:Key="ColorToCyanConverter" />
    <converters:ColorToMagentaConverter x:Key="ColorToMagentaConverter" />
    <converters:ColorToYellowConverter x:Key="ColorToYellowConverter" />
    <converters:ColorToBlackKeyConverter x:Key="ColorToBlackKeyConverter" />

</ResourceDictionary>

I hope the names are self-explanatory but if you need any explanation let me know !

Hope it helped 😉