Added BitVectorFormatter (as well as ColorDefinition)
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Tue, 28 May 2019 21:29:28 +0000 (23:29 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Tue, 28 May 2019 21:29:28 +0000 (23:29 +0200)
era.mi/src/era/mi/logic/types/BitVectorFormatter.java [new file with mode: 0644]
era.mi/src/era/mi/logic/types/ColorDefinition.java [new file with mode: 0644]

diff --git a/era.mi/src/era/mi/logic/types/BitVectorFormatter.java b/era.mi/src/era/mi/logic/types/BitVectorFormatter.java
new file mode 100644 (file)
index 0000000..6dab1d7
--- /dev/null
@@ -0,0 +1,53 @@
+package era.mi.logic.types;
+
+import era.mi.logic.types.ColorDefinition.BuiltInColor;
+import era.mi.logic.wires.Wire.ReadEnd;
+
+public class BitVectorFormatter
+{
+       public static String formatValueAsString(ReadEnd end)
+       {
+               return formatAsString(end == null ? null : end.getValues());
+       }
+
+       public static String formatAsString(BitVector bitVector)
+       {
+               if (bitVector == null)
+                       return "null";
+               else
+                       return bitVector.toString();
+       }
+
+       public static ColorDefinition formatAsColor(ReadEnd end)
+       {
+               return formatAsColor(end == null ? null : end.getValues());
+       }
+
+       public static ColorDefinition formatAsColor(BitVector bitVector)
+       {
+               // TODO maybe find a color assignment for multiple-bit bit vectors?
+               if (bitVector == null || bitVector.length() != 1)
+                       return new ColorDefinition(BuiltInColor.COLOR_BLACK);
+               else
+                       switch (bitVector.getBit(0))
+                       {
+                       case ONE:
+                               return new ColorDefinition(BuiltInColor.COLOR_GREEN);
+                       case U:
+                               return new ColorDefinition(BuiltInColor.COLOR_CYAN);
+                       case X:
+                               return new ColorDefinition(BuiltInColor.COLOR_RED);
+                       case Z:
+                               return new ColorDefinition(BuiltInColor.COLOR_YELLOW);
+                       case ZERO:
+                               return new ColorDefinition(BuiltInColor.COLOR_GRAY);
+                       default:
+                               throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getBit(0));
+                       }
+       }
+
+       private BitVectorFormatter()
+       {
+               throw new UnsupportedOperationException("No BitVectorFormatter instances");
+       }
+}
\ No newline at end of file
diff --git a/era.mi/src/era/mi/logic/types/ColorDefinition.java b/era.mi/src/era/mi/logic/types/ColorDefinition.java
new file mode 100644 (file)
index 0000000..b9e851f
--- /dev/null
@@ -0,0 +1,54 @@
+package era.mi.logic.types;
+
+/**
+ * A way to define a color with the possibility to use colors built into the system (called "system colors" in SWT).
+ * <p>
+ * A {@link ColorDefinition} is defined either by a {@link BuiltInColor} constant, in which case <code>r==g==b==-1</code>, or by red / green
+ * / blue components, in which case <code>builtInColor==null</code>
+ */
+public class ColorDefinition
+{
+       /**
+        * The built-in color constant defining this color.
+        */
+       public final ColorDefinition.BuiltInColor builtInColor;
+       /**
+        * The red color component defining this color.
+        */
+       public final int r;
+       /**
+        * The green color component defining this color.
+        */
+       public final int g;
+       /**
+        * The blue color component defining this color.
+        */
+       public final int b;
+
+       public ColorDefinition(ColorDefinition.BuiltInColor col)
+       {
+               if (col == null)
+                       throw new IllegalArgumentException("Illegal built-in color: " + col);
+               this.builtInColor = col;
+               this.r = -1;
+               this.g = -1;
+               this.b = -1;
+       }
+
+       public ColorDefinition(int r, int g, int b)
+       {
+               if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255)
+                       throw new IllegalArgumentException("Illegal color components: r=" + r + "; g=" + g + "; b=" + b);
+               this.builtInColor = null;
+               this.r = r;
+               this.g = g;
+               this.b = b;
+       }
+
+       public static enum BuiltInColor
+       {
+               COLOR_WHITE, COLOR_BLACK, COLOR_RED, COLOR_DARK_RED, COLOR_GREEN, COLOR_DARK_GREEN, COLOR_YELLOW, COLOR_DARK_YELLOW, COLOR_BLUE,
+               COLOR_DARK_BLUE, COLOR_MAGENTA, COLOR_DARK_MAGENTA, COLOR_CYAN, COLOR_DARK_CYAN, COLOR_GRAY, COLOR_DARK_GRAY;
+       }
+
+}
\ No newline at end of file