public static void Draw(this EllipticalGradient target, CGContext ctx, CGRect bounds)
        {
            target.Update();

            CGRect clippingBounds = bounds;

            if (!target.Frame.IsEmpty)
            {
                bounds = new CGRect(target.Frame.X, target.Frame.Y, target.Frame.Width, target.Frame.Height);
            }

            var gradColors   = GenerateGradientColors(target.Colors);
            var colorSpace   = CGColorSpace.CreateDeviceRGB();
            var grad         = new CGGradient(colorSpace, gradColors, ConvertToNativeArray(target.Locations));
            var gradCenter   = new CGPoint((bounds.Width * target.Center.X), (bounds.Height * target.Center.Y));
            var gradRadius   = (nfloat)Math.Min(bounds.Size.Width / 2, bounds.Size.Height / 2);
            var drawingFlags = GetGradientFlags(target);
            var scaleT       = CGAffineTransform.MakeScale(target.Scale.X, target.Scale.Y);

            ctx.SaveState();
            if (!target.Frame.IsEmpty)
            {
                ctx.ClipToRect(new CGRect(bounds.X + clippingBounds.X, bounds.Y + clippingBounds.Y, clippingBounds.Width, clippingBounds.Height));
            }

            ctx.TranslateCTM(bounds.X + gradCenter.X, bounds.Y + gradCenter.Y);
            ctx.RotateCTM((nfloat)(target.Rotation * (Math.PI / 180)));
            ctx.ScaleCTM(scaleT.xx, scaleT.yy);
            ctx.DrawRadialGradient(grad, CGPoint.Empty, 0, CGPoint.Empty, gradRadius, drawingFlags);
            ctx.RestoreState();

            grad.Dispose();
            colorSpace.Dispose();
        }
        public override void Draw(CGRect rect)
        {
            //Stopwatch s = new Stopwatch();
            //s.Start();

            //Console.WriteLine (" ----- SatBrightPickerView Draw");

            CGContext context = UIGraphics.GetCurrentContext();

            CGColor[] gradColors    = new CGColor[] { UIColor.FromHSBA(hue, 1, 1, 1).CGColor, new CGColor(1, 1, 1, 1) };
            nfloat[]  gradLocations = new nfloat[] { 0.0f, 1.0f };

            var colorSpace = CGColorSpace.CreateDeviceRGB();

            CGGradient gradient = new CGGradient(colorSpace, gradColors, gradLocations);

            context.DrawLinearGradient(gradient, new CGPoint(rect.Size.Width, 0), new CGPoint(0, 0), CGGradientDrawingOptions.DrawsBeforeStartLocation);

            gradColors = new CGColor[] { new CGColor(0, 0, 0, 0), new CGColor(0, 0, 0, 1) };

            gradient = new CGGradient(colorSpace, gradColors, gradLocations);
            context.DrawLinearGradient(gradient, new CGPoint(0, 0), new CGPoint(0, rect.Size.Height), CGGradientDrawingOptions.DrawsBeforeStartLocation);

            gradient.Dispose();
            colorSpace.Dispose();

            //s.Stop();
            //Console.WriteLine("-----> SatBright Draw time: " + s.Elapsed.ToString());
        }         //draw
Exemplo n.º 3
0
        public override void Draw(RectangleF rect)
        {
            CGContext context = UIGraphics.GetCurrentContext();

            switch (this.mask)
            {
            case SVProgressHUDMask.Black: {
                UIColor.FromWhiteAlpha(0f, 0.5f).SetColor();
                context.FillRect(this.Bounds);
                break;
            }

            case SVProgressHUDMask.Gradient: {
                float[] locations = new float[2] {
                    0.0f, 1.0f
                };
                float[] colors = new float[8] {
                    0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.75f
                };
                CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
                CGGradient   gradient   = new CGGradient(colorSpace, colors, locations);
                colorSpace.Dispose();

                PointF center = new PointF(this.Bounds.Size.Width / 2f, this.Bounds.Size.Height / 2f);
                float  radius = Math.Min(this.Bounds.Size.Width, this.Bounds.Size.Height);
                context.DrawRadialGradient(gradient, center, 0, center, radius, CGGradientDrawingOptions.DrawsAfterEndLocation);
                gradient.Dispose();
                break;
            }
            }
        }
        public static void Draw(this LinearGradient target, CGContext ctx, CGRect bounds)
        {
            target.Update();

            CGRect clippingBounds = bounds;

            if (!target.Frame.IsEmpty)
            {
                bounds = new CGRect(target.Frame.X, target.Frame.Y, target.Frame.Width, target.Frame.Height);
            }

            var gradColors = GenerateGradientColors(target.Colors);
            var colorSpace = CGColorSpace.CreateDeviceRGB();
            var grad       = new CGGradient(colorSpace, gradColors, ConvertToNativeArray(target.Locations));

            var points       = DetermineGradientPoints(target);
            var startPoint   = new CGPoint(points[0] * bounds.Width, points[1] * bounds.Height);
            var endPoint     = new CGPoint(points[2] * bounds.Width, points[3] * bounds.Height);
            var drawingFlags = GetGradientFlags(target);

            ctx.SaveState();

            if (!target.Frame.IsEmpty)
            {
                ctx.ClipToRect(new CGRect(bounds.X + clippingBounds.X, bounds.Y + clippingBounds.Y, clippingBounds.Width, clippingBounds.Height));
            }

            ctx.DrawLinearGradient(grad, startPoint, endPoint, drawingFlags);
            ctx.RestoreState();

            grad.Dispose();
            colorSpace.Dispose();
        }
