diff options
author | Larry Ewing <lewing@xamarin.com> | 2018-04-06 19:46:49 +0300 |
---|---|---|
committer | Larry Ewing <lewing@microsoft.com> | 2018-07-16 22:05:48 +0300 |
commit | cd389979117ca88f6327b1cf079932da18614da0 (patch) | |
tree | 4b1db5030a53b5a0ca2eb583c7a121e99c5e040b /Xamarin.PropertyEditing.Mac | |
parent | 6305a78c3ca22051216b6e94efee62b6394d0764 (diff) |
Get basic alpha support working
Diffstat (limited to 'Xamarin.PropertyEditing.Mac')
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/BrushEditorControl.cs | 11 | ||||
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs | 26 |
2 files changed, 30 insertions, 7 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/BrushEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/BrushEditorControl.cs index 502bb06..1527581 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/BrushEditorControl.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/BrushEditorControl.cs @@ -28,7 +28,7 @@ namespace Xamarin.PropertyEditing.Mac public override void MouseDown (NSEvent theEvent) { if (Popover != null) - Popover.Show (new CGRect (20, this.Frame.Height/2 - 5, 5, 5), this, NSRectEdge.MinYEdge); + Popover.Show (new CGRect (20, this.Frame.Height/2 - 2.5, 5, 5), this, NSRectEdge.MinYEdge); } public override void Layout() @@ -121,17 +121,20 @@ namespace Xamarin.PropertyEditing.Mac } } + NSImage CreateSwatch (CommonColor color, CGSize size) { var board = new CICheckerboardGenerator () { - Color0 = CIColor.FromCGColor (color.ToCGColor ()), - Color1 = CIColor.FromCGColor (color.ToCGColor ()), + Color0 = CIColor.FromCGColor (color.Blend (CommonColor.White).ToCGColor ()), + Color1 = CIColor.FromCGColor (color.Blend (CommonColor.Black).ToCGColor ()), Width = (float)Math.Min (size.Height / 2f, 10), Center = new CIVector (new nfloat[] { 0, 0 }), }; var context = new CIContext (null); - return new NSImage (context.CreateCGImage (board.OutputImage, new CGRect (0, 0, size.Width, size.Height)), size); + var image = new NSImage (context.CreateCGImage (board.OutputImage, new CGRect (0, 0, size.Width, size.Height)), size); + context.Dispose (); + return image; } protected override void OnViewModelChanged (PropertyViewModel oldModel) diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs index 84e16e9..936a4f9 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs @@ -236,7 +236,8 @@ namespace Xamarin.PropertyEditing.Mac viewModel.Color = CommonColor.FromHSB ( Math.Min (360, Math.Max (0, color.Hue)), Math.Min (1, Math.Max (0, saturation)), - Math.Min (1, Math.Max (0, brightness))); + Math.Min (1, Math.Max (0, brightness)), + color.A); } } @@ -307,7 +308,8 @@ namespace Xamarin.PropertyEditing.Mac viewModel.Color = CommonColor.FromHSB ( Math.Max (0, Math.Min (360, hue)), Math.Max (0, Math.Min (1, color.Saturation)), - Math.Max (0, Math.Min (1, color.Brightness))); + Math.Max (0, Math.Min (1, color.Brightness)), + color.A); } public override void LayoutSublayers() @@ -344,9 +346,27 @@ namespace Xamarin.PropertyEditing.Mac public static class DrawingHelpers { public static CGColor ToCGColor (this CommonColor color) - => new CGColor (color.R / 255f, color.G / 255f, color.B / 255f); + => new CGColor (color.R / 255f, color.G / 255f, color.B / 255f, color.A / 255f); public static CGColor ToCGColor (this CommonColor color, float alpha) => new CGColor (color.R / 255f, color.G / 255f, color.B / 255f, alpha); + + public static CommonColor Blend (this CommonColor a, CommonColor b) + { + byte C (byte cb1, byte ab1, byte cb2) { + var c1 = cb1 / 255f; + var a1 = ab1 / 255f; + var c2 = cb2 / 255f; + + var c = Math.Max (0, Math.Min (255, (c1 + c2 * (1 - a1)) * 255)); + return (byte)c; + } + + return new CommonColor ( + C (a.R, a.A, b.R), + C (a.G, a.A, b.G), + C (a.B, a.A, b.B), + C (a.A, a.A, b.A)); + } } class SolidColorBrushEditor : ColorEditorView |