From 010982b08a8bf693f915f123f806e9ffe8ac2d41 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Wed, 25 Apr 2018 22:23:26 -0700 Subject: [PATCH] Use standard colors for all controls --- Ooui.Forms/Cells/EntryCellRenderer.cs | 2 +- Ooui.Forms/Cells/TextCellRenderer.cs | 8 ++++---- Ooui.Forms/Extensions/ColorExtensions.cs | 20 ++++++++------------ Ooui.Forms/OouiTheme.cs | 14 ++++++++++++++ Ooui.Forms/Renderers/BoxRenderer.cs | 6 +----- Ooui.Forms/Renderers/ButtonRenderer.cs | 9 ++------- Ooui.Forms/Renderers/EditorRenderer.cs | 7 +------ Ooui.Forms/Renderers/EntryRenderer.cs | 10 +--------- Ooui.Forms/Renderers/FrameRenderer.cs | 12 ++++++------ Ooui.Forms/Renderers/LabelRenderer.cs | 5 +---- Ooui.Forms/Renderers/LinkLabelRenderer.cs | 5 +---- Ooui.Forms/Renderers/PickerRenderer.cs | 2 +- Ooui.Forms/Renderers/ViewRenderer.cs | 8 +------- Ooui.Forms/VisualElementRenderer.cs | 7 +------ Ooui/Color.cs | 8 ++++++++ 15 files changed, 51 insertions(+), 72 deletions(-) create mode 100644 Ooui.Forms/OouiTheme.cs diff --git a/Ooui.Forms/Cells/EntryCellRenderer.cs b/Ooui.Forms/Cells/EntryCellRenderer.cs index f0edb6a..ca3f38a 100644 --- a/Ooui.Forms/Cells/EntryCellRenderer.cs +++ b/Ooui.Forms/Cells/EntryCellRenderer.cs @@ -76,7 +76,7 @@ namespace Ooui.Forms.Cells private static void UpdateLabelColor(CellView cell, EntryCell entryCell) { - cell.TextLabel.Style.Color = entryCell.LabelColor.ToOouiColor(); + cell.TextLabel.Style.Color = entryCell.LabelColor.ToOouiColor(OouiTheme.TextColor); } private static void UpdatePlaceholder(CellView cell, EntryCell entryCell) diff --git a/Ooui.Forms/Cells/TextCellRenderer.cs b/Ooui.Forms/Cells/TextCellRenderer.cs index 55daab5..56126aa 100644 --- a/Ooui.Forms/Cells/TextCellRenderer.cs +++ b/Ooui.Forms/Cells/TextCellRenderer.cs @@ -20,8 +20,8 @@ namespace Ooui.Forms.Cells nativeTextCell.TextLabel.Text = textCell.Text ?? string.Empty; nativeTextCell.DetailTextLabel.Text = textCell.Detail ?? string.Empty; - nativeTextCell.TextLabel.Style.Color = textCell.TextColor.ToOouiColor(); - nativeTextCell.DetailTextLabel.Style.Color = textCell.DetailColor.ToOouiColor(); + nativeTextCell.TextLabel.Style.Color = textCell.TextColor.ToOouiColor(OouiTheme.TextColor); + nativeTextCell.DetailTextLabel.Style.Color = textCell.DetailColor.ToOouiColor(OouiTheme.SecondaryTextColor); WireUpForceUpdateSizeRequested(item, nativeTextCell); @@ -40,9 +40,9 @@ namespace Ooui.Forms.Cells else if (args.PropertyName == TextCell.DetailProperty.PropertyName) tvc.DetailTextLabel.Text = textCell.Detail ?? string.Empty; else if (args.PropertyName == TextCell.TextColorProperty.PropertyName) - tvc.TextLabel.Style.Color = textCell.TextColor.ToOouiColor(); + tvc.TextLabel.Style.Color = textCell.TextColor.ToOouiColor(OouiTheme.TextColor); else if (args.PropertyName == TextCell.DetailColorProperty.PropertyName) - tvc.DetailTextLabel.Style.Color = textCell.DetailColor.ToOouiColor(); + tvc.DetailTextLabel.Style.Color = textCell.DetailColor.ToOouiColor(OouiTheme.SecondaryTextColor); } } } diff --git a/Ooui.Forms/Extensions/ColorExtensions.cs b/Ooui.Forms/Extensions/ColorExtensions.cs index f3bab76..b3a7b80 100644 --- a/Ooui.Forms/Extensions/ColorExtensions.cs +++ b/Ooui.Forms/Extensions/ColorExtensions.cs @@ -4,31 +4,27 @@ namespace Ooui.Forms.Extensions { public static class ColorExtensions { - public static Color ToOouiColor (this Xamarin.Forms.Color color) + static Color ToOouiColor (ref Xamarin.Forms.Color color) { - const byte defaultRed = 0; - const byte defaultGreen = 0; - const byte defaultBlue = 0; - const byte defaultAlpha = 255; - byte r = color.R < 0 ? defaultRed : (byte)(color.R * 255.0 + 0.5); - byte g = color.G < 0 ? defaultGreen : (byte)(color.G * 255.0 + 0.5); - byte b = color.B < 0 ? defaultBlue : (byte)(color.B * 255.0 + 0.5); - byte a = color.A < 0 ? defaultAlpha : (byte)(color.A * 255.0 + 0.5); + byte r = (byte)(color.R * 255.0 + 0.5); + byte g = (byte)(color.G * 255.0 + 0.5); + byte b = (byte)(color.B * 255.0 + 0.5); + byte a = (byte)(color.A * 255.0 + 0.5); return new Color (r, g, b, a); } public static Color ToOouiColor (this Xamarin.Forms.Color color, Xamarin.Forms.Color defaultColor) { if (color == Xamarin.Forms.Color.Default) - return defaultColor.ToOouiColor (); - return color.ToOouiColor (); + return ToOouiColor (ref defaultColor); + return ToOouiColor (ref color); } public static Color ToOouiColor (this Xamarin.Forms.Color color, Ooui.Color defaultColor) { if (color == Xamarin.Forms.Color.Default) return defaultColor; - return color.ToOouiColor (); + return ToOouiColor (ref color); } } } diff --git a/Ooui.Forms/OouiTheme.cs b/Ooui.Forms/OouiTheme.cs new file mode 100644 index 0000000..caa95df --- /dev/null +++ b/Ooui.Forms/OouiTheme.cs @@ -0,0 +1,14 @@ +using System; +namespace Ooui.Forms +{ + public static class OouiTheme + { + public static readonly Color BackgroundColor = new Color (0xFF, 0xFF, 0xFF, 0xFF); + public static readonly Color TextColor = new Color (0x0, 0x0, 0x0, 0xFF); + public static readonly Color SecondaryTextColor = new Color (119, 119, 119, 0xFF); + + public static readonly Color ButtonBackgroundColor = new Color (51, 122, 183, 0xFF); + public static readonly Color ButtonBorderColor = new Color (46, 109, 164, 0xFF); + public static readonly Color ButtonTextColor = new Color (0xFF, 0xFF, 0xFF, 0xFF); + } +} diff --git a/Ooui.Forms/Renderers/BoxRenderer.cs b/Ooui.Forms/Renderers/BoxRenderer.cs index ee64082..7e20f8c 100644 --- a/Ooui.Forms/Renderers/BoxRenderer.cs +++ b/Ooui.Forms/Renderers/BoxRenderer.cs @@ -29,11 +29,7 @@ namespace Ooui.Forms.Renderers if (Element == null) return; - var elementColor = Element.Color; - if (!elementColor.IsDefault) - _colorToRenderer = elementColor.ToOouiColor (); - else - _colorToRenderer = Colors.Clear; + _colorToRenderer = Element.Color.ToOouiColor (Colors.Clear); Style.BackgroundColor = _colorToRenderer; } diff --git a/Ooui.Forms/Renderers/ButtonRenderer.cs b/Ooui.Forms/Renderers/ButtonRenderer.cs index 647008e..e253cea 100644 --- a/Ooui.Forms/Renderers/ButtonRenderer.cs +++ b/Ooui.Forms/Renderers/ButtonRenderer.cs @@ -84,7 +84,7 @@ namespace Ooui.Forms.Renderers var button = Element; if (button.BorderColor != Xamarin.Forms.Color.Default) - uiButton.Style.BorderColor = button.BorderColor.ToOouiColor (); + uiButton.Style.BorderColor = button.BorderColor.ToOouiColor (OouiTheme.ButtonBorderColor); else uiButton.Style.BorderColor = null; @@ -149,12 +149,7 @@ namespace Ooui.Forms.Renderers void UpdateTextColor () { - if (Element.TextColor == Xamarin.Forms.Color.Default) { - Control.Style.Color = null; - } - else { - Control.Style.Color = Element.TextColor.ToOouiColor (); - } + Control.Style.Color = Element.TextColor.ToOouiColor (OouiTheme.ButtonTextColor); } } } diff --git a/Ooui.Forms/Renderers/EditorRenderer.cs b/Ooui.Forms/Renderers/EditorRenderer.cs index f5de695..9f946a9 100644 --- a/Ooui.Forms/Renderers/EditorRenderer.cs +++ b/Ooui.Forms/Renderers/EditorRenderer.cs @@ -126,12 +126,7 @@ namespace Ooui.Forms.Renderers void UpdateTextColor () { - var textColor = Element.TextColor; - - if (textColor.IsDefault) - Control.Style.Color = "black"; - else - Control.Style.Color = textColor.ToOouiColor (); + Control.Style.Color = Element.TextColor.ToOouiColor (OouiTheme.TextColor); } } } diff --git a/Ooui.Forms/Renderers/EntryRenderer.cs b/Ooui.Forms/Renderers/EntryRenderer.cs index 1b31236..0bc7d25 100644 --- a/Ooui.Forms/Renderers/EntryRenderer.cs +++ b/Ooui.Forms/Renderers/EntryRenderer.cs @@ -8,7 +8,6 @@ namespace Ooui.Forms.Renderers { public class EntryRenderer : ViewRenderer { - Ooui.Color _defaultTextColor; bool _disposed; static Size initialSize = Size.Zero; @@ -72,8 +71,6 @@ namespace Ooui.Forms.Renderers textField.ClassName = "form-control"; - _defaultTextColor = Colors.Black; - textField.Input += OnEditingChanged; textField.Change += OnEditingEnded; } @@ -151,12 +148,7 @@ namespace Ooui.Forms.Renderers void UpdateColor () { - var textColor = Element.TextColor; - - if (textColor.IsDefault || !Element.IsEnabled) - Control.Style.Color = _defaultTextColor; - else - Control.Style.Color = textColor.ToOouiColor (); + Control.Style.Color = Element.TextColor.ToOouiColor (OouiTheme.TextColor); } void UpdateFont () diff --git a/Ooui.Forms/Renderers/FrameRenderer.cs b/Ooui.Forms/Renderers/FrameRenderer.cs index 752f534..b7b9ceb 100644 --- a/Ooui.Forms/Renderers/FrameRenderer.cs +++ b/Ooui.Forms/Renderers/FrameRenderer.cs @@ -37,10 +37,7 @@ namespace Ooui.Forms.Renderers Layer.BorderRadius = cornerRadius; - if (Element.BackgroundColor == Xamarin.Forms.Color.Default) - Layer.BackgroundColor = "white"; - else - Layer.BackgroundColor = Element.BackgroundColor.ToOouiColor (); + Layer.BackgroundColor = Element.BackgroundColor.ToOouiColor (OouiTheme.BackgroundColor); if (Element.HasShadow) { //Layer.ShadowRadius = 5; @@ -52,10 +49,13 @@ namespace Ooui.Forms.Renderers //Layer.ShadowOpacity = 0; } - if (Element.OutlineColor == Xamarin.Forms.Color.Default) + if (Element.OutlineColor == Xamarin.Forms.Color.Default) { Layer.BorderColor = Colors.Clear; + Layer.BorderWidth = 1; + Layer.BorderStyle = "none"; + } else { - Layer.BorderColor = Element.OutlineColor.ToOouiColor (); + Layer.BorderColor = Element.OutlineColor.ToOouiColor (Colors.Clear); Layer.BorderWidth = 1; Layer.BorderStyle = "solid"; } diff --git a/Ooui.Forms/Renderers/LabelRenderer.cs b/Ooui.Forms/Renderers/LabelRenderer.cs index 8405925..43f9377 100644 --- a/Ooui.Forms/Renderers/LabelRenderer.cs +++ b/Ooui.Forms/Renderers/LabelRenderer.cs @@ -97,10 +97,7 @@ namespace Ooui.Forms.Renderers protected override void SetBackgroundColor (Xamarin.Forms.Color color) { - if (color == Xamarin.Forms.Color.Default) - Style.BackgroundColor = Colors.Clear; - else - Style.BackgroundColor = color.ToOouiColor(); + Style.BackgroundColor = color.ToOouiColor (Colors.Clear); } void UpdateAlignment () diff --git a/Ooui.Forms/Renderers/LinkLabelRenderer.cs b/Ooui.Forms/Renderers/LinkLabelRenderer.cs index 79cbce0..cc5b5ce 100644 --- a/Ooui.Forms/Renderers/LinkLabelRenderer.cs +++ b/Ooui.Forms/Renderers/LinkLabelRenderer.cs @@ -100,10 +100,7 @@ namespace Ooui.Forms.Renderers protected override void SetBackgroundColor (Xamarin.Forms.Color color) { - if (color == Xamarin.Forms.Color.Default) - Style.BackgroundColor = Colors.Clear; - else - Style.BackgroundColor = color.ToOouiColor (); + Style.BackgroundColor = color.ToOouiColor (Colors.Clear); } void UpdateHRef () diff --git a/Ooui.Forms/Renderers/PickerRenderer.cs b/Ooui.Forms/Renderers/PickerRenderer.cs index 9f14b06..b52d12f 100644 --- a/Ooui.Forms/Renderers/PickerRenderer.cs +++ b/Ooui.Forms/Renderers/PickerRenderer.cs @@ -64,7 +64,7 @@ namespace Ooui.Forms.Renderers private void UpdateBackgroundColor() { - var backgroundColor = Element.BackgroundColor.ToOouiColor(); + var backgroundColor = Element.BackgroundColor.ToOouiColor(Colors.Clear); _select.Style.BackgroundColor = backgroundColor; } diff --git a/Ooui.Forms/Renderers/ViewRenderer.cs b/Ooui.Forms/Renderers/ViewRenderer.cs index 029ebf9..c656d7f 100644 --- a/Ooui.Forms/Renderers/ViewRenderer.cs +++ b/Ooui.Forms/Renderers/ViewRenderer.cs @@ -11,8 +11,6 @@ namespace Ooui.Forms.Renderers public class ViewRenderer : VisualElementRenderer where TElement : View where TNativeElement : Ooui.Element { - Color _defaultColor; - public TNativeElement Control { get; private set; } /// @@ -85,15 +83,11 @@ namespace Ooui.Forms.Renderers if (Control == null) return; - if (color == Xamarin.Forms.Color.Default) - Control.Style.BackgroundColor = _defaultColor; - else - Control.Style.BackgroundColor = color.ToOouiColor (); + Control.Style.BackgroundColor = color.ToOouiColor (OouiTheme.BackgroundColor); } protected void SetNativeControl (Ooui.Element element) { - _defaultColor = Color.FromStyleValue (element.Style.BackgroundColor); Control = (TNativeElement)element; if (Element.BackgroundColor != Xamarin.Forms.Color.Default) diff --git a/Ooui.Forms/VisualElementRenderer.cs b/Ooui.Forms/VisualElementRenderer.cs index 6a3beb6..fec2b1d 100644 --- a/Ooui.Forms/VisualElementRenderer.cs +++ b/Ooui.Forms/VisualElementRenderer.cs @@ -18,8 +18,6 @@ namespace Ooui.Forms { bool disposedValue = false; // To detect redundant calls - readonly Color _defaultColor = Colors.Clear; - readonly PropertyChangedEventHandler _propertyChangedHandler; public TElement Element { get; private set; } @@ -158,10 +156,7 @@ namespace Ooui.Forms protected virtual void SetBackgroundColor (Xamarin.Forms.Color color) { - if (color == Xamarin.Forms.Color.Default) - Style.BackgroundColor = _defaultColor; - else - Style.BackgroundColor = color.ToOouiColor (); + Style.BackgroundColor = color.ToOouiColor (Colors.Clear); } protected virtual void UpdateNativeWidget () diff --git a/Ooui/Color.cs b/Ooui/Color.cs index 0c072d6..216d8eb 100644 --- a/Ooui/Color.cs +++ b/Ooui/Color.cs @@ -17,6 +17,14 @@ namespace Ooui A = a; } + public Color (byte r, byte g, byte b) + { + R = r; + G = g; + B = b; + A = 0xFF; + } + public double Red { get => R / 255.0; set => R = value >= 1.0 ? (byte)255 : ((value <= 0.0) ? (byte)0 : (byte)(value * 255.0 + 0.5));