Exemplo n.º 5
0
        public static UIImage BuildCoverFlow(UIImage image, float reflectionFraction)
        {
            int reflectionHeight = (int)(image.Size.Height * reflectionFraction);

            // gradient is always black and white and the mask must be in the gray colorspace
            var colorSpace = CGColorSpace.CreateDeviceGray();

            // Create the bitmap context
            var gradientBitmapContext = new CGBitmapContext(IntPtr.Zero, 1, reflectionHeight, 8, 0, colorSpace, CGImageAlphaInfo.None);

            // define the start and end grayscale values (with the alpha, even though
            // our bitmap context doesn't support alpha the gradien requires it)
            float [] colors = { 0, 1, 1, 1 };

            // Create the CGGradient and then release the gray color space
            var grayScaleGradient = new CGGradient(colorSpace, colors, null);

            colorSpace.Dispose();

            // create the start and end points for the gradient vector (straight down)
            var gradientStartPoint = new PointF(0, reflectionHeight);
            var gradientEndPoint   = PointF.Empty;

            // draw the gradient into the gray bitmap context
            gradientBitmapContext.DrawLinearGradient(grayScaleGradient, gradientStartPoint, gradientEndPoint, CGGradientDrawingOptions.DrawsAfterEndLocation);
            grayScaleGradient.Dispose();

            // Add a black fill with 50% opactiy
            gradientBitmapContext.SetFillColor(0, 0.5f);
            gradientBitmapContext.FillRect(new RectangleF(0, 0, 1, reflectionHeight));

            // conver the context into a CGImage and release the context
            var gradientImageMask = gradientBitmapContext.ToImage();

            gradientBitmapContext.Dispose();

            // create an image by masking the bitmap of the mainView content with the gradient view
            // then release the pre-masked content bitmap and the gradient bitmap
            var cgImage         = image.CGImage;
            var reflectionImage = cgImage.WithMask(gradientImageMask);

            cgImage.Dispose();
            gradientImageMask.Dispose();

            var size = new SizeF(image.Size.Width, image.Size.Height + reflectionHeight);

            UIGraphics.BeginImageContext(size);
            image.Draw(PointF.Empty);
            var context = UIGraphics.GetCurrentContext();

            context.DrawImage(new RectangleF(0, image.Size.Height, image.Size.Width, reflectionHeight), reflectionImage);

            var result = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();
            reflectionImage.Dispose();

            return(result);
        }
