Skip to content

Instantly share code, notes, and snippets.

@patridge
Created June 16, 2015 19:03
Show Gist options
  • Select an option

  • Save patridge/0a7f174aa46c50ab4b0f to your computer and use it in GitHub Desktop.

Select an option

Save patridge/0a7f174aa46c50ab4b0f to your computer and use it in GitHub Desktop.
Xamarin Color Helper
// If you don't want to pull the whole [Splat](https://github.com/paulcbetts/splat) library over just for colors,
// here are a couple functions to help.
// Store your color values as hex-based `long`s and just convert them to native colors when you use them.
// NOTE: iOS uses RGBA floats (0..1) and Android uses ARGB ints (0..255) when converting.
// Usage example:
SomeView.SetBackgroundColor(MyAppColors.SomeGrayHighlight.AsNative());
// Core library can hold your colors:
public static class MyAppColors {
public const long SomeGrayHighlight = 0xffcccccc;
// ... the rest of your favorit colors ...
}
// Android conversion extension:
public static class ColorExtensions {
public static Color AsNative(this long hexValue) {
return Color.Argb(
((int)(hexValue & 0xff000000) >> 24),
((int)(hexValue & 0xff0000) >> 16),
((int)(hexValue & 0xff00) >> 8),
((int)(hexValue & 0xff)));
}
}
// iOS conversion extensions:
public static UIColor AsNative(this long hexValue) {
return UIColor.FromRGBA(
(((float)((hexValue & 0xff0000) >> 16)) / 255.0f),
(((float)((hexValue & 0xff00) >> 8)) / 255.0f),
(((float)((hexValue & 0xff))) / 255.0f),
(((float)((hexValue & 0xff000000) >> 24)) / 255.0f));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment