Exemple #1
0
 /* Color.toString() is not great so we need a way to convert a
  * Color to some easily readable format, since we only
  * support black, white, blue, green, red, & yellow we can
  * easily check these by looking at the RGB values of the
  * color found by calling Color.getRGB().
  */
 public String colorToString(Color color) {
   if (color.getRGB() == Color.BLACK.getRGB()) {
     return "Black";
   } else if (color.getRGB() == Color.BLUE.getRGB()) {
     return "Blue";
   } else if (color.getRGB() == Color.GREEN.getRGB()) {
     return "Green";
   } else if (color.getRGB() == Color.RED.getRGB()) {
     return "Red";
   } else if (color.getRGB() == Color.YELLOW.getRGB()) {
     return "Yellow";
   } else {
     return "No Color Information";
   }
 }
  @Override
  public void glyph(
      final Graphics2D g,
      final Rectangle2D rect,
      final CachedDimRangeSymbolizer symbol,
      final MapLayer layer) {

    int[] ARGB = new int[] {Color.RED.getRGB(), Color.GREEN.getRGB(), Color.BLUE.getRGB()};

    if (layer instanceof CoverageMapLayer) {
      final CoverageMapLayer cml = (CoverageMapLayer) layer;
      final CoverageReference ref = cml.getCoverageReference();
      try {
        final GridCoverageReader reader = ref.acquireReader();
        final GridCoverageReadParam param = new GridCoverageReadParam();
        param.setResolution(1, 1);
        GridCoverage2D cov = (GridCoverage2D) reader.read(0, param);
        ref.recycle(reader);
        cov = cov.view(ViewType.NATIVE);
        RenderedImage img = cov.getRenderedImage();
        ColorModel cm = img.getColorModel();

        if (cm instanceof IndexColorModel) {
          final IndexColorModel icm = (IndexColorModel) cm;

          final GridSampleDimension sampleDim = cov.getSampleDimension(0);

          int size = icm.getMapSize();
          ARGB = new int[size];
          icm.getRGBs(ARGB);
          final double minVal = sampleDim.getMinimumValue();
          final double maxVal = sampleDim.getMaximumValue();

          final ColorMap colorMap = new ColorMap();
          colorMap.setGeophysicsRange(
              ColorMap.ANY_QUANTITATIVE_CATEGORY,
              new MeasurementRange(
                  NumberRange.create(minVal, true, maxVal, true), sampleDim.getUnits()));

          GridSampleDimension ret = colorMap.recolor(sampleDim, ARGB);
        }

      } catch (CoverageStoreException | CancellationException ex) {
        Logging.getLogger("org.geotoolkit.display2d.ext.dimrange").log(Level.WARNING, null, ex);
      }
    }

    final float[] space = new float[ARGB.length];
    final Color[] colors = new Color[ARGB.length];
    for (int i = 0; i < space.length; i++) {
      space[i] = (float) i / (space.length - 1);
      colors[i] = new Color(ARGB[i]);
    }

    final LinearGradientPaint paint =
        new LinearGradientPaint(
            (float) rect.getMinX(),
            (float) rect.getMinY(),
            (float) rect.getMaxX(),
            (float) rect.getMinY(),
            space,
            colors);

    g.setPaint(paint);
    g.fill(rect);
  }