示例#1
0
  // 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);
    }
  }
示例#2
0
 /**
  * Method is responsible for generating a color scale from hard coded values. It should be called
  * when there is no input default color scale.
  */
 public void setDefaultValues() {
   clear();
   setScaling(ColorScale.Scaling.MinMax);
   put(0.0, new Color(0, 0, 0, 0));
   put(1e-12, new Color(255, 255, 255, 128));
   put(0.1, new Color(247, 252, 253));
   put(0.2, new Color(229, 245, 249));
   put(0.3, new Color(204, 236, 230));
   put(0.4, new Color(153, 216, 201));
   put(0.5, new Color(102, 194, 164));
   put(0.6, new Color(65, 174, 118));
   put(0.7, new Color(35, 139, 69));
   put(0.8, new Color(0, 109, 44));
   put(0.9, new Color(0, 68, 27));
   put(1.0, new Color(0, 0, 0));
   setInterpolate(true);
   setForceValuesIntoRange(false);
 }
示例#3
0
 /**
  * Method is responsible for generating a color scale from hard coded values. It should be called
  * when there is no input default color scale.
  */
 public void setDefaultGrayScaleValues() {
   clear();
   setScaling(ColorScale.Scaling.MinMax);
   put(0.0, new Color(0, 0, 0));
   put(0.1, new Color(26, 26, 26));
   put(0.2, new Color(52, 52, 52));
   put(0.3, new Color(77, 77, 77));
   put(0.4, new Color(102, 102, 102));
   put(0.5, new Color(128, 128, 128));
   put(0.6, new Color(154, 154, 154));
   put(0.7, new Color(179, 179, 179));
   put(0.8, new Color(205, 205, 205));
   put(0.9, new Color(230, 230, 230));
   put(1.0, new Color(255, 255, 255));
   setInterpolate(true);
   setForceValuesIntoRange(false);
 }
示例#4
0
 public void put(final double key, final int[] c) {
   put(new Double(key), new Color(c[0], c[1], c[2], c[3]));
   cache = null;
 }
示例#5
0
 public void put(final double key, final int r, final int g, final int b, final int a) {
   put(new Double(key), new Color(r, g, b, a));
   cache = null;
 }
示例#6
0
 public void put(final double key, final Color c) {
   put(new Double(key), c);
   cache = null;
 }
示例#7
0
  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);
    }
  }