forked from xamarin/ios-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuartzBlendingViewController.xib.cs
250 lines (213 loc) · 6.59 KB
/
QuartzBlendingViewController.xib.cs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using QuartzSample;
using MonoTouch.CoreGraphics;
using System.Drawing;
namespace QuartzSample
{
public partial class QuartzBlendingViewController : QuartzViewController
{
#region Constructors
// The IntPtr and initWithCoder constructors are required for items that need
// to be able to be created from a xib rather than from managed code
public QuartzBlendingViewController (IntPtr handle) : base (handle)
{
Initialize ();
}
[Export ("initWithCoder:")]
public QuartzBlendingViewController (NSCoder coder) : base (coder)
{
Initialize ();
}
public QuartzBlendingViewController () : base ("QuartzBlendingViewController", null)
{
Initialize ();
}
public QuartzBlendingViewController (CreateView creator, string title, string info) : base (creator, "QuartzBlendingViewController", title, info)
{
Array.Sort (Colors, ColorSortByLuminance);
}
void Initialize ()
{
}
#endregion
static UIColor [] Colors = new UIColor [] {
UIColor.Red,
UIColor.Green,
UIColor.Blue,
UIColor.Yellow,
UIColor.Magenta,
UIColor.Cyan,
UIColor.Orange,
UIColor.Purple,
UIColor.Brown,
UIColor.White,
UIColor.LightGray,
UIColor.DarkGray,
UIColor.Black
};
static string [] BlendModes = new string [] {
"Normal",
"Multiply",
"Screen",
"Overlay",
"Darken",
"Lighten",
"ColorDodge",
"ColorBurn",
"SoftLight",
"HardLight",
"Difference",
"Exclusion",
"Hue",
"Saturation",
"Color",
"Luminosity",
// Porter-Duff Blend Modes
"Clear",
"Copy",
"SourceIn",
"SourceOut",
"SourceAtop",
"DestinationOver",
"DestinationIn",
"DestinationOut",
"DestinationAtop",
"XOR",
"PlusDarker",
"PlusLighter",
};
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
picker.Model = new BlendSelector (this);
var qbv = (QuartzBlendingView) quartzView;
picker.Select (Array.IndexOf (Colors, qbv.DestinationColor), 0, false);
picker.Select (Array.IndexOf (Colors, qbv.SourceColor), 1, false);
picker.Select ((int) qbv.BlendMode, 2, false);
}
static float LuminanceForColor (UIColor color)
{
CGColor cgcolor = color.CGColor;
var components = cgcolor.Components;
float luminance = 0;
switch (cgcolor.ColorSpace.Model){
case CGColorSpaceModel.Monochrome:
// For grayscale colors, the luminance is the color value
luminance = components[0];
break;
case CGColorSpaceModel.RGB:
// For RGB colors, we calculate luminance assuming sRGB Primaries as per
// http://en.wikipedia.org/wiki/Luminance_(relative)
luminance = 0.2126f * components[0] + 0.7152f * components[1] + 0.0722f * components[2];
break;
default:
// We don't implement support for non-gray, non-rgb colors at this time.
// Since our only consumer is colorSortByLuminance, we return a larger than normal
// value to ensure that these types of colors are sorted to the end of the list.
luminance = 2.0f;
break;
}
return luminance;
}
// Simple comparison function that sorts the two (presumed) UIColors according to their luminance value.
static int ColorSortByLuminance (UIColor color1, UIColor color2)
{
float luminance1 = LuminanceForColor(color1);
float luminance2 = LuminanceForColor(color2);
if (luminance1 == luminance2)
return 0;
else if (luminance1 < luminance2)
return -1;
else
return 1;
}
public class BlendSelector : UIPickerViewModel
{
QuartzBlendingViewController parent;
public BlendSelector (QuartzBlendingViewController parent)
{
this.parent = parent;
}
public override int GetComponentCount (UIPickerView picker){
return 3;
}
public override int GetRowsInComponent (UIPickerView picker, int component)
{
if (component == 0 || component == 1)
return Colors.Length;
return BlendModes.Length;
}
public override float GetComponentWidth (UIPickerView picker, int component)
{
if (component == 0 || component == 1)
return 48f;
return 192f;
}
const int kColorTag = 1;
const int kLabelTag = 1;
public override UIView GetView (UIPickerView picker, int row, int component, UIView view)
{
var size = picker.RowSizeForComponent (component);
if (component == 0 || component == 1){
if (view == null || view.Tag != kColorTag){
view = new UIView (new RectangleF (0, 0, size.Width-4, size.Height-4)){
Tag = kColorTag
};
}
view.BackgroundColor = Colors [row];
} else {
if (view == null || view.Tag != kLabelTag){
view = new UILabel (new RectangleF (0, 0, size.Width-4, size.Height-4)){
Tag = kLabelTag,
Opaque = false,
BackgroundColor = UIColor.Clear
};
}
var label = (UILabel) view;
label.TextColor = UIColor.Black;
label.Text = BlendModes [row];
label.Font = UIFont.BoldSystemFontOfSize (18f);
}
return view;
}
public override void Selected (UIPickerView picker, int row, int component)
{
var qbv = (QuartzBlendingView) parent.quartzView;
qbv.DestinationColor = Colors [picker.SelectedRowInComponent (0)];
qbv.SourceColor = Colors [picker.SelectedRowInComponent (1)];
qbv.BlendMode = (CGBlendMode) picker.SelectedRowInComponent (2);
qbv.SetNeedsDisplay ();
}
}
}
public class QuartzBlendingView : QuartzView {
public UIColor SourceColor { get; set; }
public UIColor DestinationColor { get; set; }
public CGBlendMode BlendMode { get; set; }
public QuartzBlendingView () : base () {
SourceColor = UIColor.White;
DestinationColor = UIColor.Black;
BlendMode = CGBlendMode.Normal;
}
public override void DrawInContext (CGContext context)
{
// Start with a background whose color we don't use in the demo
context.SetGrayFillColor (0.2f, 1);
context.FillRect (Bounds);
// We want to just lay down the background without any blending so we use the Copy mode rather than Normal
context.SetBlendMode(CGBlendMode.Copy);
// Draw a rect with the "background" color - this is the "Destination" for the blending formulas
context.SetFillColorWithColor (DestinationColor.CGColor);
context.FillRect(new RectangleF (110, 20, 100, 100));
// Set up our blend mode
context.SetBlendMode (BlendMode);
// And draw a rect with the "foreground" color - this is the "Source" for the blending formulas
context.SetFillColorWithColor (SourceColor.CGColor);
context.FillRect (new RectangleF (60, 45, 200, 50));
}
}
}