public boolean equals(final ColorScale cs) { if (min == null && cs.min != null || min != null && cs.min == null) { return false; } if ((min != null && cs.min != null) && Double.compare(min, cs.min) != 0) { return false; } if (max == null && cs.max != null || max != null && cs.max == null) { return false; } if ((max != null && cs.max != null) && Double.compare(max, cs.max) != 0) { return false; } if (!scaling.equals(cs.scaling)) { return false; } if (interpolate != cs.interpolate) { return false; } if (forceValuesIntoRange != cs.forceValuesIntoRange) { return false; } if (reliefShading != cs.reliefShading) { return false; } if (nullColor.length != cs.nullColor.length) { return false; } for (int i = 0; i < nullColor.length; i++) { if (nullColor[i] != cs.nullColor[i]) { return false; } } if (size() != cs.size()) { return false; } final Iterator<Double> iterator1 = cs.keySet().iterator(); for (final Double d1 : this.keySet()) { final Double d2 = iterator1.next(); if (d1.compareTo(d2) != 0) { return false; } final Color value1 = get(d1); final Color value2 = get(d2); if (!value1.equals(value2)) { return false; } } return true; }
// load color scale from json public void fromJSON(final String json) throws ColorScaleException { try { final ObjectMapper mapper = new ObjectMapper(); final Map<String, Object> colorScaleMap = mapper.readValue(json, Map.class); final String scalingStr = (String) colorScaleMap.get("Scaling"); if (scalingStr != null) { scaling = Scaling.valueOf(scalingStr); } final String reliefShadingStr = (String) colorScaleMap.get("ReliefShading"); reliefShading = ("1").equals(reliefShadingStr) || ("true").equalsIgnoreCase(reliefShadingStr); final String interpolateStr = (String) colorScaleMap.get("Interpolate"); interpolate = (interpolateStr == null) || interpolateStr.isEmpty() || ("1").equals(interpolateStr) || ("true").equalsIgnoreCase(interpolateStr); final String forceStr = (String) colorScaleMap.get("ForceValuesIntoRange"); forceValuesIntoRange = ("1").equals(forceStr) || ("true").equalsIgnoreCase(forceStr); final Map<String, String> nullColorMap = (Map<String, String>) colorScaleMap.get("NullColor"); final String nullColorStr = nullColorMap.get("color"); if (nullColorStr != null) { parseColor(nullColorStr, nullColorMap.get("opacity"), nullColor); } final ArrayList<Map<String, String>> colorsList = (ArrayList<Map<String, String>>) colorScaleMap.get("Colors"); if (colorsList != null) { for (final Map<String, String> color : colorsList) { final int[] colorArr = new int[4]; final String colorStr = color.get("color"); final String valueStr = color.get("value"); final Double value = Double.valueOf(valueStr); if (colorStr != null) { parseColor(colorStr, color.get("opacity"), colorArr); put(value, colorArr); } } } } catch (final Exception e) { throw new BadJSONException(e); } }
public void fromXML(final Document doc) throws ColorScaleException { try { clear(); final XPath xpath = XmlUtils.createXPath(); name = xpath.evaluate("/ColorMap/@name", doc); final Node nodeTitle = (Node) xpath.evaluate("/ColorMap/Title", doc, XPathConstants.NODE); if (nodeTitle != null) { title = xpath.evaluate("text()", nodeTitle); } final Node nodeDesc = (Node) xpath.evaluate("/ColorMap/Description", doc, XPathConstants.NODE); if (nodeDesc != null) { description = xpath.evaluate("text()", nodeDesc); } scaling = Scaling.valueOf(xpath.evaluate("/ColorMap/Scaling/text()", doc)); final String reliefShadingStr = xpath.evaluate("/ColorMap/ReliefShading/text()", doc).toLowerCase(); reliefShading = reliefShadingStr.equals("1") || reliefShadingStr.equals("true"); final String interpolateStr = xpath.evaluate("/ColorMap/Interpolate/text()", doc).toLowerCase(); interpolate = interpolateStr.isEmpty() || (interpolateStr.equals("1") || interpolateStr.equals("true")); final String forceStr = xpath.evaluate("/ColorMap/ForceValuesIntoRange/text()", doc).toLowerCase(); forceValuesIntoRange = (forceStr.equals("1") || forceStr.equals("true")); final Node nullColorNode = (Node) xpath.evaluate("/ColorMap/NullColor", doc, XPathConstants.NODE); if (nullColorNode != null) { final String colorStr = xpath.evaluate("@color", nullColorNode); final String opacityStr = xpath.evaluate("@opacity", nullColorNode); parseColor(colorStr, opacityStr, nullColor); } final int[] color = new int[4]; final XPathExpression expr = xpath.compile("/ColorMap/Color"); final NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { final Node node = nodes.item(i); final String valueStr = xpath.evaluate("@value", node); final String colorStr = xpath.evaluate("@color", node); final String opacityStr = xpath.evaluate("@opacity", node); if (valueStr.isEmpty()) { throw new IOException( "Error parsing XML: A value must be specified for a color element."); } final double value = Double.valueOf(valueStr); parseColor(colorStr, opacityStr, color); put(value, color); } cache = null; } catch (final Exception e) { throw new BadXMLException(e); } }