Exemple #1
0
        public void RoboHelper_IsCommandLineArgumentsValid_Should_Throw_Correct_Exeption(string command, string exceptionMessage)
        {
            var    commandSegments = RoboHelper.GetCommandSegments(command);
            Action act             = () => RoboHelper.IsCommandLineArgumentsValid(commandSegments);

            act.Should().Throw <ArgumentException>().WithMessage(exceptionMessage);
        }
Exemple #2
0
        public void RoboHelper_GetCommandSegments_Should_Return_Correct_Result(string command, string action, string x, string y, string direction)
        {
            var result = RoboHelper.GetCommandSegments(command);

            result.X.Should().Be(x);
            result.Y.Should().Be(y);
            result.Direction.Should().Be(direction);
            result.Action.Should().Be(action);
        }
Exemple #3
0
        public void RoboHelper_IsCordinateValid_Should_Throw_Correct_Exeption(string command, string exceptionMessage, int height, int width)
        {
            var commandSegments = RoboHelper.GetCommandSegments(command);

            var moveAreaMock = new Mock <IMoveArea <int> >();

            moveAreaMock.SetupGet(s => s.Width).Returns(width);
            moveAreaMock.SetupGet(s => s.Height).Returns(height);

            Action act = () => RoboHelper.IsCordinateValid <int>(commandSegments, moveAreaMock.Object);

            act.Should().Throw <ArgumentException>().WithMessage(exceptionMessage);
        }
