public void ColorerTest() { // StringToString Assert.AreEqual(Colorer.StringToString("a"), "ffaaaaaa"); Assert.AreEqual(Colorer.StringToString("AB"), "ffababab"); Assert.AreEqual(Colorer.StringToString("cD0"), "ffccdd00"); Assert.AreEqual(Colorer.StringToString("C0DE"), "cc00ddee"); try { Colorer.StringToString("ad0be"); Assert.Fail(); } catch { } Assert.AreEqual(Colorer.StringToString("dEc0dE"), "ffdec0de"); try { Colorer.StringToString("dec0ded"); Assert.Fail(); } catch { } Assert.AreEqual(Colorer.StringToString("F00dFaCe"), "f00dface"); try { Colorer.StringToString("coder"); Assert.Fail(); } catch { } try { Colorer.StringToString("deadbeeff00d"); Assert.Fail(); } catch { } // StringToValue Assert.AreEqual(Colorer.StringToValue("a"), 0xffaaaaaa); Assert.AreEqual(Colorer.StringToValue("AB"), 0xffababab); Assert.AreEqual(Colorer.StringToValue("cD0"), 0xffccdd00); Assert.AreEqual(Colorer.StringToValue("C0DE"), 0xcc00ddee); try { Colorer.StringToValue("ad0be"); Assert.Fail(); } catch { } Assert.AreEqual(Colorer.StringToValue("dEc0dE"), 0xffdec0de); try { Colorer.StringToValue("dec0ded"); Assert.Fail(); } catch { } Assert.AreEqual(Colorer.StringToValue("F00dFaCe"), 0xf00dface); try { Colorer.StringToValue("coder"); Assert.Fail(); } catch { } try { Colorer.StringToValue("deadbeeff00d"); Assert.Fail(); } catch { } // ValueToString Assert.AreEqual(Colorer.ValueToString(0xffaaaaaa), "ffaaaaaa"); Assert.AreEqual(Colorer.ValueToString(0xffababab), "ffababab"); Assert.AreEqual(Colorer.ValueToString(0xffccdd00), "ffccdd00"); Assert.AreEqual(Colorer.ValueToString(0xcc00ddee), "cc00ddee"); Assert.AreEqual(Colorer.ValueToString(0xffdec0de), "ffdec0de"); Assert.AreEqual(Colorer.ValueToString(0xf00dface), "f00dface"); // ValueToARGB ValueToARGB(0xffaaaaaa, 0xff, 0xaa, 0xaa, 0xaa); ValueToARGB(0xffababab, 0xff, 0xab, 0xab, 0xab); ValueToARGB(0xffccdd00, 0xff, 0xcc, 0xdd, 0x00); ValueToARGB(0xcc00ddee, 0xcc, 0x00, 0xdd, 0xee); ValueToARGB(0xffdec0de, 0xff, 0xde, 0xc0, 0xde); ValueToARGB(0xf00dface, 0xf0, 0x0d, 0xfa, 0xce); // StringToARGB StringToARGB("a", 0xff, 0xaa, 0xaa, 0xaa); StringToARGB("AB", 0xff, 0xab, 0xab, 0xab); StringToARGB("cD0", 0xff, 0xcc, 0xdd, 0x00); StringToARGB("C0DE", 0xcc, 0x00, 0xdd, 0xee); try { StringToARGB("ad0be", 0, 0, 0, 0); Assert.Fail(); } catch { } StringToARGB("dEc0dE", 0xff, 0xde, 0xc0, 0xde); try { StringToARGB("dec0ded", 0, 0, 0, 0); Assert.Fail(); } catch { } StringToARGB("F00dFaCe", 0xf0, 0x0d, 0xfa, 0xce); try { StringToARGB("coder", 0, 0, 0, 0); Assert.Fail(); } catch { } try { StringToARGB("deadbeeff00d", 0, 0, 0, 0); Assert.Fail(); } catch { } // ARGBToString Assert.AreEqual(Colorer.ARGBToString(0xff, 0xaa, 0xaa, 0xaa), "ffaaaaaa"); Assert.AreEqual(Colorer.ARGBToString(0xff, 0xab, 0xab, 0xab), "ffababab"); Assert.AreEqual(Colorer.ARGBToString(0xff, 0xcc, 0xdd, 0x00), "ffccdd00"); Assert.AreEqual(Colorer.ARGBToString(0xcc, 0x00, 0xdd, 0xee), "cc00ddee"); Assert.AreEqual(Colorer.ARGBToString(0xff, 0xde, 0xc0, 0xde), "ffdec0de"); Assert.AreEqual(Colorer.ARGBToString(0xf0, 0x0d, 0xfa, 0xce), "f00dface"); // ARGBToValue Assert.AreEqual(Colorer.ARGBToValue(0xff, 0xaa, 0xaa, 0xaa), 0xffaaaaaa); Assert.AreEqual(Colorer.ARGBToValue(0xff, 0xab, 0xab, 0xab), 0xffababab); Assert.AreEqual(Colorer.ARGBToValue(0xff, 0xcc, 0xdd, 0x00), 0xffccdd00); Assert.AreEqual(Colorer.ARGBToValue(0xcc, 0x00, 0xdd, 0xee), 0xcc00ddee); Assert.AreEqual(Colorer.ARGBToValue(0xff, 0xde, 0xc0, 0xde), 0xffdec0de); Assert.AreEqual(Colorer.ARGBToValue(0xf0, 0x0d, 0xfa, 0xce), 0xf00dface); }
void Command_Image_Crop(ImageCropDialog.Result result) { var variables = GetVariables(); var destX = new NEExpression(result.XExpression).Evaluate <int>(variables); var destY = new NEExpression(result.YExpression).Evaluate <int>(variables); var newWidth = new NEExpression(result.WidthExpression).Evaluate <int>(variables); var newHeight = new NEExpression(result.HeightExpression).Evaluate <int>(variables); if ((newWidth <= 0) || (newHeight <= 0)) { throw new Exception("Width and height must be greater than 0"); } var bitmap = GetBitmap(); var srcX = 0; var srcY = 0; var width = bitmap.Width; var height = bitmap.Height; if (destX < 0) { width += destX; srcX -= destX; destX = 0; } if (destY < 0) { height += destY; srcY -= destY; destY = 0; } width = Math.Min(width, newWidth - destX); height = Math.Min(height, newHeight - destY); var resultBitmap = new System.Drawing.Bitmap(newWidth, newHeight, bitmap.PixelFormat); resultBitmap.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution); using (var graphics = System.Drawing.Graphics.FromImage(resultBitmap)) using (var wrapMode = new System.Drawing.Imaging.ImageAttributes()) { graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb((int)Colorer.StringToValue(result.FillColor))), new System.Drawing.Rectangle(System.Drawing.Point.Empty, resultBitmap.Size)); graphics.DrawImage(bitmap, new System.Drawing.Rectangle(destX, destY, width, height), new System.Drawing.Rectangle(srcX, srcY, width, height), System.Drawing.GraphicsUnit.Pixel); } Replace(new List <Range> { FullRange }, new List <string> { Coder.BitmapToString(resultBitmap) }); SetSelections(new List <Range> { BeginRange }); }