diff options
author | Larry Ewing <lewing@xamarin.com> | 2018-04-19 21:28:05 +0300 |
---|---|---|
committer | Larry Ewing <lewing@microsoft.com> | 2018-07-16 22:05:48 +0300 |
commit | d2d4c127d1e7a14bdeb3e39ef9992993b1266c10 (patch) | |
tree | 284253d17254c9e9a96718fb27839470b33a7cce /Xamarin.PropertyEditing.Mac | |
parent | efc1f61c75a59b17b0460277e51c4f3c47dc7074 (diff) |
Make the ColorEditorLayers use ColorComponent
Diffstat (limited to 'Xamarin.PropertyEditing.Mac')
3 files changed, 36 insertions, 28 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentEditor.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentEditor.cs index 50d9e62..7328f21 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentEditor.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentEditor.cs @@ -157,9 +157,10 @@ namespace Xamarin.PropertyEditing.Mac if (set != null) { var channel = set.Editor.ComponentEditor; var grad = set.Gradient; - ViewModel.Color = channel.UpdateColorFromValue ( + ViewModel.Color = channel.UpdateColorFromLocation ( + grad, ViewModel.Color, - channel.ValueFromLocation (grad, Layer.ConvertPointToLayer (location,grad.SuperLayer))); + Layer.ConvertPointToLayer (location,grad.SuperLayer)); return; } } @@ -175,9 +176,10 @@ namespace Xamarin.PropertyEditing.Mac if (set != null) { var channel = set.Editor.ComponentEditor; var grad = set.Gradient; - ViewModel.Color = channel.UpdateColorFromValue ( + ViewModel.Color = channel.UpdateColorFromLocation ( + grad, ViewModel.Color, - channel.ValueFromLocation (grad, Layer.ConvertPointToLayer (location, grad.SuperLayer))); + Layer.ConvertPointToLayer (location, grad.SuperLayer)); return; } base.MouseMoved (theEvent); @@ -232,26 +234,31 @@ namespace Xamarin.PropertyEditing.Mac public void UpdateGradientLayer (CAGradientLayer layer, CommonColor color) { + var c = color.UpdateRGB (a: 255); + layer.Colors = LerpSteps (MinimumValue, MaximumValue, 7) - .Select (value => UpdateColorFromValue (color.UpdateRGB (a: 255), value).ToCGColor ()).ToArray (); + .Select (value => UpdateColorFromValue (c, value).ToCGColor ()).ToArray (); } - public double InverseLerp (CGPoint start, CGPoint end, CGPoint loc) + public double InvLerp (CGPoint start, CGPoint end, CGPoint loc) { var a = new CGVector (end.X - start.X, end.Y - start.Y); var b = new CGVector (loc.X - start.X, loc.Y - start.Y); var dot = a.dx * b.dx + a.dy * b.dy; var len = Math.Sqrt (a.dx * a.dx + a.dy * a.dy); - var pos = dot / len; - return MaximumValue * pos - MinimumValue * (1 - pos); + return dot / len; } - public CGPoint Lerp (CGPoint start, CGPoint end, double amount) - { - return new CGPoint ( - start.X + (end.X - start.X) * amount, - start.Y + (end.Y - start.Y) * amount); - } + public static double InvLerp (double start, double end, double value) + => (value - start) / (end - start); + + public static double Lerp (double start, double end, double amount) + => end * amount - start * (1 - amount); + + public static CGPoint Lerp (CGPoint start, CGPoint end, double amount) + => new CGPoint ( + start.X + (end.X - start.X) * amount, + start.Y + (end.Y - start.Y) * amount); public double ValueFromLocation (CAGradientLayer layer, CGPoint loc) { @@ -260,13 +267,17 @@ namespace Xamarin.PropertyEditing.Mac (loc.X - rect.X) / rect.Width, (loc.Y - rect.Y) / rect.Height); - return Clamp (InverseLerp (layer.StartPoint, layer.EndPoint, unitLoc)); + return Clamp (Lerp (MinimumValue, MaximumValue, InvLerp (layer.StartPoint, layer.EndPoint, unitLoc))); } + public CommonColor UpdateColorFromLocation (CAGradientLayer layer, CommonColor color, CGPoint loc) + => UpdateColorFromValue (color, ValueFromLocation (layer, loc)); + public CGPoint LocationFromColor (CAGradientLayer layer, CommonColor color) { var pos = ValueFromColor (color); - var amount = (pos - MinimumValue) / (MaximumValue - MinimumValue); + + var amount = InvLerp (MinimumValue, MaximumValue, pos); var unitLoc = Lerp (layer.StartPoint, layer.EndPoint, amount); return new CGPoint ( diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/DrawingExtensions.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/DrawingExtensions.cs index 78f85be..e8cd5c9 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/DrawingExtensions.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/DrawingExtensions.cs @@ -85,7 +85,6 @@ namespace Xamarin.PropertyEditing.Mac width: rect.Width, height: rect.Height); - public static CommonColor UpdateRGB ( this CommonColor color, byte? r = null, diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs index 6db6c4a..fd5c01e 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs @@ -345,7 +345,7 @@ namespace Xamarin.PropertyEditing.Mac base.LayoutSublayers(); Saturation.Frame = new CGRect (Margin, Margin, Frame.Width - 2 * Margin, Frame.Height - 2 * Margin); - Brightness.Frame = new CGRect (0, 0, Frame.Width - 2 * Margin, Frame.Height - 2 * Margin); + Brightness.Frame = Saturation.Frame.Bounds (); Saturation.StartPoint = new CGPoint (0, .5); Saturation.EndPoint = new CGPoint (1, .5); } @@ -355,20 +355,17 @@ namespace Xamarin.PropertyEditing.Mac { LayoutIfNeeded (); var color = interaction.Color; - - var frame = Saturation.Frame; + var sat = saturationEditor.LocationFromColor (Saturation, color); var bright = brightnessEditor.LocationFromColor (Brightness, color); var x = sat.X; - var y = bright.Y + frame.Y; + var y = bright.Y + Saturation.Frame.Y; Grip.Frame = new CGRect (x - GripRadius, y - GripRadius, GripRadius * 2, GripRadius * 2); - Saturation.Colors = new[] { - new CGColor (1f, 1f, 1f), - interaction.Color.HueColor.ToCGColor () - }; + if (interaction.StartColor.ToCGColor () != Saturation.Colors.Last()) + saturationEditor.UpdateGradientLayer (Saturation, interaction.StartColor.HueColor); } public override void UpdateFromLocation (EditorInteraction interaction, CGPoint location) @@ -441,16 +438,17 @@ namespace Xamarin.PropertyEditing.Mac var loc = location; var clos = Math.Min (Colors.Frame.Height, Math.Max (0, loc.Y - Colors.Frame.Y)); - Grip.Frame = new CGRect (1, clos + Colors.Frame.Y - Grip.Frame.Height / 2, Frame.Width - 2, 2 * GripRadius); + Grip.Frame = new CGRect (1, clos + Colors.Frame.Y - Grip.Frame.Height / 2f, Frame.Width - 2, 2 * GripRadius); var hue = (1 - clos/ Colors.Frame.Height) * 360; if (interaction == null) return; var color = interaction.Color; - c = interaction.Color = hueEditor.UpdateColorFromValue ( + c = interaction.Color = hueEditor.UpdateColorFromLocation ( + Colors, interaction.Color, - hueEditor.ValueFromLocation (Colors, loc)); + loc); } public override void LayoutSublayers() |