Exemple #4
0
        /// <summary>
        /// Renders the files.
        /// </summary>
        /// <param name="srcFiles">The source files.</param>
        /// <param name="backgroundColor">Color of the background.</param>
        /// <param name="srcWidth">Width of the source.</param>
        /// <param name="srcHeight">Height of the source.</param>
        /// <param name="destWidth">Width of the dest.</param>
        /// <param name="destHeight">Height of the dest.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        protected override Image RenderFiles(IEnumerable <string> srcFiles, string backgroundColor, int srcWidth, int srcHeight, int destWidth, int destHeight, Options options)
        {
            var retval = new Bitmap(srcWidth, srcHeight);

            using (var canvas = Graphics.FromImage(retval))
            {
                canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                if (!string.IsNullOrWhiteSpace(backgroundColor))
                {
                    var color = RoboHelper.ConvertHexColor(backgroundColor);
                    using (var brush = new SolidBrush(color))
                        canvas.FillRectangle(brush, 0, 0, srcWidth, srcHeight);
                }

                foreach (var imageFile in srcFiles)
                {
                    using (var image = Image.FromFile(imageFile))
                        canvas.DrawImage(image, new Rectangle(0, 0, srcWidth, srcHeight),
                                         new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                }
                canvas.Save();
            }

            if (srcWidth != destWidth || srcHeight != destHeight)
            {
                var resizedImage = new Bitmap(retval, destWidth, destHeight);
                retval.Dispose();

                retval = resizedImage;
            }

            if (options.HasFlag(Options.Grayscale))
            {
                RoboHelper.MakeBlackAndWhite(ref retval);
            }
            if (options.HasFlag(Options.Blur))
            {
                RoboHelper.Blur(ref retval, 5);
            }

            return(retval);
        }
Exemple #5
0
        private void ProcessInitialPlacement(string command)
        {
            var commandSegments = RoboHelper.GetCommandSegments(command);

            //this will throw exception if the commads are invalid
            //This is done this way so that the detail error messages are captured for ease of user
            RoboHelper.IsCommandLineArgumentsValid(commandSegments);

            //Check if the original cordinate is valid
            RoboHelper.IsCordinateValid <T>(commandSegments, MoveArea);

            //set the initial position
            Position = new Cordinate <T>
            {
                X = (T)Convert.ChangeType(commandSegments.X, typeof(T)),
                Y = (T)Convert.ChangeType(commandSegments.Y, typeof(T))
            };
            //set the initial direction
            Direction = DirectionFactory <T, U> .Create(commandSegments.Direction);
        }
Exemple #6
0
        /*
         * protected override Image RenderFiles(IEnumerable<string> srcFiles, int srcWidth, int srcHeight, int destWidth, int destHeight)
         * {
         *  var retval = new Bitmap(srcWidth, srcHeight);
         *  using (var canvas = Graphics.FromImage(retval))
         *  {
         *      canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
         *      foreach (var imageFile in srcFiles)
         *      {
         *          using (var image = Image.FromFile(imageFile))
         *              canvas.DrawImage(image, new Rectangle(0, 0, srcWidth, srcHeight),
         *                  new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
         *      }
         *      canvas.Save();
         *  }
         *
         *  if (srcWidth != destWidth || srcHeight != destHeight)
         *  {
         *      var resizedImage = new Bitmap(retval, destWidth, destHeight);
         *      retval.Dispose();
         *
         *      retval = resizedImage;
         *  }
         *
         *  return retval;
         * }*/
        protected override ImageSource RenderFiles(IEnumerable <string> srcFiles, string backgroundColor, int srcWidth, int srcHeight, int destWidth, int destHeight, Options options)
        {
            var retval = new WriteableBitmap(srcWidth, srcHeight);

            if (backgroundColor != null)
            {
                var color = RoboHelper.ConvertHexColor(backgroundColor);
                retval.FillRectangle(0, 0, srcHeight, srcHeight, color);
            }

            foreach (var imageFile in srcFiles)
            {
                var image = new BitmapImage();
                using (var fs = new FileStream(imageFile, FileMode.Open))
                    image.SetSource(fs);
                retval.Blit(new Rect(0, 0, srcWidth, srcHeight), new WriteableBitmap(image), new Rect(0, 0, image.PixelWidth, image.PixelHeight));

                //TOOD implement options
            }
            return(retval);
        }
Exemple #7
0
        public void RoboHelper_IsPlaceExist_Should_Return_Correct_Response(bool response, params string[] commands)
        {
            var result = RoboHelper.IsPlaceExist(commands);

            result.Should().Be(response);
        }
Exemple #8
0
        public void RoboHelper_IsValidDirection_Should_Return_Correct_Response(string direction, bool response)
        {
            var result = RoboHelper.IsValidDirection(direction);

            result.Should().Be(response);
        }
Exemple #9
0
        public Image Render(string set, string backgroundSet, string color, int width, int height, Options options = Options.None)
        {
            Image image1 = null, image2 = null, image3 = null;

            try
            {
                var facHeight2 = (int)(height * 0.85);
                var facWidth2  = (int)(width * 0.85);
                var facHeight3 = (int)(height * 0.90);
                var facWidth3  = (int)(width * 0.90);


                image1 = RenderOne(_hexDigest1, width, height);
                image2 = RenderOne(_hexDigest2, facWidth2, facHeight2);
                image3 = RenderOne(_hexDigest3, facWidth3, facHeight3);

                var robo                = RoboHash.Create(Xor(Xor(_hexDigest1, _hexDigest2), _hexDigest3));
                var backgroundColor     = backgroundSet != null && backgroundSet.StartsWith("#") ? backgroundSet : null;
                var backgroundImageName = backgroundColor == null?robo.GetBackgroundImageFileName(backgroundSet) : null;

                var retval = new Bitmap(width, height);
                try
                {
                    using (var canvas = Graphics.FromImage(retval))
                    {
                        canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        if (backgroundColor != null)
                        {
                            using (var brush = new SolidBrush(RoboHelper.ConvertHexColor(backgroundColor)))
                                canvas.FillRectangle(brush, 0, 0, width, height);
                        }
                        else if (!string.IsNullOrWhiteSpace(backgroundImageName))
                        {
                            using (var image = Image.FromFile(backgroundImageName))
                            {
                                canvas.DrawImage(image, new Rectangle(0, 0, width, height),
                                                 new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                            }
                        }

                        var x = (width - width) / 2;
                        var y = (height - height) / 2;

                        var left  = (int)(width / 4.0 - width * 0.05);
                        var right = (int)(width / 4.0 + width * 0.1);

                        var offHeight2 = height - facHeight2;
                        var offHeight3 = height - facHeight3;

                        canvas.DrawImage(image2, new Rectangle(x - left, y + offHeight2, image2.Width, image2.Height),
                                         new Rectangle(0, 0, image2.Width, image2.Height), GraphicsUnit.Pixel);

                        canvas.DrawImage(image3, new Rectangle(x + right, y + offHeight3, image3.Width, image3.Height),
                                         new Rectangle(0, 0, image3.Width, image3.Height), GraphicsUnit.Pixel);

                        canvas.DrawImage(image1, new Rectangle(x, y, image1.Width, image1.Height),
                                         new Rectangle(0, 0, image1.Width, image1.Height), GraphicsUnit.Pixel);
                    }


                    if (options.HasFlag(Options.Grayscale))
                    {
                        RoboHelper.MakeBlackAndWhite(ref retval);
                    }
                    if (options.HasFlag(Options.Blur))
                    {
                        RoboHelper.Blur(ref retval, 5);
                    }
                }
                catch
                {
                    retval.Dispose();
                    retval = null;
                }
                return(retval);
            }
            finally
            {
                if (image1 != null)
                {
                    image1.Dispose();
                }
                if (image2 != null)
                {
                    image2.Dispose();
                }
                if (image3 != null)
                {
                    image3.Dispose();
                }
            }
        }
Exemple #10
0
        public string ProcessAction(string command)
        {
            var commandSegments = RoboHelper.GetCommandSegments(command);

            //check
            if (commandSegments != null)
            {
                //this will throw exception if the commads are invalid
                //This is done this way so that the detail error messages are captured for ease of user
                RoboHelper.IsCommandLineArgumentsValid(commandSegments);

                //Check if the original cordinate is valid
                RoboHelper.IsCordinateValid <T>(commandSegments, MoveArea);

                //set the new position
                Position = new Cordinate <T>
                {
                    X = (T)Convert.ChangeType(commandSegments.X, typeof(T)),
                    Y = (T)Convert.ChangeType(commandSegments.Y, typeof(T))
                };
                //set the new direction
                Direction = DirectionFactory <T, U> .Create(commandSegments.Direction);
            }
            else
            {
                if (RoboHelper.IsValidReportAction(command))
                {
                    return(new Report <T, U>().GetOutput(Position, Direction));
                }

                if (RoboHelper.IsPlaceExist(new string[] { command }))
                {
                    throw new ArgumentException($"PLACE command missing arguments");
                }

                if (!RoboHelper.IsValidMoveAction(command))
                {
                    throw new ArgumentException($"Invalid command line arguments");
                }

                if (Direction == null)
                {
                    throw new InvalidOperationException("Robo unable to perform this command without initial direction");
                }

                if (Position == null)
                {
                    throw new InvalidOperationException("Robo unable to perform this command without initial position");
                }

                //create the action
                var action = ActionFactory <T, U> .Create(command);

                //get the new direction
                Direction = Direction.NextFacing(action);
                //calculate the new position
                Position = action.ComputeNewCordinate(Position, Step, MoveArea, Direction);
            }

            return(string.Empty);
        }