private NSImage CreateImageWithColor(string colorValue)
        {
            NSGraphicsContext.GlobalSaveGraphicsState();
            CGSize  size      = new CGSize(12, 12);
            NSImage tintImage = new NSImage(size);

            tintImage.LockFocus();

            float        cornerRadius = 5f;
            CGRect       rect         = new CGRect(0, 0, 10, 10);
            NSBezierPath path         = NSBezierPath.FromRoundedRect(rect, cornerRadius, cornerRadius);

            if (string.IsNullOrEmpty(colorValue))
            {
                NSColor.Grid.Set();
                path.Stroke();
            }
            else
            {
                Utility.ColorWithHexColorValue(colorValue, 1.0f).SetFill();
                path.Fill();
            }

            tintImage.UnlockFocus();
            CGContext context = NSGraphicsContext.CurrentContext.CGContext;

            return(tintImage);
        }
Beispiel #2
0
        public override void DrawRow(nint row, CGRect clipRect)
        {
            if (row >= RowCount)
            {
                return;
            }

            base.DrawRow(row, clipRect);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext.ShouldAntialias = false;

            CGRect rectRow    = RectForRow(row);
            CGRect rectColumn = RectForColumn(0);
            CGRect rect       = Frame;

            CGPoint start = new CGPoint(rectColumn.Left, rectRow.Top);
            CGPoint end   = new CGPoint(rectRow.Right, rectRow.Top);

            var linePath = new NSBezierPath();

            GridColor.Set();
            linePath.MoveTo(start);
            linePath.LineTo(end);
            linePath.ClosePath();
            linePath.Stroke();

            NSGraphicsContext.GlobalRestoreGraphicsState();
        }
Beispiel #3
0
        public void DrawLayer(CALayer layer, CoreGraphics.CGContext context)
        {
            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext graphicsContext = NSGraphicsContext.FromGraphicsPort(context, true);

            NSGraphicsContext.CurrentContext = graphicsContext;

            NSBezierPath path = new NSBezierPath();
            //ベジェ曲線
            var x1 = this.Frame.Left;
            var y1 = this.Frame.Top;
            var x2 = this.Frame.Right;
            var y2 = this.Frame.Bottom;

            path.MoveTo(new CoreGraphics.CGPoint(x1, y1));
            path.CurveTo(new CoreGraphics.CGPoint(x2, y1),
                         new CoreGraphics.CGPoint(x1, y2),
                         new CoreGraphics.CGPoint(x2, y2));
            //背景は白
            NSColor.White.Set();
            path.Fill();
            //線は青
            NSColor.Blue.Set();
            //線の太さ
            path.LineWidth = 2;
            path.Stroke();

            NSGraphicsContext.GlobalRestoreGraphicsState();
        }
Beispiel #4
0
        public override void DrawRect(CGRect dirtyRect)
        {
            NSGraphicsContext.GlobalSaveGraphicsState();
            NSColor.White.Set();
            NSGraphics.RectFill(dirtyRect);

            Subviews[1].Hidden  = !this.IsSelected;
            Subviews [2].Hidden = !this.IsSelected;
            NSGraphicsContext.GlobalRestoreGraphicsState();
        }
Beispiel #5
0
        void StartDrawing()
        {
#if OSX
            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext = graphicsContext;
#elif IOS
            UIGraphics.PushContext(Control);
#endif
            Control.SaveState();
        }
Beispiel #6
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);
        }
