Esempio n. 1
0
        public void ImageShouldBeOverlayedPolygonOutlineWithOpacity()
        {
            string path = TestEnvironment.CreateOutputDirectory("Drawing", "Polygons");

            SixLabors.Primitives.PointF[] simplePath = new SixLabors.Primitives.PointF[] {
                new Vector2(10, 10),
                new Vector2(200, 150),
                new Vector2(50, 300)
            };

            Rgba32 color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150);

            using (Image <Rgba32> image = new Image <Rgba32>(500, 500))
            {
                image.Mutate(x => x
                             .BackgroundColor(Rgba32.Blue)
                             .DrawPolygon(color, 10, simplePath));
                image.Save($"{path}/Opacity.png");

                //shift background color towards forground color by the opacity amount
                Rgba32 mergedColor = new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f));

                using (PixelAccessor <Rgba32> sourcePixels = image.Lock())
                {
                    Assert.Equal(mergedColor, sourcePixels[9, 9]);

                    Assert.Equal(mergedColor, sourcePixels[199, 149]);

                    Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]);

                    Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]);
                }
            }
        }
Esempio n. 2
0
        private static IEnumerable <SixLabors.Primitives.PointF[]> GetRectangles(string data)
        {
            var faces = JArray.Parse(data);

            foreach (var face in faces)
            {
                var id = (string)face["faceID"];

                var top    = (int)face["faceRectangle"]["top"];
                var left   = (int)face["faceRectangle"]["left"];
                var width  = (int)face["faceRectangle"]["width"];
                var height = (int)face["faceRectangle"]["height"];

                var rectangle = new SixLabors.Primitives.PointF[]
                {
                    new SixLabors.Primitives.PointF(left, top),
                    new SixLabors.Primitives.PointF(left + width, top),
                    new SixLabors.Primitives.PointF(left + width, top + height),
                    new SixLabors.Primitives.PointF(left, top + height)
                };
                yield return(rectangle);
            }
        }
Esempio n. 3
0
        public override byte[] Render(string mode = "human")
        {
            float b;
            float t;
            float r;
            float l;
            var   screen_width  = 600;
            var   screen_height = 400;
            var   world_width   = x_threshold * 2;
            var   scale         = screen_width / world_width;
            var   carty         = 300;
            var   polewidth     = 10.0f;
            var   poleheight    = scale * (2 * length);
            var   cartwidth     = 50.0f;
            var   cartheight    = 30.0f;

            if (viewer == null)
            {
                lock (this) {
                    //to prevent double initalization.
                    if (viewer == null)
                    {
                        viewer = Viewer.Run(screen_width, screen_height, "cartpole-v1");
                    }
                }
            }

            //pole
            l = -polewidth / 2;
            r = polewidth / 2;
            t = poleheight - polewidth / 2;
            b = -polewidth / 2;
            var pole   = new RectangularPolygon(-polewidth / 2, carty - poleheight, polewidth, poleheight);
            var circle = new EllipsePolygon(0, carty - polewidth / 2, polewidth / 2);

            //cart
            l = -cartwidth / 2;
            r = cartwidth / 2;
            t = cartheight / 2;
            b = -cartheight / 2;
            var axleoffset = cartheight / 4.0;
            var cart       = new RectangularPolygon(-cartwidth / 2, carty - cartheight / 2, cartwidth, cartheight);
            var draw       = new List <(IPath, Rgba32)>();

            if (!Equals(state, null))
            {
                var center_x = (float)(state.GetDouble(0) * scale + screen_width / 2.0f);
                //no y cuz it doesnt change.
                var cbounds    = circle.Bounds;
                var pivotPoint = new PointF(cbounds.X + cbounds.Width / 2f, cbounds.Y + cbounds.Height / 2f);

                draw.Add((cart.Translate(center_x, 0), Rgba32.Black));
                draw.Add((pole.Transform(Matrix3x2.CreateRotation((float)-state.GetDouble(2), pivotPoint)).Translate(center_x, 0), new Rgba32(204, 153, 102)));
                draw.Add((circle.Translate(center_x, 0), Rgba32.Teal));
            }
            else
            {
                draw.Add((pole, Rgba32.Orange));
                draw.Add((cart, Rgba32.Black));
                draw.Add((circle, Rgba32.Teal));
            }

            var img = new Image <Rgba32>(screen_width, screen_height);

            //line
            img.Mutate(i => i.BackgroundColor(Rgba32.White));
            img.Mutate(i => i.BackgroundColor(Rgba32.White));
            img.Mutate(i => i.Fill(Rgba32.Black, new RectangularPolygon(new PointF(0, carty), new PointF(screen_width, carty + 1))));
            foreach (var(path, rgba32) in draw)
            {
                img.Mutate(i => i.Fill(rgba32, path));
            }

            viewer.Render(img);

            //todo if (mode == "rgb_array") {
            //todo     ImageConverter converter = new ImageConverter();
            //todo     return (byte[]) converter.ConvertTo(((ImageImage) canvas.GetImage()).Image, typeof(byte[]));
            //todo }

            return(null);
        }
Esempio n. 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SizeF"/> struct from the given <see cref="PointF"/>.
 /// </summary>
 /// <param name="point">The point</param>
 public SizeF(PointF point)
 {
     this.Width  = point.X;
     this.Height = point.Y;
 }