示例#1
0
		public static void Colourize (NSView control, Color color, Action drawAction)
		{
			var size = control.Frame.Size;
			var image = new NSImage (size);
			
			image.LockFocusFlipped (control.IsFlipped);
			drawAction ();
			image.UnlockFocus ();
			
			var ciImage = CIImage.FromData (image.AsTiff ());
			
			if (control.IsFlipped) {
				var realSize = control.ConvertSizeToBase (size);
				var affineTransform = new NSAffineTransform ();
				affineTransform.Translate (0, realSize.Height);
				affineTransform.Scale (1, -1);
				var filter1 = CIFilter.FromName ("CIAffineTransform");
				filter1.SetValueForKey (ciImage, CIInputImage);
				filter1.SetValueForKey (affineTransform, CIInputTransform);
				ciImage = filter1.ValueForKey (CIOutputImage) as CIImage;
			}
			
			var filter2 = CIFilter.FromName ("CIColorControls");
			filter2.SetDefaults ();
			filter2.SetValueForKey (ciImage, CIInputImage);
			filter2.SetValueForKey (new NSNumber (0.0f), CIInputSaturation);
			ciImage = filter2.ValueForKey (CIOutputImage) as CIImage;
			
			var filter3 = CIFilter.FromName ("CIColorMatrix");
			filter3.SetDefaults ();
			filter3.SetValueForKey (ciImage, CIInputImage);
			filter3.SetValueForKey (new CIVector (0, color.R, 0), CIInputRVector);
			filter3.SetValueForKey (new CIVector (color.G, 0, 0), CIInputGVector);
			filter3.SetValueForKey (new CIVector (0, 0, color.B), CIInputBVector);
			ciImage = filter3.ValueForKey (CIOutputImage) as CIImage;
			
			image = new NSImage (size);
			var rep = NSCIImageRep.FromCIImage (ciImage);
			image.AddRepresentation (rep);
			image.Draw (SD.PointF.Empty, new SD.RectangleF (SD.PointF.Empty, size), NSCompositingOperation.SourceOver, 1);
			/* Use this when implemented in maccore:
			ciImage.Draw (SD.PointF.Empty, new SD.RectangleF (SD.PointF.Empty, size), NSCompositingOperation.SourceOver, 1);
			 */
		}
		NSImage MakeRotatedCopy (NSImage original, float degrees)
		{
			var copy = new NSImage (original.Size);
			copy.LockFocus ();
			try {
				var rot = new NSAffineTransform ();
				rot.Translate (original.Size.Width / 2, original.Size.Height / 2);
				rot.RotateByDegrees (degrees);
				rot.Translate (-original.Size.Width / 2, -original.Size.Height / 2);
				rot.Concat ();
				original.Draw (PointF.Empty, RectangleF.Empty, NSCompositingOperation.Copy, 1);
			} finally {
				copy.UnlockFocus ();
			}
			return copy;
		}