/** Encodes a colorizer into a style Rule. */ public static Rule encode(Colorizer c, Rule rule) { rule.put("raster-colorizer-default-mode", c.mode); rule.put("raster-colorizer-default-color", c.color); Mixed mixed = new Mixed(); for (Colorizer.Stop stop : c.stops()) { Function f = new Function("stop") { @Override public Object evaluate(Object obj) { return null; } }; f.args().add(new Literal(stop.value)); f.args().add(new Literal(stop.color)); f.args().add(new Literal(stop.mode)); f.args().add(new Literal(stop.epsilon)); mixed.expressions().add(f); } rule.put("raster-colorizer-stops", mixed); return rule; }
/** Decodes a colorizer from a style Rule. */ public static Colorizer decode(Rule rule) { Colorizer.Builder cb = Colorizer.build(); cb.mode( Colorizer.Mode.valueOf( rule.string(null, "raster-colorizer-default-mode", "linear").toUpperCase(Locale.ROOT))); cb.color(rule.color(null, "raster-colorizer-default-color", RGB.black)); if (rule.has("raster-colorizer-stops")) { // TODO: make this routine more robust Mixed m = (Mixed) rule.get("raster-colorizer-stops"); for (Expression expr : m.expressions()) { Function stop = (Function) expr; List<Expression> args = stop.args(); Double value = Convert.toNumber(args.get(0).evaluate(null)).get().doubleValue(); RGB color = RGB.convert(args.get(1).evaluate(null)).get(); if (args.size() > 2) { Mode mode = Mode.valueOf(args.get(2).evaluate(null).toString().toUpperCase(Locale.ROOT)); if (args.size() > 3) { double epsilon = Convert.toNumber(args.get(3).evaluate(null), Double.class).get(); if (epsilon != 0d) { cb.stop(value, color, epsilon); } else { cb.stop(value, color, mode); } } else { cb.stop(value, color, mode); } } else { cb.stop(value, color); } } } return cb.colorizer(); }