Beispiel #7
0
        public static NSImageRep Resize(this NSImageRep image, CGSize newsize, ImageInterpolation interpolation = ImageInterpolation.Default, CGSize?imageSize = null)
        {
            var newrep = new NSBitmapImageRep(IntPtr.Zero, (nint)newsize.Width, (nint)newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * (nint)newsize.Width, 32);

            newrep.Size = imageSize ?? newsize;

            var graphics = NSGraphicsContext.FromBitmap(newrep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext           = graphics;
            graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG();
            image.DrawInRect(new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, NSCompositingOperation.SourceOver, 1f, true, DrawHints);
            NSGraphicsContext.GlobalRestoreGraphicsState();
            return(newrep);
        }
        public static NSImage Resize(this NSImage image, sd.Size newsize, ImageInterpolation interpolation = ImageInterpolation.Default)
        {
            var newimage = new NSImage(newsize);
            var newrep   = new NSBitmapImageRep(IntPtr.Zero, newsize.Width, newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * newsize.Width, 32);

            newimage.AddRepresentation(newrep);

            var graphics = NSGraphicsContext.FromBitmap(newrep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext           = graphics;
            graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG();
            image.DrawInRect(new sd.RectangleF(sd.PointF.Empty, newimage.Size), new sd.RectangleF(sd.PointF.Empty, image.Size), NSCompositingOperation.SourceOver, 1f);
            NSGraphicsContext.GlobalRestoreGraphicsState();
            return(newimage);
        }
        public override void DrawRect(CGRect dirtyRect)
        {
            base.DrawRect(dirtyRect);

            NSGraphicsContext.GlobalSaveGraphicsState();

            //backgound frame
            DrawCoverRect();

            //title frame
            DrawTitleRect();

            //bottom line
            DrawBottomLine();

            NSGraphicsContext.GlobalRestoreGraphicsState();
        }
Beispiel #10
0
        public static NSImageRep Resize(this NSImageRep image, CGSize newsize, ImageInterpolation interpolation = ImageInterpolation.Default, CGSize?imageSize = null)
        {
            var newrep = new NSBitmapImageRep(IntPtr.Zero, (nint)newsize.Width, (nint)newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * (nint)newsize.Width, 32);

            newrep.Size = imageSize ?? newsize;

            var graphics = NSGraphicsContext.FromBitmap(newrep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext           = graphics;
            graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG();
#if XAMMAC
            // Xamarin.Mac doesn't allow null for hints, remove this when it does.
            Messaging.bool_objc_msgSend_CGRect_CGRect_UIntPtr_nfloat_bool_IntPtr(image.Handle, selDrawInRect_FromRect_Operation_Fraction_RespectFlipped_Hints_Handle, new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, (UIntPtr)(ulong)NSCompositingOperation.SourceOver, 1f, true, IntPtr.Zero);
#else
            image.DrawInRect(new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, NSCompositingOperation.SourceOver, 1f, true, null);
#endif
            NSGraphicsContext.GlobalRestoreGraphicsState();
            return(newrep);
        }
Beispiel #11
0
            public void Draw(CGContext ctx, CGColor foregroundColor, double x, double y)
            {
                bool tempForegroundSet = false;

                // if no color attribute is set for the whole string,
                // NSLayoutManager will use the default control foreground color.
                // To override the default color we need to apply the current CGContext stroke color
                // before all other attributes are set, otherwise it will remove all other foreground colors.
                if (foregroundColor != null && !Attributes.Any(a => a is ColorTextAttribute && a.StartIndex == 0 && a.Count == Text.Length))
                {
                    // FIXME: we need to find a better way to accomplish this without the need to reset all attributes.
                    ResetAttributes(NSColor.FromCGColor(foregroundColor));
                    tempForegroundSet = true;
                }

                ctx.SaveState();
                NSGraphicsContext.GlobalSaveGraphicsState();
                var nsContext = NSGraphicsContext.FromCGContext(ctx, true);

                NSGraphicsContext.CurrentContext = nsContext;

                using (var TextLayout = new NSLayoutManager())
                {
                    TextLayout.AddTextContainer(TextContainer);
                    TextStorage.AddLayoutManager(TextLayout);

                    TextLayout.DrawBackgroundForGlyphRange(new NSRange(0, Text.Length), new CGPoint(x, y));
                    TextLayout.DrawGlyphsForGlyphRange(new NSRange(0, Text.Length), new CGPoint(x, y));
                    TextStorage.RemoveLayoutManager(TextLayout);
                    TextLayout.RemoveTextContainer(0);
                }

                // reset foreground color change
                if (tempForegroundSet)
                {
                    ResetAttributes();
                }

                NSGraphicsContext.GlobalRestoreGraphicsState();
                ctx.RestoreState();
            }
Beispiel #12
0
        /// <summary>
        /// Draw the triangle
        /// </summary>
        /// <param name="rect">Rect.</param>
        public override void DrawRect(CGRect dirtyRect)
        {
            base.DrawRect(dirtyRect);

            NSGraphicsContext.GlobalSaveGraphicsState();

            BackgroudColor.Set();

            CGContext context = NSGraphicsContext.CurrentContext.GraphicsPort;

            context.BeginPath();
            context.MoveTo(PUBLICATION_DOWN_WIDTH, PUBLICATION_DOWN_WIDTH);

            context.AddLineToPoint(PUBLICATION_DOWN_WIDTH, 0.0f);
            context.AddLineToPoint(0.0f, PUBLICATION_DOWN_WIDTH);
            context.AddLineToPoint(PUBLICATION_DOWN_WIDTH, PUBLICATION_DOWN_WIDTH);
            context.ClosePath();

            context.DrawPath(CGPathDrawingMode.Fill);

            NSGraphicsContext.GlobalRestoreGraphicsState();
        }
        public override void DrawRect(RectangleF dirtyRect)
        {
            var contentRect = new RectangleF(Bounds.X + LineThickness, Bounds.Y + LineThickness, Bounds.Width - (LineThickness * 2), Bounds.Height - (LineThickness * 2));

            // Mac coords are reversed vs. .Net / MonoTouch coords, so we just reverse the top/bottom coords to compensate
            var top    = contentRect.Bottom;
            var bottom = contentRect.Top;

            var left  = contentRect.Left;
            var right = contentRect.Right;
            var path  = new NSBezierPath();

            // Draw the 'arrow' at the top
            path.MoveTo(new PointF(ArrowX, top));
            path.LineTo(new PointF(ArrowX + ArrowWidth / 2, top - ArrowHeight));
            path.LineTo(new PointF(right - CornerRadius, top - ArrowHeight));

            // Right right
            var topRightCorner = new PointF(right, top - ArrowHeight);

            path.CurveTo(new PointF(right, top - ArrowHeight - CornerRadius), topRightCorner, topRightCorner);

            // Right line
            path.LineTo(new PointF(right, bottom + CornerRadius));

            // Bottom right
            var bottomRightCorner = new PointF(right, bottom);

            path.CurveTo(new PointF(right - CornerRadius, bottom), bottomRightCorner, bottomRightCorner);

            // Bottom line
            path.LineTo(new PointF(left + CornerRadius, bottom));

            // Bottom left
            var bottomLeftCorner = new PointF(left, bottom);

            path.CurveTo(new PointF(left, bottom + CornerRadius), bottomLeftCorner, bottomLeftCorner);

            // Left line
            path.LineTo(new PointF(left, top - ArrowHeight - CornerRadius));

            // Top left
            var topLeftCorner = new PointF(left, top - ArrowHeight);

            path.CurveTo(new PointF(left + CornerRadius, top - ArrowHeight), topLeftCorner, topLeftCorner);

            // Line up to start of 'arrow' & finish
            path.LineTo(new PointF(ArrowX - ArrowWidth / 2, top - ArrowHeight));
            path.ClosePath();

            // Fill the path with a semi-transparent white
            NSColor.FromDeviceWhite(1.0f, FillOpacity).SetFill();
            path.Fill();

            NSGraphicsContext.GlobalSaveGraphicsState();

            // Clip all rendering of controls within view to within the path outline we specified earlier
            var clip = NSBezierPath.FromRect(Bounds);

            clip.AppendPath(path);
            clip.AddClip();

            // Draw the border
            path.LineWidth = LineThickness * 2;
            NSColor.White.SetStroke();
            path.Stroke();

            NSGraphicsContext.GlobalRestoreGraphicsState();
        }
Beispiel #14
0
        protected void EnsureRep()
        {
            if (rep == null)
            {
                rep = GetBestRepresentation();
            }

            // on Big Sur, rep is usually going to be a proxy, so let's find the concrete NSBitmapImageRep class the slow way..

            if (bmprep != null)
            {
                return;
            }

            if (rep is IconFrameHandler.LazyImageRep lazyRep)
            {
                bmprep = lazyRep.Rep;
            }
            else
            {
                bmprep = rep as NSBitmapImageRep ?? GetBestRepresentation() as NSBitmapImageRep;
            }

            if (bmprep != null)
            {
                return;
            }

            // go through concrete representations as we might have a proxy (Big Sur)
            // this is fixed with MonoMac, but not Xamarin.Mac.
            var representations = Control.Representations();

            for (int i = 0; i < representations.Length; i++)
            {
                NSImageRep rep = representations[i];
                if (rep is NSBitmapImageRep brep)
                {
                    bmprep = brep;
                    return;
                }
            }

            // create a new bitmap rep and copy the contents
            var size             = Size;
            int numComponents    = rep.HasAlpha ? 4 : 3;
            int bitsPerComponent = 8;
            int bitsPerPixel     = numComponents * bitsPerComponent;
            int bytesPerPixel    = bitsPerPixel / 8;
            int bytesPerRow      = bytesPerPixel * size.Width;

            bmprep = new NSBitmapImageRep(IntPtr.Zero, size.Width, size.Height, bitsPerComponent, numComponents, rep.HasAlpha, false, rep.ColorSpaceName, bytesPerRow, bitsPerPixel);
            var graphicsContext = NSGraphicsContext.FromBitmap(bmprep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext = graphicsContext;
            Control.Draw(CGPoint.Empty, new CGRect(CGPoint.Empty, size.ToNS()), NSCompositingOperation.Copy, 1);
            NSGraphicsContext.GlobalRestoreGraphicsState();

            // remove all existing representations
            for (int i = 0; i < representations.Length; i++)
            {
                NSImageRep rep = representations[i];
                Control.RemoveRepresentation(rep);
            }

            // add the new one back
            Control.AddRepresentation(bmprep);
        }