Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DrawingExtensions.cs « Custom « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e2f107566d2f19245c7a0be1505f17bdae73af6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using AppKit;
using CoreGraphics;
using CoreImage;
using Xamarin.PropertyEditing.Drawing;

namespace Xamarin.PropertyEditing.Mac
{
	public static class DrawingExtensions
	{
		public static NSImage CreateSwatch (this CommonColor color, CGSize size)
		{
			var c0 = CIColor.FromCGColor (color.Blend (CommonColor.White).ToCGColor ());
			var c1 = CIColor.FromCGColor (color.Blend (CommonColor.Black).ToCGColor ());

			return CreateSwatch (color, size, c0, c1);
		}

		public static NSImage CreateSwatch (this CommonColor color, CGSize size, CIColor c0, CIColor c1)
			=> new NSImage (GenerateCheckerboard (new CGRect (0, 0, size.Width,size.Height), c0, c1), size);


		public static CGImage GenerateCheckerboard (CGRect frame)
			=> GenerateCheckerboard (frame, CIColor.WhiteColor, CIColor.BlackColor);

		public static CGImage GenerateCheckerboard (CGRect frame, CIColor c0, CIColor c1)
		{
			using (var board = new CICheckerboardGenerator () {
				Color0 = c0,
				Color1 = c1,
				Width = (float)Math.Min (frame.Height / 2f, 10),
				Center = new CIVector (new nfloat[] { 0, 0 }),
			}) {
				using (var context = new CIContext (null)) {
					return context.CreateCGImage (board.OutputImage, new CGRect (0, 0, frame.Width, frame.Height));
				}
			}
		}

		public static CGColor ToCGColor (this CommonColor color)
			=> new CGColor (color.R / 255f, color.G / 255f, color.B / 255f, color.A / 255f);

		public static NSColor ToNSColor (this CommonColor color)
			=> NSColor.FromRgba (color.R, color.G, color.B, color.A);

		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));
		}
	}
}