Exemplo n.º 6
0
        public static UIImage AddImageReflection(UIImage image, float reflectionFraction)
        {
            int reflectionHeight = (int) (image.Size.Height * reflectionFraction);

            // Create a 2 bit CGImage containing a gradient that will be used for masking the
            // main view content to create the 'fade' of the reflection.  The CGImageCreateWithMask
            // function will stretch the bitmap image as required, so we can create a 1 pixel wide gradient

            // gradient is always black and white and the mask must be in the gray colorspace
            var colorSpace = CGColorSpace.CreateDeviceGray ();

            // Creat the bitmap context
            var gradientBitmapContext = new CGBitmapContext (IntPtr.Zero, 1, reflectionHeight, 8, 0, colorSpace, CGImageAlphaInfo.None);

            // define the start and end grayscale values (with the alpha, even though
            // our bitmap context doesn't support alpha the gradien requires it)
            float [] colors = { 0, 1, 1, 1 };

            // Create the CGGradient and then release the gray color space
            var grayScaleGradient = new CGGradient (colorSpace, colors, null);
            colorSpace.Dispose ();

            // create the start and end points for the gradient vector (straight down)
            var gradientStartPoint = new PointF (0, reflectionHeight);
            var gradientEndPoint = PointF.Empty;

            // draw the gradient into the gray bitmap context
            gradientBitmapContext.DrawLinearGradient (grayScaleGradient, gradientStartPoint,
                                                      gradientEndPoint, CGGradientDrawingOptions.DrawsAfterEndLocation);
            grayScaleGradient.Dispose ();

            // Add a black fill with 50% opactiy
            gradientBitmapContext.SetGrayFillColor (0, 0.5f);
            gradientBitmapContext.FillRect (new RectangleF (0, 0, 1, reflectionHeight));

            // conver the context into a CGImage and release the context
            var gradientImageMask = gradientBitmapContext.ToImage ();
            gradientBitmapContext.Dispose ();

            // create an image by masking the bitmap of the mainView content with the gradient view
            // then release the pre-masked content bitmap and the gradient bitmap
            var reflectionImage = image.CGImage.WithMask (gradientImageMask);
            gradientImageMask.Dispose ();

            var size = new SizeF (image.Size.Width, image.Size.Height + reflectionHeight);

            UIGraphics.BeginImageContext (size);
            image.Draw (PointF.Empty);
            var context = UIGraphics.GetCurrentContext ();
            context.DrawImage (new RectangleF (0, image.Size.Height, image.Size.Width, reflectionHeight), reflectionImage);

            var result = UIGraphics.GetImageFromCurrentImageContext ();
            UIGraphics.EndImageContext ();
            reflectionImage.Dispose ();

            return result;
        }
        public override void DrawInContext(CoreGraphics.CGContext ctx)
        {
            base.DrawInContext(ctx);

            var knobFrame = CGRect.Inflate(PaintFrame, -2.0f, -2.0f);

            UIBezierPath knobPath = UIBezierPath.FromRoundedRect((CGRect)knobFrame, (nfloat)knobFrame.Height * Slider.Curvaceousness / 2.0f);

            // 1) fill - with a subtle shadow
            ctx.SetShadow(new CGSize(0, 1), 1.0f, UIColor.Gray.CGColor);
            ctx.SetFillColor(Slider.KnobColor.CGColor);
            ctx.AddPath(knobPath.CGPath);
            ctx.FillPath();

            // 2) outline
            ctx.SetStrokeColor(UIColor.Gray.CGColor);
            ctx.SetLineWidth((nfloat)0.5f);
            ctx.AddPath(knobPath.CGPath);
            ctx.StrokePath();


            // 3) inner gradient
            var rect     = CGRect.Inflate(knobFrame, -2.0f, -2.0f);
            var clipPath = UIBezierPath.FromRoundedRect((CGRect)rect, (nfloat)rect.Height * Slider.Curvaceousness / 2.0f);

            CGGradient   myGradient;
            CGColorSpace myColorspace;

            nfloat[] locations  = { 0.0f, 1.0f };
            nfloat[] components = { 0.0f, 0.0f, 0.0f, 0.15f,   // Start color
                                    0.0f, 0.0f, 0.0f, 0.05f }; // End color

            myColorspace = CGColorSpace.CreateDeviceRGB();     // CGColorSpaceCreateDeviceRGB();
            myGradient   = new CGGradient(myColorspace, components, locations);

            CGPoint startPoint = new CGPoint((float)rect.GetMidX(), (float)rect.GetMinY());
            CGPoint endPoint   = new CGPoint((float)rect.GetMidX(), (float)rect.GetMaxY());

            ctx.SaveState();
            ctx.AddPath(clipPath.CGPath);
            ctx.Clip();
            ctx.DrawLinearGradient((CGGradient)myGradient, (CGPoint)startPoint, (CGPoint)endPoint, (CGGradientDrawingOptions)0);

            myGradient.Dispose();
            myColorspace.Dispose();
            ctx.RestoreState();

            // 4) highlight
            if (Highlighted)
            {
                // fill
                ctx.SetFillColor(UIColor.FromWhiteAlpha((nfloat)0.0f, (nfloat)0.1f).CGColor);
                ctx.AddPath(knobPath.CGPath);
                ctx.FillPath();
            }
        }
Exemplo n.º 8
0
            public override void Draw(CGRect rect)
            {
                var context = UIGraphics.GetCurrentContext();

                context.SaveState();
                var frame = rect;

                frame.X      = (frame.Width - diameter) / 2;
                frame.Y      = frame.Height / 2;
                frame.Height = diameter;
                frame.Width  = diameter;

                //context.SetFillColorWithColor(UIColor.Black.ColorWithAlpha(.7f).CGColor);
                //context.FillEllipseInRect(frame);

                frame.Y -= 3;
                context.SetFillColor(UIColor.White.CGColor);
                context.FillRect(rect);
                context.SetFillColor(Color.CGColor);
                context.FillEllipseInRect(frame);

                context.RestoreState();

                //
                var shineFrame = frame;

                shineFrame.Height = (shineFrame.Height / 2);

                // the colors
                var           topColor    = UIColor.White.ColorWithAlpha(0.5f).CGColor;
                var           bottomColor = UIColor.White.ColorWithAlpha(0.10f).CGColor;
                List <nfloat> colors      = new List <nfloat>();

                colors.AddRange(topColor.Components);
                colors.AddRange(bottomColor.Components);
                nfloat[] locations = new nfloat[] { 0, 1 };

                CGGradient gradient = new CGGradient(topColor.ColorSpace, colors.ToArray(), locations);

                context.SaveState();
                context.SetShouldAntialias(true);
                context.AddEllipseInRect(shineFrame);
                context.Clip();

                var startPoint = new CGPoint(shineFrame.GetMidX(), shineFrame.GetMidY());
                var endPoint   = new CGPoint(shineFrame.GetMidX(), shineFrame.GetMaxY());

                context.DrawLinearGradient(gradient, startPoint, endPoint, CGGradientDrawingOptions.DrawsBeforeStartLocation);
                gradient.Dispose();
                context.RestoreState();
            }
