public void RectInflate() { var r = new Rect (new Size (10, 20)); r.Inflate (4, 6); Assert.AreEqual (-4, r.X); Assert.AreEqual (-6, r.Y); Assert.AreEqual (18, r.Width); Assert.AreEqual (32, r.Height); }
public async Task EllipseRadialGradient() { var canvas = Platforms.Current.CreateImageCanvas(new Size(100)); var rect = new Rect(0, 10, 100, 80); var brush = new RadialGradientBrush( new Point(0.5, 0.5), new Size(0.5, 0.125), Colors.Green, Colors.LightGray); canvas.DrawEllipse(rect, brush: brush); await SaveImage(canvas, "Brush.EllipseRadialGradient.png"); }
public async Task RectLinearGradient() { var canvas = Platforms.Current.CreateImageCanvas(new Size(100)); var rect = new Rect(0, 10, 100, 80); var brush = new LinearGradientBrush( Point.Zero, Point.OneY, Colors.Green, Colors.LightGray); canvas.DrawRectangle(rect, brush: brush); await SaveImage(canvas, "Brush.RectLinearGradient.png"); }
public async Task RectAbsLinearGradient() { var canvas = Platforms.Current.CreateImageCanvas(new Size(100)); var rect = new Rect(0, 10, 100, 80); var brush = new LinearGradientBrush( Point.Zero, new Point(0, 200), Colors.Yellow, Colors.Red); brush.Absolute = true; canvas.DrawRectangle(rect, brush: brush); await SaveImage(canvas, "Brush.RectAbsLinearGradient.png"); }
public async Task HSL() { // http://en.wikipedia.org/wiki/HSL_and_HSV#Examples var tests = new[] { Tuple.Create (new Color (1.000, 1.000, 1.000), Color.FromHSL ( 0.0/360.0, 0.000, 1.000)), Tuple.Create (new Color (0.500, 0.500, 0.500), Color.FromHSL ( 0.0/360.0, 0.000, 0.500)), Tuple.Create (new Color (0.000, 0.000, 0.000), Color.FromHSL ( 0.0/360.0, 0.000, 0.000)), Tuple.Create (new Color (1.000, 0.000, 0.000), Color.FromHSL ( 0.0/360.0, 1.000, 0.500)), Tuple.Create (new Color (0.750, 0.750, 0.000), Color.FromHSL ( 60.0/360.0, 1.000, 0.375)), Tuple.Create (new Color (0.000, 0.500, 0.000), Color.FromHSL (120.0/360.0, 1.000, 0.250)), Tuple.Create (new Color (0.500, 1.000, 1.000), Color.FromHSL (180.0/360.0, 1.000, 0.750)), Tuple.Create (new Color (0.500, 0.500, 1.000), Color.FromHSL (240.0/360.0, 1.000, 0.750)), Tuple.Create (new Color (0.750, 0.250, 0.750), Color.FromHSL (300.0/360.0, 0.500, 0.500)), Tuple.Create (new Color (0.628, 0.643, 0.142), Color.FromHSL ( 61.8/360.0, 0.638, 0.393)), Tuple.Create (new Color (0.255, 0.104, 0.918), Color.FromHSL (251.1/360.0, 0.832, 0.511)), Tuple.Create (new Color (0.116, 0.675, 0.255), Color.FromHSL (134.9/360.0, 0.707, 0.396)), Tuple.Create (new Color (0.941, 0.785, 0.053), Color.FromHSL ( 49.5/360.0, 0.893, 0.497)), Tuple.Create (new Color (0.704, 0.187, 0.897), Color.FromHSL (283.7/360.0, 0.775, 0.542)), Tuple.Create (new Color (0.931, 0.463, 0.316), Color.FromHSL ( 14.3/360.0, 0.817, 0.624)), Tuple.Create (new Color (0.998, 0.974, 0.532), Color.FromHSL ( 56.9/360.0, 0.991, 0.765)), Tuple.Create (new Color (0.099, 0.795, 0.591), Color.FromHSL (162.4/360.0, 0.779, 0.447)), Tuple.Create (new Color (0.211, 0.149, 0.597), Color.FromHSL (248.3/360.0, 0.601, 0.373)), Tuple.Create (new Color (0.495, 0.493, 0.721), Color.FromHSL (240.5/360.0, 0.290, 0.607)), }; var s = 32; var canvas = Platforms.Current.CreateImageCanvas(new Size(s * tests.Length, s)); foreach (var t in tests) { var rect = new Rect(0, 0, s, s); canvas.FillRectangle(rect, t.Item2); canvas.Translate(s, 0); AssertInRange(t.Item2.R, t.Item1.R - 1, t.Item1.R + 1); AssertInRange(t.Item2.G, t.Item1.G - 1, t.Item1.G + 1); AssertInRange(t.Item2.B, t.Item1.B - 1, t.Item1.B + 1); } await SaveImage(canvas, "Color.HSL.png"); }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, BaseBrush brush = null) { if (brush == null) return; var paint = GetFontPaint(font, alignment); var w = paint.MeasureText(text); var fm = paint.GetFontMetrics(); var h = fm.Ascent + fm.Descent; var point = frame.Position; var fr = new Rect(point, new Size(w, h)); AddBrushPaint(paint, brush, fr); graphics.DrawText(text, (float) point.X, (float) point.Y, paint); }
public void DrawRectangle(Rect frame, Pen pen = null, BaseBrush brush = null) { if (brush != null) { var paint = GetBrushPaint(brush, frame); graphics.DrawRect((float) (frame.X), (float) (frame.Y), (float) (frame.X + frame.Width), (float) (frame.Y + frame.Height), paint); } if (pen != null) { var paint = GetPenPaint(pen); graphics.DrawRect((float) (frame.X), (float) (frame.Y), (float) (frame.X + frame.Width), (float) (frame.Y + frame.Height), paint); } }
public void DrawImage(IImage image, Rect frame, double alpha = 1.0) { var ii = image as BitmapImage; if (ii != null) { var paint = GetImagePaint(alpha); var isize = new Size(ii.Bitmap.Width, ii.Bitmap.Height); var scale = frame.Size/isize; var m = new Matrix(); m.PreTranslate((float) frame.X, (float) frame.Y); m.PreScale((float) scale.Width, (float) scale.Height); graphics.DrawBitmap(ii.Bitmap, m, paint); } }
public void DrawRectangle(Rect frame, Pen pen = null, BaseBrush brush = null) { }
D2D1.Brush GetBrush(Rect frame, BaseBrush brush) { if (brush == null) return null; var sb = brush as SolidBrush; if (sb != null) { return new D2D1.SolidColorBrush (renderTarget, sb.Color.ToColor4 ()); } var lgb = brush as LinearGradientBrush; if (lgb != null) { if (lgb.Stops.Count < 2) return null; var props = new D2D1.LinearGradientBrushProperties { StartPoint = lgb.GetAbsoluteStart (frame).ToVector2 (), EndPoint = lgb.GetAbsoluteEnd (frame).ToVector2 (), }; return new D2D1.LinearGradientBrush (renderTarget, props, GetStops (lgb.Stops)); } var rgb = brush as RadialGradientBrush; if (rgb != null) { if (rgb.Stops.Count < 2) return null; var rad = rgb.GetAbsoluteRadius (frame); var center = rgb.GetAbsoluteCenter (frame); var focus = rgb.GetAbsoluteFocus (frame); var props = new D2D1.RadialGradientBrushProperties { Center = center.ToVector2 (), RadiusX = (float)rad.Width, RadiusY = (float)rad.Height, GradientOriginOffset = (focus - center).ToVector2 (), }; return new D2D1.RadialGradientBrush (renderTarget, props, GetStops (rgb.Stops)); } // TODO: Radial gradient brushes return new D2D1.SolidColorBrush (renderTarget, Colors.Black.ToColor4 ()); }
public void DrawRectangle(Rect frame, Pen pen = null, BaseBrush brush = null) { var p = GetBrush (pen); var b = GetBrush (frame, brush); if (b != null) { renderTarget.FillRectangle (frame.ToRectangleF (), b); } if (p != null) { renderTarget.DrawRectangle (frame.ToRectangleF (), p, (float)pen.Width, GetStrokeStyle (pen)); } }
public void DrawEllipse(Rect frame, Pen pen = null, BaseBrush brush = null) { var p = GetBrush (pen); var b = GetBrush (frame, brush); var c = frame.Center; var s = new D2D1.Ellipse (new Vector2 ((float)c.X, (float)c.Y), (float)(frame.Width / 2.0), (float)(frame.Height / 2.0)); if (b != null) { renderTarget.FillEllipse (s, b); } if (p != null) { renderTarget.DrawEllipse (s, p, (float)pen.Width, GetStrokeStyle (pen)); } }
public Rect MoveInto(Rect frame) { var r = this; if (r.Right > frame.Right) r.X = frame.Right - r.Width; if (r.X < frame.X) r.X = frame.X; if (r.Bottom > frame.Bottom) r.Y = frame.Bottom - r.Height; if (r.Y < frame.Y) r.Y = frame.Y; return r; }
public void DrawEllipse(Rect frame, Pen pen = null, BaseBrush brush = null) { }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, BaseBrush brush = null) { }
private void AddBrushPaint(Paint paint, BaseBrush brush, Rect frame) { paint.SetStyle(Paint.Style.Fill); var sb = brush as SolidBrush; if (sb != null) { paint.SetARGB(sb.Color.A, sb.Color.R, sb.Color.G, sb.Color.B); return; } var lgb = brush as LinearGradientBrush; if (lgb != null) { var n = lgb.Stops.Count; if (n >= 2) { var locs = new float[n]; var comps = new int[n]; for (var i = 0; i < n; i++) { var s = lgb.Stops[i]; locs[i] = (float) s.Offset; comps[i] = s.Color.Argb; } var p1 = lgb.Absolute ? lgb.Start : frame.Position + lgb.Start*frame.Size; var p2 = lgb.Absolute ? lgb.End : frame.Position + lgb.End*frame.Size; var lg = new LinearGradient( (float) p1.X, (float) p1.Y, (float) p2.X, (float) p2.Y, comps, locs, Shader.TileMode.Clamp); paint.SetShader(lg); } return; } var rgb = brush as RadialGradientBrush; if (rgb != null) { var n = rgb.Stops.Count; if (n >= 2) { var locs = new float[n]; var comps = new int[n]; for (var i = 0; i < n; i++) { var s = rgb.Stops[i]; locs[i] = (float) s.Offset; comps[i] = s.Color.Argb; } var p1 = rgb.GetAbsoluteCenter(frame); var r = rgb.GetAbsoluteRadius(frame); var rg = new RadialGradient( (float) p1.X, (float) p1.Y, (float) r.Max, comps, locs, Shader.TileMode.Clamp); paint.SetShader(rg); } return; } throw new NotSupportedException("Brush " + brush); }
private Paint GetBrushPaint(BaseBrush brush, Rect frame) { var paint = new Paint(PaintFlags.AntiAlias); AddBrushPaint(paint, brush, frame); return paint; }
private void Read(XDocument doc) { var svg = doc.Root; var ns = svg.Name.Namespace; // // Find the defs (gradients) // foreach (var d in svg.Descendants()) { var idA = d.Attribute("id"); if (idA != null) { defs[ReadString(idA).Trim()] = d; } } // // Get the dimensions // var widthA = svg.Attribute("width"); var heightA = svg.Attribute("height"); var width = _valuesParser.ReadNumber(widthA); var height = _valuesParser.ReadNumber(heightA); var size = new Size(width, height); var viewBox = new Rect(size); var viewBoxA = svg.Attribute("viewBox") ?? svg.Attribute("viewPort"); if (viewBoxA != null) { viewBox = ReadRectangle(viewBoxA.Value); } if (widthA != null && widthA.Value.Contains("%")) { size.Width *= viewBox.Width; } if (heightA != null && heightA.Value.Contains("%")) { size.Height *= viewBox.Height; } // // Add the elements // Graphic = new Graphic(size, viewBox); AddElements(Graphic.Children, svg.Elements(), null, null); }
public void DrawImage(IImage image, Rect frame, double alpha = 1.0) { var i = GetImage (image); renderTarget.DrawBitmap (i, frame.ToRectangleF (), (float)alpha, D2D1.BitmapInterpolationMode.Linear); }
private Rect ReadRectangle(string s) { var r = new Rect(); var p = s.Split(WS, StringSplitOptions.RemoveEmptyEntries); if (p.Length > 0) r.X = _valuesParser.ReadNumber(p[0]); if (p.Length > 1) r.Y = _valuesParser.ReadNumber(p[1]); if (p.Length > 2) r.Width = _valuesParser.ReadNumber(p[2]); if (p.Length > 3) r.Height = _valuesParser.ReadNumber(p[3]); return r; }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, BaseBrush brush = null) { var layout = new DW.TextLayout (factories.DWFactory, text, GetTextFormat (font), (float)frame.Width, (float)frame.Height); var h = layout.Metrics.Height; //todo : : Attempted to read or write protected memory. This is often an indication that other memory is corrupt. //renderTarget.DrawTextLayout ((frame.TopLeft - h*Point.OneY.Y).ToVector2 (), layout, GetBrush (frame, brush)); }
public void DrawEllipse(Rect frame, Pen pen = null, BaseBrush brush = null) { if (brush != null) { var paint = GetBrushPaint(brush, frame); graphics.DrawOval(frame.GetRectF(), paint); } if (pen != null) { var paint = GetPenPaint(pen); graphics.DrawOval(frame.GetRectF(), paint); } }
public SurfaceImageSourceCanvas(SurfaceImageSource surfaceImageSource, Rect updateRect, Direct2DFactories factories = null) : base(factories) { sisn = ComObject.As<DXGI.ISurfaceImageSourceNative> (surfaceImageSource); SharpDX.Point offset; var surface = sisn.BeginDraw (updateRect.ToRectangle (), out offset); var dpi = 96.0; var properties = new D2D1.RenderTargetProperties (D2D1.RenderTargetType.Default, new D2D1.PixelFormat (DXGI.Format.Unknown, D2D1.AlphaMode.Unknown), (float)(dpi), (float)(dpi), D2D1.RenderTargetUsage.None, D2D1.FeatureLevel.Level_DEFAULT); Initialize (new D2D1.RenderTarget (this.factories.D2DFactory, surface, properties)); }
public void DrawImage(IImage image, Rect frame, double alpha = 1.0) { }