diff options
author | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 13:02:25 -0700 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 16:13:41 -0700 |
commit | 17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch) | |
tree | b5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Platform.Android/ColorExtensions.cs | |
download | xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2 xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip |
Initial import
Diffstat (limited to 'Xamarin.Forms.Platform.Android/ColorExtensions.cs')
-rw-r--r-- | Xamarin.Forms.Platform.Android/ColorExtensions.cs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Android/ColorExtensions.cs b/Xamarin.Forms.Platform.Android/ColorExtensions.cs new file mode 100644 index 00000000..afe7286d --- /dev/null +++ b/Xamarin.Forms.Platform.Android/ColorExtensions.cs @@ -0,0 +1,40 @@ +using Android.Content.Res; +using AColor = Android.Graphics.Color; + +namespace Xamarin.Forms.Platform.Android +{ + public static class ColorExtensions + { + static readonly int[][] ColorStates = { new[] { global::Android.Resource.Attribute.StateEnabled }, new[] { -global::Android.Resource.Attribute.StateEnabled } }; + + public static AColor ToAndroid(this Color self) + { + return new AColor((byte)(byte.MaxValue * self.R), (byte)(byte.MaxValue * self.G), (byte)(byte.MaxValue * self.B), (byte)(byte.MaxValue * self.A)); + } + + public static AColor ToAndroid(this Color self, int defaultColorResourceId) + { + if (self == Color.Default) + { + using(Resources resources = Resources.System) + return resources.GetColor(defaultColorResourceId); + } + + return ToAndroid(self); + } + + public static AColor ToAndroid(this Color self, Color defaultColor) + { + if (self == Color.Default) + return defaultColor.ToAndroid(); + + return ToAndroid(self); + } + + public static ColorStateList ToAndroidPreserveDisabled(this Color color, ColorStateList defaults) + { + int disabled = defaults.GetColorForState(ColorStates[1], color.ToAndroid()); + return new ColorStateList(ColorStates, new[] { color.ToAndroid().ToArgb(), disabled }); + } + } +}
\ No newline at end of file |