Exemplo n.º 9
0
        public override void Draw(CGRect rect)
        {
            var currentContext = UIGraphics.GetCurrentContext();
            var locations      = new nfloat[] { 0.0f, 1.0f };
            var colors         = new[] { GradientStartColor.CGColor, GradientEndColor.CGColor };
            var rgbColorspace  = CGColorSpace.CreateDeviceRGB();
            var glossGradient  = new CGGradient(rgbColorspace, colors, locations);

            currentContext.DrawLinearGradient(glossGradient, CGPoint.Empty, Bounds.BottomRight(), CGGradientDrawingOptions.DrawsAfterEndLocation);
            glossGradient.Dispose();
            rgbColorspace.Dispose();
            base.BackgroundColor = UIColor.Clear;
            base.Draw((CGRect)rect);
        }
Exemplo n.º 10
0
        public static void DrawRoundRect(this CGContext cr, CGGradient gradient, CGRect rect, float[] cornerRadii, float angle = (float)-Math.PI / 2.0f)
        {
            cr.SaveState();

            RoundRectPath(cr, rect, cornerRadii);

            cr.Clip();
            CGPoint startg = RectIntersect(angle, rect);
            CGPoint endg   = RectIntersect(angle + (float)Math.PI, rect);

            cr.DrawLinearGradient(gradient, startg, endg, 0);
            gradient.Dispose();


            cr.RestoreState();
        }
