Added pen color

This commit is contained in:
Lukasz Baldyga committed 2020-10-25 09:11:45 +00:00
1 parent 7a98404d4b
commit f3316955b5
4 files changed
+65 -1

No files matched your search

@@ -72,6 +72,7 @@
<Compile Include="Verbs\DrawingVerbs\DrawLines.cs" />
<Compile Include="Verbs\DrawingVerbs\DrawTo.cs" />
<Compile Include="Verbs\DrawingVerbs\MoveTo.cs" />
<Compile Include="Verbs\DrawingVerbs\PenColor.cs" />
<Compile Include="Verbs\DrawingVerbs\Quadrilateral.cs" />
<Compile Include="Verbs\DrawingVerbs\Rectangle.cs" />
<Compile Include="Verbs\DrawingVerbs\RegularPolygon.cs" />
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Advanced_Software_Engineering {
class PenColor : Verb {
Drawer drawer;
Color color;
public PenColor(Drawer drawer, Color color) {
this.drawer = drawer;
this.color = color;
}
public void ExecuteVerb() {
drawer.SetPenColor(color);
}
public string GetDescription() {
return "Set pen color to: " + color.ToString();
}
}
}
@@ -21,5 +21,38 @@ namespace Advanced_Software_Engineering {
public static Point ConvertToPoint(string text1, string text2) {
return new Point(ConvertToInt(text1), ConvertToInt(text2));
}
//TODO: Make unit tests for this function
public static Color TextToColor(string text) {
Color color = Color.FromName(text);
if (color.IsKnownColor) return color;
if (text.StartsWith("#"))
text = text.Substring(1);
switch (text.Length) {
case 6:
return Color.FromArgb(
int.Parse(text.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(text.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(text.Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
case 8:
return Color.FromArgb(
int.Parse(text.Substring(6, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(text.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(text.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(text.Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
default:
throw new Exception("Color not valid");
}
}
public static Color IntsToColor(int r, int g, int b, int a = 0) {
return Color.FromArgb(a, r, g, b);
}
}
}
@@ -144,8 +144,11 @@ namespace Advanced_Software_Engineering {
} else throw new Exception("Command doesn't take parameters");
case "pen":
if(parameters.Length == 1) {
return new PenColor(drawer, Value.TextToColor(parameters[0]));
} else throw new Exception("Command has an incorrect number of parameters");
case "fill":
throw new Exception("Not implemented");
default:
throw new Exception("Command not found");