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

BrushPropertyViewModel.cs « ViewModels « Xamarin.PropertyEditing - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c9c8b527549b9fe616b98ad7762459d4a1894768 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.Collections.Generic;
using Xamarin.PropertyEditing.Drawing;

namespace Xamarin.PropertyEditing.ViewModels
{
	internal class BrushPropertyViewModel : PropertyViewModel<CommonBrush>
	{
		public BrushPropertyViewModel(IPropertyInfo property, IEnumerable<IObjectEditor> editors)
			: base(property, editors)
		{
			if (property is IColorSpaced colorSpacedPropertyInfo) {
				ColorSpaces = colorSpacedPropertyInfo.ColorSpaces;
			}
		}

		public IReadOnlyList<string> ColorSpaces { get; }

		public CommonSolidBrush PreviousSolidBrush { get; set; }

		string ColorSpace => Value is CommonSolidBrush solidBrush ? solidBrush.ColorSpace : null;

		CommonColor? hueColor;
		public CommonColor HueColor {
			get => (hueColor ?? (hueColor = LastColor.HueColor)).Value;
			set {
				if (!hueColor.Equals (value)) {
					var saturation = Color.Saturation;
					var brightness = Color.Brightness;
					Color = CommonColor.FromHSB (value.Hue, saturation, brightness, Color.A);
					OnPropertyChanged (nameof (Color));
					hueColor = value;
					OnPropertyChanged ();
					Value = new CommonSolidBrush (Color, ColorSpace, Value.Opacity);
				}
			}
		}

		CommonColor? shade;
		public CommonColor Shade {
			get => (shade.HasValue ? shade : (shade = LastColor)).Value;
			set {
				if (!shade.Equals (value)) {
					shade = value;
					OnPropertyChanged ();
					Value = new CommonSolidBrush (value, ColorSpace, Value.Opacity);
				}
			}
		}

		public CommonColor Color {
			get => Value is CommonSolidBrush solidBrush ? solidBrush.Color : new CommonColor(0, 0, 0);
			set {
				if (!Color.Equals (value)) {
					CommonColor oldHue = HueColor;
					CommonColor newHue = value.HueColor;
					Value = new CommonSolidBrush (value, ColorSpace, Value.Opacity);
					OnPropertyChanged ();
					if (!newHue.Equals (oldHue)) {
						hueColor = newHue;
						OnPropertyChanged (nameof (HueColor));
					}
					if (!value.Equals (shade)) {
						shade = value;
						OnPropertyChanged (nameof (Shade));
					}
					if (!initialColor.HasValue) {
						initialColor = value;
					}
				}
			}
		}

		CommonColor? initialColor;
		public CommonColor InitialColor => initialColor ?? (initialColor = Color).Value;

		CommonColor? lastColor;
		public CommonColor LastColor => lastColor ?? (lastColor = Color).Value;

		public void CommitLastColor ()
		{
			lastColor = Color;
			shade = Color;
			hueColor = Color.HueColor;
			OnPropertyChanged (nameof (LastColor));
			OnPropertyChanged (nameof (Shade));
			OnPropertyChanged (nameof (HueColor));
			Value = new CommonSolidBrush (Color, ColorSpace, Value.Opacity);
		}

		public void CommitShade ()
		{
			lastColor = Shade;
			OnPropertyChanged (nameof (LastColor));
			Value = new CommonSolidBrush (Shade, ColorSpace, Value.Opacity);
		}
	}
}