Skip to content

Commit dca8cfe

Browse files
committed
graphic: improve dump styling
- Class names are in bold - Colors now have the #RRGGBB part of their textual representation colored with the actual color - Field names are dimmed - Children begin "inline" rather than on a new line
1 parent 6f1e179 commit dca8cfe

File tree

16 files changed

+183
-116
lines changed

16 files changed

+183
-116
lines changed

lib/src/main/java/jtamaro/graphic/ActionableGraphic.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ SequencedMap<String, Graphic> getChildren() {
6666
}
6767

6868
@Override
69-
protected SequencedMap<String, String> getProps(boolean plainText) {
70-
final SequencedMap<String, String> props = super.getProps(plainText);
69+
protected SequencedMap<String, String> getProps(PropStyle propStyle) {
70+
final SequencedMap<String, String> props = super.getProps(propStyle);
7171
if (!pressAction.isEmpty()) {
7272
props.put("onPress", "event");
7373
}

lib/src/main/java/jtamaro/graphic/CircularSector.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,11 @@ protected String getInspectLabel() {
6363
}
6464

6565
@Override
66-
protected SequencedMap<String, String> getProps(boolean plainText) {
67-
final SequencedMap<String, String> props = super.getProps(plainText);
66+
protected SequencedMap<String, String> getProps(PropStyle propStyle) {
67+
final SequencedMap<String, String> props = super.getProps(propStyle);
6868
props.put("radius", String.format("%.2f", radius));
6969
props.put("angle", String.format("%.2f", radius));
70-
props.put("color", plainText
71-
? color.toString()
72-
: Colors.htmlString(color));
70+
props.put("color", Colors.format(propStyle, color));
7371
return props;
7472
}
7573

lib/src/main/java/jtamaro/graphic/Color.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public int alpha() {
3131

3232
@Override
3333
public String toString() {
34-
return String.format("Color[rgb=#%1$02X%2$02X%3$02X, opacity=%4$.2f]", red, green, blue, opacity);
34+
return String.format(
35+
"Color[rgb=#%1$02X%2$02X%3$02X, opacity=%4$.2f]",
36+
red,
37+
green,
38+
blue,
39+
opacity
40+
);
3541
}
3642
}

lib/src/main/java/jtamaro/graphic/Colors.java

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,24 +165,47 @@ public static Color hsva(double hue, double saturation, double value, double opa
165165
return new Color(red, green, blue, opacity);
166166
}
167167

168+
/**
169+
* Return a textual visualization for the given color following the given style.
170+
*/
171+
static String format(Graphic.PropStyle style, Color color) {
172+
return switch (style) {
173+
case PLAIN -> color.toString();
174+
case ANSI_ESCAPE_CODES -> formatAnsiEscape(color);
175+
case HTML -> formatHtml(color);
176+
};
177+
}
178+
179+
/**
180+
* Return a textual visualization for the given color with ansi escape codes that use the color
181+
* itself to colorize its representation.
182+
*
183+
* @hidden
184+
*/
185+
public static String formatAnsiEscape(Color color) {
186+
return String.format(
187+
"Color[rgb=\033[38;2;%5$sm\033[48;2;%1$d;%2$d;%3$dm#%1$02X%2$02X%3$02X\033[0m, opacity=%4$.2f]",
188+
color.red(),
189+
color.green(),
190+
color.blue(),
191+
color.opacity(),
192+
isLight(color) ? "0;0;0" : "255;255;255"
193+
);
194+
}
195+
168196
/**
169197
* Return an HTML string of a span with text color that of the given color and for content its
170198
* <pre>#RRGGBB</pre> representation.
171199
*/
172-
static String htmlString(Color color) {
173-
final double luminance = 0.2126 * color.red() / 255.0
174-
+ 0.7152 * color.green() / 255.0
175-
+ 0.0722 * color.blue() / 255.0;
176-
final String foregroundColor = luminance > 0.5
177-
? "#000000"
178-
: "#ffffff";
200+
private static String formatHtml(Color color) {
179201
return String.format(
180202
"<font bgcolor=\"rgba(%1$d,%2$d,%3$d,%4$.2f)\" color=\"%5$s\">&nbsp;#%1$02x%2$02x%3$02x&nbsp;</font>",
181203
color.red(),
182204
color.green(),
183205
color.blue(),
184206
color.opacity(),
185-
foregroundColor);
207+
isLight(color) ? "#000000" : "#ffffff"
208+
);
186209
}
187210

188211
/**
@@ -191,4 +214,19 @@ static String htmlString(Color color) {
191214
public static java.awt.Color toAwtColor(Color color) {
192215
return new java.awt.Color(color.red(), color.green(), color.blue(), color.alpha());
193216
}
217+
218+
/**
219+
* Return whether the given color is light or dark.
220+
*
221+
* @implNote Formula taken from Item 3.5 of the
222+
* <a href="https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.709-6-201506-I!!PDF-E.pdf">
223+
* Parameter values for the HDTV standards for production and international programme
224+
* exchange</a>
225+
*/
226+
private static boolean isLight(Color color) {
227+
final double luminance = 0.2126 * color.red() / 255.0
228+
+ 0.7152 * color.green() / 255.0
229+
+ 0.0722 * color.blue() / 255.0;
230+
return luminance > 0.5;
231+
}
194232
}

lib/src/main/java/jtamaro/graphic/Ellipse.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@ protected String getInspectLabel() {
6060
}
6161

6262
@Override
63-
protected SequencedMap<String, String> getProps(boolean plainText) {
64-
final SequencedMap<String, String> props = super.getProps(plainText);
65-
props.put("color", plainText
66-
? color.toString()
67-
: Colors.htmlString(color));
63+
protected SequencedMap<String, String> getProps(PropStyle propStyle) {
64+
final SequencedMap<String, String> props = super.getProps(propStyle);
65+
props.put("color", Colors.format(propStyle, color));
6866
return props;
6967
}
7068

lib/src/main/java/jtamaro/graphic/Graphic.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,31 +226,32 @@ public final String dump() {
226226
*
227227
* @implSpec Remember to invoke {@code super.dump(sb, indent)} first, then add all children using
228228
* {@link #dumpChild(StringBuilder, String, String, Graphic)}.
229-
* @implNote All the props from {@link #getProps(boolean)} are automatically added by invoking the
230-
* super method.
229+
* @implNote All the props from {@link #getProps(PropStyle)} are automatically added by invoking
230+
* the super method.
231231
*/
232232
protected final void dump(StringBuilder sb, String indent) {
233-
sb.append(indent)
233+
sb.append("\033[1m")
234234
.append(getClass().getSimpleName())
235+
.append("\033[0m")
235236
.append("\n");
236-
getProps(true).forEach((k, v) -> dumpField(sb, indent, k, v));
237+
getProps(PropStyle.ANSI_ESCAPE_CODES).forEach((k, v) -> dumpField(sb, indent, k, v));
237238
getChildren().forEach((k, v) -> dumpChild(sb, indent, k, v));
238239
}
239240

240241
private void dumpField(StringBuilder sb, String indent, String name, Object value) {
241242
sb.append(indent)
242-
.append("├─ ")
243+
.append("├─ \033[2m")
243244
.append(name)
244-
.append(": ")
245+
.append("\033[0m: ")
245246
.append(value)
246247
.append("\n");
247248
}
248249

249250
private void dumpChild(StringBuilder sb, String indent, String name, Graphic child) {
250251
sb.append(indent)
251-
.append("├─ ")
252+
.append("├─ \033[2m")
252253
.append(name)
253-
.append(":\n");
254+
.append("\033[0m: ");
254255
child.dump(sb, indent + "│ ");
255256
}
256257

@@ -272,21 +273,35 @@ protected String getInspectLabel() {
272273
return "graphic";
273274
}
274275

276+
277+
/**
278+
* Properties of the graphic.
279+
*
280+
* <p>Same as {@link #getProps(PropStyle)}, with {@link PropStyle#PLAIN} as the style.
281+
*
282+
* @implSpec The returned map is supposed to be mutable in non final-classes that override this
283+
* method so that it's easier for subclasses to add more entries to it.
284+
* @see #getProps(PropStyle)
285+
*/
286+
protected SequencedMap<String, String> getProps() {
287+
return getProps(PropStyle.PLAIN);
288+
}
289+
275290
/**
276291
* Properties of the graphic.
277292
*
278293
* <p>These are used to produce various representations of the graphic.
279294
*
280-
* @param plainText Whether the value strings be in plain text ({@code true}) or formatted in HTML
281-
* markup ({@code false}).
295+
* @param propStyle Whether the value strings be in plain text, formatted in HTML or ANSI escape
296+
* codes markup.
282297
* @implSpec The returned map is supposed to be mutable in non final-classes that override this
283298
* method so that it's easier for subclasses to add more entries to it.
284299
* @see #dump(StringBuilder, String)
285300
* @see #toString()
286301
* @see GuiGraphicPropertiesPanel
287302
* @see GuiGraphicTreeCellRenderer
288303
*/
289-
protected SequencedMap<String, String> getProps(boolean plainText) {
304+
protected SequencedMap<String, String> getProps(PropStyle propStyle) {
290305
// We use LinkedHashMap as the implementation class because:
291306
// 1. We care about insertion order
292307
// 2. We never perform random reads on the map, just forEach iterations
@@ -327,7 +342,7 @@ public int hashCode() {
327342
public final String toString() {
328343
return getClass().getSimpleName()
329344
+ "["
330-
+ getProps(true).entrySet().stream()
345+
+ getProps(PropStyle.PLAIN).entrySet().stream()
331346
.map(e -> e.getKey() + "=" + e.getValue())
332347
.collect(Collectors.joining(", "))
333348
+ "]";
@@ -351,4 +366,13 @@ public Graphic getGraphic() {
351366
return Graphic.this;
352367
}
353368
}
369+
370+
/**
371+
* @hidden
372+
*/
373+
protected enum PropStyle {
374+
PLAIN,
375+
ANSI_ESCAPE_CODES,
376+
HTML,
377+
}
354378
}

lib/src/main/java/jtamaro/graphic/GuiGraphicPropertiesPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void setGraphic(Graphic graphic) {
4747
} else {
4848
label.setText("<html><b>" + graphic.getInspectLabel() + "</b>"
4949
+ "<table>"
50-
+ graphic.getProps(false).entrySet().stream()
50+
+ graphic.getProps(Graphic.PropStyle.HTML).entrySet().stream()
5151
.map(e -> "<tr><td><b>"
5252
+ e.getKey()
5353
+ "</b></td><td>"

lib/src/main/java/jtamaro/graphic/GuiGraphicTreeCellRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Component getTreeCellRendererComponent(
3838
if (value instanceof Graphic.InspectTreeNode node) {
3939
final Graphic graphic = node.getGraphic();
4040
setText(graphic.getInspectLabel());
41-
final Map<String, String> props = graphic.getProps(false);
41+
final Map<String, String> props = graphic.getProps(Graphic.PropStyle.HTML);
4242
final String toolTipText = "<html><b>" + graphic.getInspectLabel() + "</b>"
4343
+ "<table>"
4444
+ props.entrySet().stream()

lib/src/main/java/jtamaro/graphic/Pin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ protected String getInspectLabel() {
107107
}
108108

109109
@Override
110-
protected SequencedMap<String, String> getProps(boolean plainText) {
111-
final SequencedMap<String, String> props = super.getProps(plainText);
110+
protected SequencedMap<String, String> getProps(PropStyle propStyle) {
111+
final SequencedMap<String, String> props = super.getProps(propStyle);
112112
props.put("pinPoint", Points.format(pinPoint));
113113
return props;
114114
}

lib/src/main/java/jtamaro/graphic/Rectangle.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,9 @@ protected String getInspectLabel() {
5656
}
5757

5858
@Override
59-
protected SequencedMap<String, String> getProps(boolean plainText) {
60-
final SequencedMap<String, String> props = super.getProps(plainText);
61-
props.put("color", plainText
62-
? color.toString()
63-
: Colors.htmlString(color));
59+
protected SequencedMap<String, String> getProps(PropStyle propStyle) {
60+
final SequencedMap<String, String> props = super.getProps(propStyle);
61+
props.put("color", Colors.format(propStyle, color));
6462
return props;
6563
}
6664

0 commit comments

Comments
 (0)