/// <summary>
        /// Tint the icon from green to red as the usage limit is approached
        /// </summary>
        /// <param name="usage">Usage as a percentage (0...1)</param>
        private void UpdateIconTint(float usage)
        {
            if (StatusController == null)
            {
                return;
            }

            float red, green;

            if (usage > 1f)
            {
                usage = 1f;
            }
            if (usage < 0f)
            {
                usage = 0f;
            }

            if (usage <= 0.5f)
            {
                green = ColourCap;
                //red = ColourCap * usage * 2f;
                red = 0f;
            }
            else
            {
                green = ((1f - usage) * 2f) * ColourCap;
                red   = ColourCap;
            }

            red   = red / 255f;
            green = green / 255f;

            StatusController.TintIcon(CIColor.FromRgb(red, green, 0f));
        }
Exemplo n.º 2
0
        public static NSImage Tint(this NSImage image, NSColor tint)
        {
            var colorGenerator = new CIConstantColorGenerator
            {
                Color = CIColor.FromCGColor(tint.CGColor)
            };

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

            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);
        }
Exemplo n.º 3
0
        public void CreateRefCount()
        {
            // Bug #7117

            CIImage  img         = new CIImage(CIColor.FromRgb(0.5f, 0.5f, 0.5f));
            Selector retainCount = new Selector("retainCount");

#if MONOMAC
            using (var ctx = new CIContext(null)) {
#else
            using (var ctx = CIContext.Create()) {
#endif
                using (var v = ctx.CreateCGImage(img, new RectangleF(0, 0, 5, 5))) {
                    int rc = Messaging.int_objc_msgSend(v.Handle, retainCount.Handle);
                    Assert.AreEqual(1, rc, "CreateCGImage #a1");
                }

                using (var v = ctx.CreateCGImage(img, new RectangleF(0, 0, 32, 32), CIImage.FormatARGB8, null)) {
                    int rc = Messaging.int_objc_msgSend(v.Handle, retainCount.Handle);
                    Assert.AreEqual(1, rc, "CreateCGImage #b1");
                }

#if !MONOMAC // CreateCGImage returning null on mac
                using (var v = ctx.CreateCGImage(img, new RectangleF(0, 0, 5, 5), CIFormat.ARGB8, null)) {
                    int rc = Messaging.int_objc_msgSend(v.Handle, retainCount.Handle);
                    Assert.AreEqual(1, rc, "CreateCGImage #c1");
                }
#endif
            }
        }
Exemplo n.º 4
0
 public void Render_Colorspace()
 {
     using (var ctx = new EAGLContext(EAGLRenderingAPI.OpenGLES2))
         using (var ci = CIContext.FromContext(ctx))
             using (var cv = new CVPixelBuffer(1, 1, CVPixelFormatType.CV24RGB))
                 using (CIImage img = new CIImage(CIColor.FromRgb(0.5f, 0.5f, 0.5f))) {
                     // that one "null allowed" was undocumented
                     ci.Render(img, cv, RectangleF.Empty, null);
                 }
 }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        public CIImage CheckerboardGenerator()
        {
            // Color 1
            var c0      = CIColor.FromRgb(1, 0, 0);
            var c1      = CIColor.FromRgb(0, 1, 0);
            var checker = new CICheckerboardGenerator()
            {
                Color0    = c0,
                Color1    = c1,
                Center    = new CIVector(new float[] { 10, 10 }), // Default [80 80]
                Sharpness = 1F                                    // Default 1
            };

            return(Crop(checker));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Transform a colour's [H]ue, [S]aturation and [V]Value
        /// </summary>
        /// <returns>The transformed colour</returns>
        /// <param name="input">Colour to transform</param>
        /// <param name="hue">Hue adjustment</param>
        /// <param name="saturation">Saturation adjustment</param>
        /// <param name="value">Value adjustment</param>
        /// <remarks>Based on: http://beesbuzz.biz/code/hsv_color_transforms.php</remarks>
        public static CIColor TransformHSV(this CIColor input, double hue, double saturation, double value)
        {
            double VSU = value * saturation * Math.Cos(hue * Math.PI / 180.0);
            double VSW = value * saturation * Math.Sin(hue * Math.PI / 180.0);

            double red = (.299 * value + 0.701 * VSU + 0.168 * VSW) * input.Red
                         + (0.587 * value - 0.587 * VSU + 0.330 * VSW) * input.Green
                         + (0.114 * value - 0.114 * VSU - 0.497 * VSW) * input.Blue;

            double green = (0.299 * value - 0.299 * VSU - 0.328 * VSW) * input.Red
                           + (0.587 * value + 0.413 * VSU + 0.035 * VSW) * input.Green
                           + (0.114 * value - 0.114 * VSU + 0.292 * VSW) * input.Blue;

            double blue = (0.299 * value - 0.3 * VSU + 0.125 * VSW) * input.Red
                          + (0.587 * value - 0.588 * VSU - 1.05 * VSW) * input.Green
                          + (0.114 * value + 0.886 * VSU - 0.203 * VSW) * input.Blue;

            if (red > 1.0)
            {
                red = 1.0;
            }
            if (red < 0.0)
            {
                red = 0.0;
            }

            if (green > 1.0)
            {
                green = 1.0;
            }
            if (green < 0.0)
            {
                green = 0.0;
            }

            if (blue > 1.0)
            {
                blue = 1.0;
            }
            if (blue < 0.0)
            {
                blue = 0.0;
            }

            var returnValue = CIColor.FromRgb((float)red, (float)green, (float)blue);

            return(returnValue);
        }
Exemplo n.º 8
0
        public IEditableImage ToMonochrome()
        {
            var mono = new CIColorMonochrome {
                Color     = CIColor.FromRgb(1, 1, 1),
                Intensity = 1.0f,
                Image     = CIImage.FromCGImage(_image.CGImage)
            };
            CIImage output        = mono.OutputImage;
            var     context       = CIContext.FromOptions(null);
            var     renderedImage = context.CreateCGImage(output, output.Extent);
            var     monoImage     = UIImage.FromImage(renderedImage, _image.CurrentScale, _image.Orientation);

            _image.Dispose();
            _image = monoImage;

            monoImage = null;
            renderedImage.Dispose();
            renderedImage = null;

            return(this);
        }