From ba9a2b22b7b87e06a5ad7d8a2050c2d8298aa66b Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Tue, 28 May 2019 23:29:28 +0200 Subject: [PATCH] Added BitVectorFormatter (as well as ColorDefinition) --- .../mi/logic/types/BitVectorFormatter.java | 53 ++++++++++++++++++ .../era/mi/logic/types/ColorDefinition.java | 54 +++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 era.mi/src/era/mi/logic/types/BitVectorFormatter.java create mode 100644 era.mi/src/era/mi/logic/types/ColorDefinition.java 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 index 00000000..6dab1d7b --- /dev/null +++ b/era.mi/src/era/mi/logic/types/BitVectorFormatter.java @@ -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 index 00000000..b9e851fd --- /dev/null +++ b/era.mi/src/era/mi/logic/types/ColorDefinition.java @@ -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). + *

+ * A {@link ColorDefinition} is defined either by a {@link BuiltInColor} constant, in which case r==g==b==-1, or by red / green + * / blue components, in which case builtInColor==null + */ +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 -- 2.17.1