Commit 3a8d4e6f authored by Piotr Gawron's avatar Piotr Gawron
Browse files

color definitions are supported

parent 40e94d06
......@@ -56,7 +56,7 @@ public class ColorParser {
throw new InvalidArgumentException(
"Invalid color value: " + string + ". Correct format: #xxxxxx (where x is a hex value)");
}
if (string.charAt(0) != '#') {
string = "#" + string;
}
......@@ -86,10 +86,15 @@ public class ColorParser {
throw new InvalidArgumentException(
"Invalid color value: " + string + ". Correct format: #xxxxxx (where x is a hex value)");
} else {
return new Color(
Integer.valueOf(string.substring(COLOR_SUBSTRING_START_RED, COLOR_SUBSTRING_START_GREEN), HEX_BASE),
Integer.valueOf(string.substring(COLOR_SUBSTRING_START_GREEN, COLOR_SUBSTRING_START_BLUE), HEX_BASE),
Integer.valueOf(string.substring(COLOR_SUBSTRING_START_BLUE, COLOR_STRING_LENGTH_WITHOUT_ALPHA), HEX_BASE));
try {
return new Color(
Integer.valueOf(string.substring(COLOR_SUBSTRING_START_RED, COLOR_SUBSTRING_START_GREEN), HEX_BASE),
Integer.valueOf(string.substring(COLOR_SUBSTRING_START_GREEN, COLOR_SUBSTRING_START_BLUE), HEX_BASE),
Integer.valueOf(string.substring(COLOR_SUBSTRING_START_BLUE, COLOR_STRING_LENGTH_WITHOUT_ALPHA), HEX_BASE));
} catch (NumberFormatException e) {
throw new InvalidArgumentException(
"Invalid color value: " + string + ". Correct format: #xxxxxx (where x is a hex value)");
}
}
}
......@@ -112,12 +117,17 @@ public class ColorParser {
throw new InvalidArgumentException(
"Invalid color value: " + string + ". Correct format: #xxxxxxxx (where x is a hex value)");
} else {
return new Color(
Integer.valueOf(string.substring(COLOR_SUBSTRING_START_RED, COLOR_SUBSTRING_START_GREEN), HEX_BASE),
Integer.valueOf(string.substring(COLOR_SUBSTRING_START_GREEN, COLOR_SUBSTRING_START_BLUE), HEX_BASE),
Integer.valueOf(string.substring(COLOR_SUBSTRING_START_BLUE, COLOR_STRING_LENGTH_WITHOUT_ALPHA), HEX_BASE),
Integer.valueOf(string.substring(COLOR_STRING_LENGTH_WITHOUT_ALPHA, COLOR_STRING_LENGTH_WITH_ALPHA), HEX_BASE)
);
try {
return new Color(
Integer.valueOf(string.substring(COLOR_SUBSTRING_START_RED, COLOR_SUBSTRING_START_GREEN), HEX_BASE),
Integer.valueOf(string.substring(COLOR_SUBSTRING_START_GREEN, COLOR_SUBSTRING_START_BLUE), HEX_BASE),
Integer.valueOf(string.substring(COLOR_SUBSTRING_START_BLUE, COLOR_STRING_LENGTH_WITHOUT_ALPHA), HEX_BASE),
Integer.valueOf(string.substring(COLOR_STRING_LENGTH_WITHOUT_ALPHA, COLOR_STRING_LENGTH_WITH_ALPHA),
HEX_BASE));
} catch (NumberFormatException e) {
throw new InvalidArgumentException(
"Invalid color value: " + string + ". Correct format: #xxxxxxxx (where x is a hex value)");
}
}
}
......
......@@ -724,13 +724,13 @@ public class SbgnmlXmlParser {
Style style = RenderUtil.getStyle(renderInformation, glyph);
if (style != null) {
try {
element.setFillColor(colorParser.parse(style.getG().getFill()));
element.setFillColor(extractColor(style.getG().getFill()));
} catch (InvalidArgumentException e) {
logger.warn(new LogMarker(ProjectLogEntryType.PARSING_ISSUE, element),
"Invalid fill color: " + style.getG().getFill());
}
try {
element.setBorderColor(colorParser.parse(style.getG().getStroke()));
element.setBorderColor(extractColor(style.getG().getStroke()));
} catch (InvalidArgumentException e) {
logger.warn(new LogMarker(ProjectLogEntryType.PARSING_ISSUE, element),
"Invalid border color: " + style.getG().getStroke());
......@@ -746,6 +746,29 @@ public class SbgnmlXmlParser {
}
}
private Color extractColor(String fill) {
Color color = getRenderColor(fill);
if (color == null) {
color = colorParser.parse(fill);
}
return color;
}
private Color getRenderColor(String color) {
ColorDefinition colorDefinition = RenderUtil.getColorDefinition(renderInformation, color);
if (colorDefinition != null) {
return colorParser.parse(colorDefinition.getValue());
}
LinearGradient gradient = RenderUtil.getGradient(renderInformation, color);
if (gradient != null) {
if (gradient.getStop().size() > 0) {
return colorParser.parse(gradient.getStop().get(0).getStopColor());
}
}
return null;
}
private void assignRenderInformation(PolylineData line, Arc glyph) {
if (renderInformation != null) {
Style style = RenderUtil.getStyle(renderInformation, glyph);
......@@ -756,10 +779,9 @@ public class SbgnmlXmlParser {
private void assignRenderInformation(PolylineData line, Style style) {
if (style != null) {
try {
Color color = colorParser.parse(style.getG().getStroke());
line.setColor(color);
line.setColor(extractColor(style.getG().getStroke()));
} catch (InvalidArgumentException e) {
logger.warn("Invalid border color: " + style.getG().getStroke());
logger.warn("Invalid line color: " + style.getG().getStroke());
}
if (style.getG().getStrokeWidth() != null) {
double lineWidth = (double) style.getG().getStrokeWidth();
......
......@@ -15,3 +15,4 @@ import org.junit.runners.Suite.SuiteClasses;
public class AllSbgnmlTests {
}
......@@ -2,6 +2,7 @@ package lcsb.mapviewer.converter.model.sbgnml;
import static org.junit.Assert.*;
import java.awt.Color;
import java.awt.geom.Point2D;
import org.apache.logging.log4j.LogManager;
......@@ -115,6 +116,16 @@ public class SbgnmlXmlParserTest2 extends SbgnmlTestFunctions {
assertEquals(0, protein.getModificationResidues().size());
}
@Test
public void testParseColors() throws Exception {
Converter converter = new SbgnmlXmlConverter();
Model model = converter
.createModel(new ConverterParams()
.filename("testFiles/sbgnmlCellDesignerInompatible/neuronal_muscle_signalling_color.sbgn"));
assertFalse(model.getElementByElementId("glyph0").getFillColor().equals(Color.WHITE));
assertTrue(super.getWarnings().size() <= 1);
}
@Test
public void testProblematicProduct() throws Exception {
Converter converter = new SbgnmlXmlConverter();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment