Skip to content

Commit 22fff7d

Browse files
committed
fix color encoding for 2013a schema
1 parent e70457f commit 22fff7d

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

org.pathvisio.lib/src/main/java/org/pathvisio/libgpml/model/GPML2013aWriter.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ protected void writeStates(List<DataNode> dataNodes, Element root) throws Conver
376376
setAttr("State.Graphics", "Height", gfx, Double.toString(state.getHeight()));
377377
// state does not have custom font properties in GPML2013a
378378
// z-order for state is not written to GPML2013a
379-
setAttr("State.Graphics", "FillColor", gfx, ColorUtils.colorToHex(state.getFillColor(), false));
379+
setAttr("State.Graphics", "FillColor", gfx, ColorUtils.colorToHex2013(state.getFillColor(), false));
380380
writeShapeStyleProperty(state, gfx);
381381
writeColor(state, gfx);
382382
// writes xref (write even if empty)
@@ -620,7 +620,6 @@ protected void writeShapes(List<Shape> shapes, Element root) throws ConverterExc
620620
writeShapedElement(shape, shp);
621621
writeGroupRef(shape.getGroupRef(), shp);
622622
Element gfx = shp.getChild("Graphics", shp.getNamespace());
623-
setAttr("Shape.Graphics", "FillColor", gfx, ColorUtils.colorToHex(shape.getFillColor(), false));
624623
setAttr("Shape.Graphics", "Rotation", gfx, Double.toString(shape.getRotation()));
625624
if (shp != null) {
626625
root.addContent(shp);
@@ -900,7 +899,7 @@ protected void writeShapedElement(ShapedElement shapedElement, Element se) throw
900899
writeRectProperty(shapedElement, gfx);
901900
// writes z-order and fill color (separately to preserve GPML2013 order)
902901
setAttr(base + ".Graphics", "ZOrder", gfx, String.valueOf(shapedElement.getZOrder()));
903-
setAttr(base + ".Graphics", "FillColor", gfx, ColorUtils.colorToHex(shapedElement.getFillColor(), false));
902+
setAttr(base + ".Graphics", "FillColor", gfx, ColorUtils.colorToHex2013(shapedElement.getFillColor(), false));
904903
// writes font properties
905904
writeFontProperty(shapedElement, gfx);
906905
// writes rest of shape style properties
@@ -1060,8 +1059,7 @@ protected void writeRectProperty(ShapedElement shapedElement, Element gfx) throw
10601059
*/
10611060
protected void writeColor(ShapedElement shapedElement, Element gfx) throws ConverterException {
10621061
String base = ((Element) gfx.getParent()).getName();
1063-
setAttr(base + ".Graphics", "Color", gfx, ColorUtils.colorToHex(shapedElement.getTextColor(), false));
1064-
1062+
setAttr(base + ".Graphics", "Color", gfx, ColorUtils.colorToHex2013(shapedElement.getTextColor(), false));
10651063
}
10661064

10671065
/**
@@ -1148,7 +1146,6 @@ protected void writeLineStyleProperty(LineElement lineElement, Element gfx) thro
11481146
setAttr(base + ".Graphics", "LineStyle", gfx, lineStyleStr);
11491147
}
11501148
setAttr(base + ".Graphics", "LineThickness", gfx, String.valueOf(lineElement.getLineWidth()));
1151-
setAttr(base + ".Graphics", "Color", gfx, ColorUtils.colorToHex(lineElement.getLineColor(), false));
1149+
setAttr(base + ".Graphics", "Color", gfx, ColorUtils.colorToHex2013(lineElement.getLineColor(), false));
11521150
}
1153-
11541151
}

org.pathvisio.lib/src/main/java/org/pathvisio/libgpml/util/ColorUtils.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ public static String colorToHex(Color color, boolean appendHash) {
5858
}
5959
}
6060
}
61+
62+
/**
63+
* Converts a {@link Color} object to a hexBinary string for 2013a.
64+
*
65+
* @param color the color object.
66+
* @param appendHash the boolean, if true appends "#" to beginning of hex
67+
* string.
68+
* @return the resulting hex string.
69+
*/
70+
public static String colorToHex2013(Color color, boolean appendHash) {
71+
int a = color.getAlpha();
72+
int r = color.getRed();
73+
int g = color.getGreen();
74+
int b = color.getBlue();
75+
76+
if(a == 0 && r == 0 && g == 0 && b == 0) {
77+
return "Transparent";
78+
} else {
79+
if (appendHash) {
80+
return String.format("#%02x%02x%02x", r, g, b);
81+
} else {
82+
return String.format("%02x%02x%02x", r, g, b);
83+
}
84+
}
85+
}
6186

6287
/**
6388
* Converts a hexBinary string to {@link Color} (r,g,b) or (r,g,b,a). If it

0 commit comments

Comments
 (0)