Exemplo n.º 11
0
        private UIImage gradientImageWithSize(SizeF size, float[] locations, float[] components, int count)
        {
            UIGraphics.BeginImageContextWithOptions(size, false, 0);
            CGContext context = UIGraphics.GetCurrentContext();

            CGColorSpace colorSpace    = CGColorSpace.CreateDeviceRGB();
            CGGradient   colorGradient = new CGGradient(colorSpace, components, locations);

            colorSpace.Dispose();
            context.DrawLinearGradient(colorGradient, new PointF(0, 0), new PointF(size.Width, 0), 0);
            colorGradient.Dispose();

            UIImage image = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();
            return(image);
        }
        public UIImage ImageFromGradient(string color)
        {
            var gradientArray = color.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);

            nfloat[]  locations = new nfloat[gradientArray.Length];
            CGColor[] colorList = new CGColor[gradientArray.Length];
            for (int i = 0; i < gradientArray.Length; i++)
            {
                var gradientColorOffset = gradientArray[i].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                colorList[i] = Xamarin.Forms.Color.FromHex(gradientColorOffset[0]).ToCGColor();
                int   offsetInt   = Int32.Parse(gradientColorOffset[1].Replace("%", ""));
                float offsetFloat = offsetInt / 100f;
                locations[i] = offsetFloat;
            }

            var rect = new CoreGraphics.CGRect(0, 0, Control.Frame.Width, Control.Frame.Height);

            var colorSpace = CGColorSpace.CreateDeviceRGB();

            UIGraphics.BeginImageContext(rect.Size);
            CoreGraphics.CGGradient gradient = new CGGradient(colorSpace, colorList, locations);

            colorSpace.Dispose();

            var context = UIGraphics.GetCurrentContext();

            context.DrawLinearGradient(gradient, new CGPoint(0, 0), new CGPoint(0, Control.Frame.Height), CGGradientDrawingOptions.None);

            gradient.Dispose();

            try
            {
                var img = UIGraphics.GetImageFromCurrentImageContext();

                UIGraphics.EndImageContext();

                return(img);
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 13
0
            public override void Draw(CGRect rect)
            {
                base.Draw(rect);

                var context = UIGraphics.GetCurrentContext();
                var colorSpace = CGColorSpace.CreateDeviceRGB();
                nfloat[] locations = { 0, 1 };

                CGColor[] colors =
                {
                    UIColor.FromRGB(0x8D, 0xCF, 0x16).CGColor,
                    UIColor.FromRGB(0x65, 0xBD, 0x10).CGColor,
                };

                var gradiend = new CGGradient(colorSpace, colors, locations);
                context.DrawLinearGradient(gradiend, new CGPoint(0, 0), new CGPoint(0, rect.Size.Height), CGGradientDrawingOptions.DrawsBeforeStartLocation);
                gradiend.Dispose();
                colorSpace.Dispose();
            }
Exemplo n.º 14
0
        public override void Draw(RectangleF rect)
        {
            base.Draw(rect);

            CGContext context = UIGraphics.GetCurrentContext();

            CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();

            float step = 0.166666666666667f;

            float[] locations = new float[] {
                0.00f,
                step,
                step *2,
                step *3,
                step *4,
                step *5,
                1.0f
            };

            CGColor c1 = new CGColor(1, 0, 1, 1);
            CGColor c2 = new CGColor(1, 1, 0, 1);
            CGColor c3 = new CGColor(0, 1, 1, 1);

            CGColor[] colors = new CGColor[] {
                UIColor.Red.CGColor,
                c1,
                UIColor.Blue.CGColor,
                c3,
                UIColor.Green.CGColor,
                c2,
                UIColor.Red.CGColor
            };


            CGGradient gradiend = new CGGradient(colorSpace, colors, locations);

            context.DrawLinearGradient(gradiend, new PointF(rect.Size.Width, 0), new PointF(0, 0), CGGradientDrawingOptions.DrawsBeforeStartLocation);
            gradiend.Dispose();
            colorSpace.Dispose();
        }         // draw
Exemplo n.º 15
0
            public override void Draw(RectangleF rect)
            {
                base.Draw(rect);

                var context    = UIGraphics.GetCurrentContext();
                var colorSpace = CGColorSpace.CreateDeviceRGB();

                float[] locations = { 0, 1 };

                CGColor[] colors =
                {
                    UIColor.FromRGB(0x8D, 0xCF, 0x16).CGColor,
                    UIColor.FromRGB(0x65, 0xBD, 0x10).CGColor,
                };

                var gradiend = new CGGradient(colorSpace, colors, locations);

                context.DrawLinearGradient(gradiend, new PointF(0, 0), new PointF(0, rect.Size.Height), CGGradientDrawingOptions.DrawsBeforeStartLocation);
                gradiend.Dispose();
                colorSpace.Dispose();
            }
        public override void Draw(RectangleF rect)
        {
            if (this.SplitViewController.DividerStyle == DIVIDER_STYLE.Thin)
            {
                base.Draw(rect);
            }
            else if (this.SplitViewController.DividerStyle == DIVIDER_STYLE.PaneSplitter)
            {
                // Draw gradient background.
                RectangleF oBounds = this.Bounds;
                CGColorSpace oRGB = CGColorSpace.CreateDeviceRGB();
                float[] aLocations = new float[] {0, 1};
                float[] aComponents = new float[]
                {
                    // light
                    0.988f, 0.988f, 0.988f, 1.0f,
                    // dark
                    0.875f, 0.875f, 0.875f, 1.0f
                };
                CGGradient oGradient = new CGGradient(oRGB, aComponents, aLocations);
                CGContext oContext = UIGraphics.GetCurrentContext();
                PointF oStart;
                PointF oEnd;
                if (this.SplitViewController.IsVertical)
                {
                    // Light left to dark right.
                    oStart = new PointF(oBounds.GetMinX(), oBounds.GetMidY());
                    oEnd = new PointF(oBounds.GetMaxX(), oBounds.GetMidY());
                }
                else
                {
                    // Light top to dark bottom.
                    oStart = new PointF(oBounds.GetMidX(), oBounds.GetMinY());
                    oEnd = new PointF(oBounds.GetMidX(), oBounds.GetMaxY());
                }
                oContext.DrawLinearGradient(oGradient, oStart, oEnd, CGGradientDrawingOptions.DrawsAfterEndLocation);
                oRGB.Dispose();
                oGradient.Dispose();

                // Draw borders.
                float fBorderThickness = 1.0f;
                UIColor.FromWhiteAlpha(0.7f, 1.0f).SetFill();
                UIColor.FromWhiteAlpha(0.7f, 1.0f).SetStroke();
                RectangleF oBorderRect = oBounds;
                if (this.SplitViewController.IsVertical)
                {
                    oBorderRect.Width = fBorderThickness;
                    UIGraphics.RectFill(oBorderRect);
                    oBorderRect.X = oBounds.GetMaxX() - fBorderThickness;
                    UIGraphics.RectFill(oBorderRect);
                }
                else
                {
                    oBorderRect.Height = fBorderThickness;
                    UIGraphics.RectFill(oBorderRect);
                    oBorderRect.Y = oBounds.GetMaxY() - fBorderThickness;
                    UIGraphics.RectFill(oBorderRect);
                }

                // Draw grip.
                this.DrawGripThumbInRect(oBounds);
            }
        }
        //(void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)theContext
        public override void Draw(RectangleF rect)
        {
            CGContext theContext = UIGraphics.GetCurrentContext();

            // Note: due to use of kCGEncodingMacRoman, this code only works with Roman alphabets!
            // In order to support non-Roman alphabets, you need to add code generate glyphs,
            // and use CGContextShowGlyphsAtPoint
            theContext.SelectFont(this.Font.Name, this.Font.PointSize, CGTextEncoding.MacRoman);

            // Set Text Matrix
            theContext.TextMatrix = new CGAffineTransform(1, 0, 0, -1, 0, 0);

            // Set Drawing Mode to clipping path, to clip the gradient created below
            theContext.SetTextDrawingMode (CGTextDrawingMode.Clip);

            // Draw the label's text
            string text = this.Text;// cStringUsingEncoding:NSMacOSRomanStringEncoding];
            theContext.ShowTextAtPoint(0, this.Font.Ascender, text, text.Length);

            // Calculate text width
            PointF textEnd = theContext.TextPosition;

            // Get the foreground text color from the UILabel.
            // Note: UIColor color space may be either monochrome or RGB.
            // If monochrome, there are 2 components, including alpha.
            // If RGB, there are 4 components, including alpha.
            CGColor textColor = this.TextColor.CGColor;
            float[] components = textColor.Components;
            int numberOfComponents = textColor.NumberOfComponents;
            bool isRGB = (numberOfComponents == 4);
            float red = components[0];
            float green = isRGB ? components[1] : components[0];
            float blue = isRGB ? components[2] : components[0];
            float alpha = isRGB ? components[3] : components[1];

            // The gradient has 4 sections, whose relative positions are defined by
            // the "gradientLocations" array:
            // 1) from 0.0 to gradientLocations[0] (dim)
            // 2) from gradientLocations[0] to gradientLocations[1] (increasing brightness)
            // 3) from gradientLocations[1] to gradientLocations[2] (decreasing brightness)
            // 4) from gradientLocations[3] to 1.0 (dim)
            int num_locations = 3;

            // The gradientComponents array is a 4 x 3 matrix. Each row of the matrix
            // defines the R, G, B, and alpha values to be used by the corresponding
            // element of the gradientLocations array
            float[] gradientComponents = new float[12];
            for (int row = 0; row < num_locations; row++)
            {
                int index = 4 * row;
                gradientComponents[index++] = red;
                gradientComponents[index++] = green;
                gradientComponents[index++] = blue;
                gradientComponents[index] = alpha * MBSliderView.gradientDimAlpha;
            }

            // If animating, set the center of the gradient to be bright (maximum alpha)
            // Otherwise it stays dim (as set above) leaving the text at uniform
            // dim brightness
            if (animationTimer != null)
            {
                gradientComponents[7] = alpha;
            }

            // Load RGB Colorspace
            CGColorSpace colorspace = CGColorSpace.CreateDeviceRGB();

            // Create Gradient
            CGGradient gradient = new CGGradient(colorspace, gradientComponents, gradientLocations);
            // Draw the gradient (using label text as the clipping path)
            theContext.DrawLinearGradient (gradient, this.Bounds.Location, textEnd, 0);

            // Cleanup
            gradient.Dispose();
            colorspace.Dispose();
        }
Exemplo n.º 18
0
            public override void Draw(RectangleF rect)
            {
                var context = UIGraphics.GetCurrentContext();

                context.SaveState();
                var frame = rect;
                frame.X = (frame.Width - diameter) / 2;
                frame.Y = frame.Height / 2;
                frame.Height = diameter;
                frame.Width = diameter;

                //context.SetFillColorWithColor(UIColor.Black.ColorWithAlpha(.7f).CGColor);
                //context.FillEllipseInRect(frame);

                frame.Y -= 3;
                context.SetFillColorWithColor(UIColor.White.CGColor);
                context.FillRect(rect);
                context.SetFillColorWithColor(Color.CGColor);
                context.FillEllipseInRect(frame);

                context.RestoreState();

                //
                var shineFrame = frame;
                shineFrame.Height = (shineFrame.Height / 2);

                // the colors
                var topColor = UIColor.White.ColorWithAlpha(0.5f).CGColor;
                var bottomColor = UIColor.White.ColorWithAlpha(0.10f).CGColor;
                List<float> colors = new List<float>();
                colors.AddRange(topColor.Components);
                colors.AddRange(bottomColor.Components);
                float[] locations = new float[] { 0, 1 };

                CGGradient gradient = new CGGradient(topColor.ColorSpace, colors.ToArray(), locations);

                context.SaveState();
                context.SetShouldAntialias(true);
                context.AddEllipseInRect(shineFrame);
                context.Clip();

                var startPoint = new PointF(shineFrame.GetMidX(), shineFrame.GetMidY());
                var endPoint = new PointF(shineFrame.GetMidX(), shineFrame.GetMaxY());

                context.DrawLinearGradient(gradient, startPoint, endPoint, CGGradientDrawingOptions.DrawsBeforeStartLocation);
                gradient.Dispose();
                context.RestoreState();
            }
        public override void DrawInContext(CoreGraphics.CGContext ctx)
        {
            base.DrawInContext (ctx);

            var knobFrame = CGRect.Inflate(PaintFrame, -2.0f, -2.0f);

            UIBezierPath knobPath = UIBezierPath.FromRoundedRect((CGRect)knobFrame, (nfloat)knobFrame.Height * Slider.Curvaceousness / 2.0f);

            // 1) fill - with a subtle shadow
            ctx.SetShadow(new CGSize(0, 1), 1.0f, UIColor.Gray.CGColor);
            ctx.SetFillColor( Slider.KnobColor.CGColor);
            ctx.AddPath( knobPath.CGPath);
            ctx.FillPath ();

            // 2) outline
            ctx.SetStrokeColor(UIColor.Gray.CGColor);
            ctx.SetLineWidth((nfloat)0.5f);
            ctx.AddPath(knobPath.CGPath);
            ctx.StrokePath ();

            // 3) inner gradient
            var rect = CGRect.Inflate(knobFrame, -2.0f, -2.0f);
            var clipPath = UIBezierPath.FromRoundedRect ((CGRect)rect, (nfloat)rect.Height * Slider.Curvaceousness / 2.0f);

            CGGradient myGradient;
            CGColorSpace myColorspace;

            nfloat[] locations = { 0.0f, 1.0f };
            nfloat[] components = { 0.0f, 0.0f, 0.0f , 0.15f,  // Start color
                0.0f, 0.0f, 0.0f, 0.05f }; // End color

            myColorspace = CGColorSpace.CreateDeviceRGB (); // CGColorSpaceCreateDeviceRGB();
            myGradient = new CGGradient( myColorspace, components, locations);

            CGPoint startPoint = new CGPoint((float)rect.GetMidX(), (float)rect.GetMinY());
            CGPoint endPoint = new CGPoint((float)rect.GetMidX(), (float)rect.GetMaxY());

            ctx.SaveState ();
            ctx.AddPath( clipPath.CGPath);
            ctx.Clip ();
            ctx.DrawLinearGradient( (CGGradient)myGradient, (CGPoint)startPoint, (CGPoint)endPoint, (CGGradientDrawingOptions)0);

            myGradient.Dispose ();
            myColorspace.Dispose();
            ctx.RestoreState();

            // 4) highlight
            if (Highlighted)
            {
                // fill
                ctx.SetFillColor(UIColor.FromWhiteAlpha((nfloat)0.0f, (nfloat)0.1f).CGColor);
                ctx.AddPath( knobPath.CGPath);
                ctx.FillPath();
            }
        }
Exemplo n.º 20
0
        private UIImage gradientImageWithSize(SizeF size, float[] locations, float[] components, int count)
        {
            UIGraphics.BeginImageContextWithOptions(size, false, 0);
            CGContext context = UIGraphics.GetCurrentContext();

            CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
            CGGradient colorGradient = new CGGradient(colorSpace, components, locations);
            colorSpace.Dispose();
            context.DrawLinearGradient(colorGradient, new PointF(0, 0), new PointF(size.Width, 0), 0);
            colorGradient.Dispose();

            UIImage image = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
            return image;
        }
Exemplo n.º 21
0
        public override void Draw(CGRect rect)
        {
            if (SplitViewController.DividerStyle == DividerStyle.Thin)
            {
                base.Draw(rect);
            }
            else if (SplitViewController.DividerStyle == DividerStyle.PaneSplitter)
            {
                // Draw gradient background.
                var bounds     = Bounds;
                var rgb        = CGColorSpace.CreateDeviceRGB();
                var locations  = new nfloat[] { 0, 1 };
                var components = new nfloat[]
                {
                    // light
                    0.988f, 0.988f, 0.988f, 1.0f,
                    // dark
                    0.875f, 0.875f, 0.875f, 1.0f
                };
                var     gradient = new CGGradient(rgb, components, locations);
                var     context  = UIGraphics.GetCurrentContext();
                CGPoint start;
                CGPoint end;
                if (SplitViewController.IsVertical)
                {
                    // Light left to dark right.
                    start = new CGPoint(bounds.GetMinX(), bounds.GetMidY());
                    end   = new CGPoint(bounds.GetMaxX(), bounds.GetMidY());
                }
                else
                {
                    // Light top to dark bottom.
                    start = new CGPoint(bounds.GetMidX(), bounds.GetMinY());
                    end   = new CGPoint(bounds.GetMidX(), bounds.GetMaxY());
                }
                context.DrawLinearGradient(gradient, start, end, CGGradientDrawingOptions.DrawsAfterEndLocation);
                rgb.Dispose();
                gradient.Dispose();

                // Draw borders.
                var borderThickness = 1.0f;
                UIColor.FromWhiteAlpha(0.7f, 1.0f).SetFill();
                UIColor.FromWhiteAlpha(0.7f, 1.0f).SetStroke();
                var borderRect = bounds;
                if (SplitViewController.IsVertical)
                {
                    borderRect.Width = borderThickness;
                    UIGraphics.RectFill(borderRect);
                    borderRect.X = bounds.GetMaxX() - borderThickness;
                    UIGraphics.RectFill(borderRect);
                }
                else
                {
                    borderRect.Height = borderThickness;
                    UIGraphics.RectFill(borderRect);
                    borderRect.Y = bounds.GetMaxY() - borderThickness;
                    UIGraphics.RectFill(borderRect);
                }

                // Draw grip.
                DrawGripThumbInRect(bounds);
            }
        }
Exemplo n.º 22
0
        public override void Draw(RectangleF rect)
        {
            CGContext context = UIGraphics.GetCurrentContext();

            switch (this.mask) {
                case SVProgressHUDMask.Black : {
                    UIColor.FromWhiteAlpha(0f, 0.5f).SetColor();
                    context.FillRect(this.Bounds);
                    break;
                }
                case SVProgressHUDMask.Gradient: {
                    float[] locations = new float[2] {0.0f, 1.0f};
                    float[] colors = new float[8] {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.75f};
                    CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
                    CGGradient gradient = new CGGradient(colorSpace, colors, locations);
                    colorSpace.Dispose();

                    PointF center = new PointF(this.Bounds.Size.Width/2f, this.Bounds.Size.Height/2f);
                    float radius = Math.Min(this.Bounds.Size.Width, this.Bounds.Size.Height);
                    context.DrawRadialGradient(gradient, center, 0, center, radius, CGGradientDrawingOptions.DrawsAfterEndLocation);
                    gradient.Dispose();
                    break;
                }
            }
        }
Exemplo n.º 23
0
        public static UIImage AddImageReflection(UIImage image, float reflectionFraction)
        {
            int reflectionHeight = (int)(image.Size.Height * reflectionFraction);

            // Create a 2 bit CGImage containing a gradient that will be used for masking the
            // main view content to create the 'fade' of the reflection.  The CGImageCreateWithMask
            // function will stretch the bitmap image as required, so we can create a 1 pixel wide gradient


            // gradient is always black and white and the mask must be in the gray colorspace
            var colorSpace = CGColorSpace.CreateDeviceGray();

            // Creat the bitmap context
            var gradientBitmapContext = new CGBitmapContext(IntPtr.Zero, 1, reflectionHeight, 8, 0, colorSpace, CGImageAlphaInfo.None);

            // define the start and end grayscale values (with the alpha, even though
            // our bitmap context doesn't support alpha the gradien requires it)
            float [] colors = { 0, 1, 1, 1 };

            // Create the CGGradient and then release the gray color space
            var grayScaleGradient = new CGGradient(colorSpace, colors, null);

            colorSpace.Dispose();

            // create the start and end points for the gradient vector (straight down)
            var gradientStartPoint = new PointF(0, reflectionHeight);
            var gradientEndPoint   = PointF.Empty;

            // draw the gradient into the gray bitmap context
            gradientBitmapContext.DrawLinearGradient(grayScaleGradient, gradientStartPoint,
                                                     gradientEndPoint, CGGradientDrawingOptions.DrawsAfterEndLocation);
            grayScaleGradient.Dispose();

            // Add a black fill with 50% opactiy
            gradientBitmapContext.SetGrayFillColor(0, 0.5f);
            gradientBitmapContext.FillRect(new RectangleF(0, 0, 1, reflectionHeight));

            // conver the context into a CGImage and release the context
            var gradientImageMask = gradientBitmapContext.ToImage();

            gradientBitmapContext.Dispose();

            // create an image by masking the bitmap of the mainView content with the gradient view
            // then release the pre-masked content bitmap and the gradient bitmap
            var reflectionImage = image.CGImage.WithMask(gradientImageMask);

            gradientImageMask.Dispose();

            var size = new SizeF(image.Size.Width, image.Size.Height + reflectionHeight);

            // Use BeginImageContextWithOptions for retina images (only available on is 4.0 and up).
            // http://stackoverflow.com/questions/6965873/drawing-on-the-retina-display-using-coregraphics-image-pixelated
            if (UIScreen.MainScreen.RespondsToSelector(new Selector("scale")))
            {
                UIGraphics.BeginImageContextWithOptions(size, false, UIScreen.MainScreen.Scale);
            }
            else
            {
                UIGraphics.BeginImageContext(size);
            }

            image.Draw(PointF.Empty);
            var context = UIGraphics.GetCurrentContext();

            context.DrawImage(new RectangleF(0, image.Size.Height, image.Size.Width, reflectionHeight), reflectionImage);

            var result = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();
            reflectionImage.Dispose();

            return(result);
        }
        public override void Draw(CGRect rect)
        {
            //Stopwatch s = new Stopwatch();
            //s.Start();

            //Console.WriteLine (" ----- SatBrightPickerView Draw");

            CGContext context = UIGraphics.GetCurrentContext ();

            CGColor[] gradColors = new CGColor[] {UIColor.FromHSBA(hue,1,1,1).CGColor,new CGColor(1,1,1,1)};
            nfloat[] gradLocations = new nfloat[] { 0.0f, 1.0f };

            var colorSpace = CGColorSpace.CreateDeviceRGB ();

            CGGradient gradient = new CGGradient (colorSpace, gradColors, gradLocations);
             context.DrawLinearGradient(gradient,new CGPoint(rect.Size.Width,0),new CGPoint(0,0),CGGradientDrawingOptions.DrawsBeforeStartLocation);

            gradColors = new CGColor[] {new CGColor(0,0,0,0), new CGColor(0,0,0,1)};

            gradient = new CGGradient(colorSpace,gradColors, gradLocations);
             context.DrawLinearGradient(gradient,new CGPoint(0,0),new CGPoint(0,rect.Size.Height),CGGradientDrawingOptions.DrawsBeforeStartLocation);

            gradient.Dispose();
            colorSpace.Dispose();

            //s.Stop();
            //Console.WriteLine("-----> SatBright Draw time: " + s.Elapsed.ToString());
        }
        public override void Draw(RectangleF rect)
        {
            base.Draw (rect);

            CGContext context = UIGraphics.GetCurrentContext();

            CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();

            float step=0.166666666666667f;

            float[] locations = new float[] {
                0.00f,
                step,
                step*2,
                step*3,
                step*4,
                step*5,
                1.0f
            };

            CGColor c1 = new CGColor(1,0,1,1);
            CGColor c2 = new CGColor(1,1,0,1);
            CGColor c3 = new CGColor(0,1,1,1);

            CGColor[] colors = new CGColor[] {
                UIColor.Red.CGColor,
                c1,
                UIColor.Blue.CGColor,
                c3,
                UIColor.Green.CGColor,
                c2,
                UIColor.Red.CGColor
            };

            CGGradient gradiend = new CGGradient(colorSpace,colors, locations);
            context.DrawLinearGradient(gradiend,new PointF(rect.Size.Width,0),new PointF(0,0),CGGradientDrawingOptions.DrawsBeforeStartLocation);
            gradiend.Dispose();
            colorSpace.Dispose();
        }