public static Color ToEto(this NSColor color, bool calibrated = false) { if (color == null) { return(Colors.Transparent); } var colorspace = calibrated ? NSColorSpace.CalibratedRGB : NSColorSpace.DeviceRGB; var converted = color.UsingColorSpaceFast(colorspace); if (converted == null) { // Convert named (e.g. system) colors to RGB using its CGColor converted = color.ToCG().ToNS().UsingColorSpaceFast(colorspace); if (converted == null) { return(new Color(color, 0, 0, 0, 1f)); } } nfloat red, green, blue, alpha; converted.GetRgba(out red, out green, out blue, out alpha); return(new Color(color, (float)red, (float)green, (float)blue, (float)alpha)); }
public static NSImage Tint(this NSImage image, NSColor tint) { var colorGenerator = new CIConstantColorGenerator { Color = CIColor.FromCGColor(tint.ToCG()) }; #pragma warning disable CS0618 // Image => InputImage in Xamarin.Mac 6.6 var colorFilter = new CIColorControls { Image = (CIImage)colorGenerator.ValueForKey(CIFilterOutputKey.Image), Saturation = 3f, Brightness = 0.35f, Contrast = 1f }; var monochromeFilter = new CIColorMonochrome { Image = CIImage.FromCGImage(image.CGImage), Color = CIColor.FromRgb(0.75f, 0.75f, 0.75f), Intensity = 1f }; var compositingFilter = new CIMultiplyCompositing { Image = (CIImage)colorFilter.ValueForKey(CIFilterOutputKey.Image), BackgroundImage = (CIImage)monochromeFilter.ValueForKey(CIFilterOutputKey.Image) }; #pragma warning restore CS0618 var outputImage = (CIImage)compositingFilter.ValueForKey(CIFilterOutputKey.Image); var extent = outputImage.Extent; var newsize = Size.Truncate(extent.Size.ToEto()); if (newsize.IsEmpty) { return(image); } var tintedImage = new NSImage(newsize.ToNS()); tintedImage.LockFocus(); try { var graphics = NSGraphicsContext.CurrentContext.GraphicsPort; var ciContext = CIContext.FromContext(graphics, new CIContextOptions { UseSoftwareRenderer = true }); ciContext.DrawImage(outputImage, extent, extent); } finally { tintedImage.UnlockFocus(); } var newrep = tintedImage.Representations()[0]; newrep.Size = image.Size; tintedImage.Size = image.Size; return(tintedImage); }
public static NSImage Tint(this NSImage image, NSColor tint) { CIFilter colorGenerator = CIFilter.FromName("CIConstantColorGenerator"); CIColor color = CIColor.FromCGColor(tint.ToCG()); colorGenerator.SetValueForKey(color, CIFilterInputKey.Color); CIFilter colorFilter = CIFilter.FromName("CIColorControls"); colorFilter.SetValueForKey(colorGenerator.ValueForKey(CIFilterOutputKey.Image), CIFilterInputKey.Image); colorFilter.SetValueForKey(NSNumber.FromFloat(3f), CIFilterInputKey.Saturation); colorFilter.SetValueForKey(NSNumber.FromFloat(0.35f), CIFilterInputKey.Brightness); colorFilter.SetValueForKey(NSNumber.FromFloat(1f), CIFilterInputKey.Contrast); CIFilter monochromeFilter = CIFilter.FromName("CIColorMonochrome"); CIImage baseImage = CIImage.FromCGImage(image.CGImage); monochromeFilter.SetValueForKey(baseImage, CIFilterInputKey.Image); monochromeFilter.SetValueForKey(CIColor.FromRgb(0.75f, 0.75f, 0.75f), CIFilterInputKey.Color); monochromeFilter.SetValueForKey(NSNumber.FromFloat(1f), CIFilterInputKey.Intensity); CIFilter compositingFilter = CIFilter.FromName("CIMultiplyCompositing"); compositingFilter.SetValueForKey(colorFilter.ValueForKey(CIFilterOutputKey.Image), CIFilterInputKey.Image); compositingFilter.SetValueForKey(monochromeFilter.ValueForKey(CIFilterOutputKey.Image), CIFilterInputKey.BackgroundImage); CIImage outputImage = (CIImage)compositingFilter.ValueForKey(CIFilterOutputKey.Image); var extent = outputImage.Extent; var newsize = sd.Size.Truncate(extent.Size); var tintedImage = new NSImage(newsize); var newrep = new NSBitmapImageRep(IntPtr.Zero, newsize.Width, newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * newsize.Width, 32); tintedImage.AddRepresentation(newrep); var graphics = NSGraphicsContext.FromBitmap(newrep); NSGraphicsContext.GlobalSaveGraphicsState(); NSGraphicsContext.CurrentContext = graphics; var ciContext = CIContext.FromContext(graphics.GraphicsPort, new CIContextOptions { UseSoftwareRenderer = true }); ciContext.DrawImage(outputImage, extent, extent); NSGraphicsContext.GlobalRestoreGraphicsState(); newrep.Size = image.Size; return(tintedImage); }