Example usage for java.awt Color getRGB

List of usage examples for java.awt Color getRGB

Introduction

In this page you can find the example usage for java.awt Color getRGB.

Prototype

public int getRGB() 

Source Link

Document

Returns the RGB value representing the color in the default sRGB ColorModel .

Usage

From source file:edu.umn.cs.spatialHadoop.visualization.FrequencyMap.java

public BufferedImage asImage() {
    if (min >= max) {
        // Values not set. Autodetect
        min = Float.MAX_VALUE;/*  w ww  .  j a va  2 s.  c o  m*/
        max = -Float.MAX_VALUE;
        for (int x = 0; x < this.getWidth(); x++) {
            for (int y = 0; y < this.getHeight(); y++) {
                if (frequencies[x][y] < min)
                    min = frequencies[x][y];
                if (frequencies[x][y] > max)
                    max = frequencies[x][y];
            }
        }
    }
    BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    for (int x = 0; x < this.getWidth(); x++) {
        for (int y = 0; y < this.getHeight(); y++) {
            Color color = calculateColor(frequencies[x][y], min, max);
            image.setRGB(x, y, color.getRGB());
        }
    }
    return image;
}

From source file:org.gitools.analysis.clustering.hierarchical.HierarchicalClusterer.java

public HierarchicalCluster cluster(IMatrix matrix, IMatrixLayer<Double> layer,
        IMatrixDimension clusterDimension, IMatrixDimension aggregationDimension, IProgressMonitor monitor) {

    Map<String, HierarchicalCluster> clusters = new HashMap<>(clusterDimension.size());
    SortedSet<ClusterPair> linkages = new ConcurrentSkipListSet<>();

    // Aggregate all the values to sort the clusters by weight
    monitor.begin("Aggregating values...", clusterDimension.size());
    final Map<String, Double> aggregation = new HashMap<>(clusterDimension.size());
    Set<String> allNullValues = new HashSet<>();
    IMatrixPosition position = matrix.newPosition();
    for (String id : position.iterate(clusterDimension)) {

        Double value = aggregator.aggregate(position.iterate(layer, aggregationDimension));

        if (value != null) {
            aggregation.put(id, value);/*from ww  w. j a  va 2s  .co m*/
        } else {
            allNullValues.add(id);
        }

    }

    // First sort the clustering dimension to show the clusters ordered by weight at the end
    if (clusterDimension instanceof IMatrixViewDimension) {
        IMatrixViewDimension sortDimension = (IMatrixViewDimension) clusterDimension;
        sortDimension.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return SortDirection.ASCENDING.compare(aggregation.get(o1), aggregation.get(o2));
            }
        });
    }

    // Calculate all the distances
    IMatrixPosition position1 = matrix.newPosition();
    IMatrixPosition position2 = matrix.newPosition();
    monitor.begin("Calculating distances...", clusterDimension.size());
    for (String id1 : position1.iterate(clusterDimension)) {

        // Check user cancel action
        monitor.worked(1);
        if (monitor.isCancelled()) {
            throw new CancellationException();
        }

        // Skip all null values
        if (allNullValues.contains(id1)) {
            continue;
        }

        HierarchicalCluster cluster1 = newCluster(clusters, id1);
        cluster1.setWeight(aggregation.get(id1));

        for (String id2 : position2.iterate(clusterDimension.from(id1))) {

            // Skip equal ids
            if (id1.equals(id2))
                continue;

            // Skip all null columns
            if (allNullValues.contains(id2)) {
                continue;
            }

            Double distance = measure.compute(position1.iterate(layer, aggregationDimension),
                    position2.iterate(layer, aggregationDimension));

            HierarchicalCluster cluster2 = newCluster(clusters, id2);

            linkages.add(new ClusterPair(distance, cluster1, cluster2));
        }
    }

    // Create the clusters agglomerating nodes by the nearest distances
    HierarchyBuilder builder = new HierarchyBuilder(newHashSet(clusters.values()), linkages);
    builder.agglomerate(linkageStrategy, monitor, clusterDimension.size());

    // Set cluster names ordered by weight
    HierarchicalCluster root = builder.getRootCluster();
    root.setName("");
    Color color = nameClusters(root.getChildren(), 1);
    root.setColor(color.getRGB());
    root.setName("root");

    return root;
}

From source file:br.prof.salesfilho.oci.image.ImageProcessor.java

public double[] getRed() {
    int[] colors = new int[this.image.getWidth() * this.image.getHeight()];
    this.image.getRGB(0, 0, this.image.getWidth(), this.image.getHeight(), colors, 0, this.image.getWidth());

    double[] red = new double[colors.length];

    for (int i = 0; i < colors.length; i++) {
        Color color = new Color(colors[i]);
        int rgb = color.getRGB();
        int r = (rgb >> 16) & 0xFF;
        red[i] = (double) r;
    }/*  w ww  .ja va 2s  .c  om*/
    return red;
}

From source file:br.prof.salesfilho.oci.image.ImageProcessor.java

public double[] getGreen() {
    int[] colors = new int[this.image.getWidth() * this.image.getHeight()];
    this.image.getRGB(0, 0, this.image.getWidth(), this.image.getHeight(), colors, 0, this.image.getWidth());

    double[] green = new double[colors.length];

    for (int i = 0; i < colors.length; i++) {
        Color color = new Color(colors[i]);
        int rgb = color.getRGB();
        int g = (rgb >> 8) & 0xFF;
        green[i] = (double) g;
    }/*w  w w.  j  av  a 2  s  .  c o  m*/
    return green;
}

From source file:br.prof.salesfilho.oci.image.ImageProcessor.java

public double[] getBlue() {
    int[] colors = new int[this.image.getWidth() * this.image.getHeight()];
    this.image.getRGB(0, 0, this.image.getWidth(), this.image.getHeight(), colors, 0, this.image.getWidth());

    double[] blue = new double[colors.length];

    for (int i = 0; i < colors.length; i++) {
        Color color = new Color(colors[i]);
        int rgb = color.getRGB();
        int b = (rgb & 0xFF);
        blue[i] = (double) b;
    }/* w  ww .  ja v a2s  .  co  m*/
    return blue;
}

From source file:br.prof.salesfilho.oci.image.ImageProcessor.java

public double[] getGrayScale() {
    int[] colors = new int[this.image.getWidth() * this.image.getHeight()];
    this.image.getRGB(0, 0, this.image.getWidth(), this.image.getHeight(), colors, 0, this.image.getWidth());

    double[] grayScale = new double[colors.length];

    for (int i = 0; i < colors.length; i++) {
        Color color = new Color(colors[i]);

        int rgb = color.getRGB();
        int r = (rgb >> 16) & 0xFF;
        int g = (rgb >> 8) & 0xFF;
        int b = (rgb & 0xFF);

        //int grayLevel = (r + g + b) / 3;
        int grayLevel = (int) ((r * 0.2126d) + (g * 0.7152d) + (b * 0.0722)); //Recomended by OpenCV

        int gray = (grayLevel << 16) + (grayLevel << 8) + grayLevel;

        grayScale[i] = (double) gray;
    }// w w  w . j av  a 2  s . c  o m
    return grayScale;
}

From source file:edu.umn.cs.spatialHadoop.operations.GeometricPlot.java

private static <S extends Shape> void plotLocal(Path inFile, Path outFile, OperationsParams params)
        throws IOException {
    int width = params.getInt("width", 1000);
    int height = params.getInt("height", 1000);

    Color strokeColor = params.getColor("color", Color.BLACK);
    int color = strokeColor.getRGB();

    String hdfDataset = (String) params.get("dataset");
    Shape shape = hdfDataset != null ? new NASARectangle() : (Shape) params.getShape("shape", null);
    Shape plotRange = params.getShape("rect", null);

    boolean keepAspectRatio = params.is("keep-ratio", true);

    String valueRangeStr = (String) params.get("valuerange");
    MinMax valueRange;/*w w  w  . j a  va 2s.  com*/
    if (valueRangeStr == null) {
        valueRange = null;
    } else {
        String[] parts = valueRangeStr.split(",");
        valueRange = new MinMax(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
    }

    InputSplit[] splits;
    FileSystem inFs = inFile.getFileSystem(params);
    FileStatus inFStatus = inFs.getFileStatus(inFile);
    if (inFStatus != null && !inFStatus.isDir()) {
        // One file, retrieve it immediately.
        // This is useful if the input is a hidden file which is automatically
        // skipped by FileInputFormat. We need to plot a hidden file for the case
        // of plotting partition boundaries of a spatial index
        splits = new InputSplit[] { new FileSplit(inFile, 0, inFStatus.getLen(), new String[0]) };
    } else {
        JobConf job = new JobConf(params);
        ShapeInputFormat<Shape> inputFormat = new ShapeInputFormat<Shape>();
        ShapeInputFormat.addInputPath(job, inFile);
        splits = inputFormat.getSplits(job, 1);
    }

    boolean vflip = params.is("vflip");

    Rectangle fileMbr;
    if (plotRange != null) {
        fileMbr = plotRange.getMBR();
    } else if (hdfDataset != null) {
        // Plotting a NASA file
        fileMbr = new Rectangle(-180, -90, 180, 90);
    } else {
        fileMbr = FileMBR.fileMBR(inFile, params);
    }

    if (keepAspectRatio) {
        // Adjust width and height to maintain aspect ratio
        if (fileMbr.getWidth() / fileMbr.getHeight() > (double) width / height) {
            // Fix width and change height
            height = (int) (fileMbr.getHeight() * width / fileMbr.getWidth());
        } else {
            width = (int) (fileMbr.getWidth() * height / fileMbr.getHeight());
        }
    }

    boolean adaptiveSample = shape instanceof Point && params.getBoolean("sample", false);
    float adaptiveSampleRatio = 0.0f;
    if (adaptiveSample) {
        // Calculate the sample ratio
        long recordCount = FileMBR.fileMBR(inFile, params).recordCount;
        adaptiveSampleRatio = params.getFloat(AdaptiveSampleFactor, 1.0f) * width * height / recordCount;
    }

    boolean gradualFade = !(shape instanceof Point) && params.getBoolean("fade", false);

    if (hdfDataset != null) {
        // Collects some stats about the HDF file
        if (valueRange == null)
            valueRange = Aggregate.aggregate(new Path[] { inFile }, params);
        NASAPoint.minValue = valueRange.minValue;
        NASAPoint.maxValue = valueRange.maxValue;
        NASAPoint.setColor1(params.getColor("color1", Color.BLUE));
        NASAPoint.setColor2(params.getColor("color2", Color.RED));
        NASAPoint.gradientType = params.getGradientType("gradient", NASAPoint.GradientType.GT_HUE);
    }

    double scale2 = (double) width * height / (fileMbr.getWidth() * fileMbr.getHeight());
    double scale = Math.sqrt(scale2);

    // Create an image
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = image.createGraphics();
    Color bg_color = params.getColor("bgcolor", new Color(0, 0, 0, 0));
    graphics.setBackground(bg_color);
    graphics.clearRect(0, 0, width, height);
    graphics.setColor(strokeColor);

    for (InputSplit split : splits) {
        if (hdfDataset != null) {
            // Read points from the HDF file
            RecordReader<NASADataset, NASAShape> reader = new HDFRecordReader(params, (FileSplit) split,
                    hdfDataset, true);
            NASADataset dataset = reader.createKey();

            while (reader.next(dataset, (NASAShape) shape)) {
                // Skip with a fixed ratio if adaptive sample is set
                if (adaptiveSample && Math.random() > adaptiveSampleRatio)
                    continue;
                if (plotRange == null || shape.isIntersected(shape)) {
                    shape.draw(graphics, fileMbr, width, height, 0.0);
                }
            }
            reader.close();
        } else {
            RecordReader<Rectangle, Shape> reader = new ShapeRecordReader<Shape>(params, (FileSplit) split);
            Rectangle cell = reader.createKey();
            while (reader.next(cell, shape)) {
                // Skip with a fixed ratio if adaptive sample is set
                if (adaptiveSample && Math.random() > adaptiveSampleRatio)
                    continue;
                Rectangle shapeMBR = shape.getMBR();
                if (shapeMBR != null) {
                    if (plotRange == null || shapeMBR.isIntersected(plotRange)) {
                        if (gradualFade) {
                            double sizeInPixels = (shapeMBR.getWidth() + shapeMBR.getHeight()) * scale;
                            if (sizeInPixels < 1.0 && Math.round(sizeInPixels * 255) < 1.0) {
                                // This shape can be safely skipped as it is too small to be plotted
                                continue;
                            } else {
                                int alpha = (int) Math.round(sizeInPixels * 255);
                                graphics.setColor(new Color((alpha << 24) | color, true));
                            }
                        }
                        shape.draw(graphics, fileMbr, width, height, scale2);
                    }
                }
            }
            reader.close();
        }
    }
    // Write image to output
    graphics.dispose();
    if (vflip) {
        AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
        tx.translate(0, -image.getHeight());
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
        image = op.filter(image, null);
    }
    FileSystem outFs = outFile.getFileSystem(params);
    OutputStream out = outFs.create(outFile, true);
    ImageIO.write(image, "png", out);
    out.close();

}

From source file:GifEncoder.java

public void addTransparentColor(Color color) {
    try {/*from  w w  w.java  2s .c  o  m*/
        if (f < 256) {
            c[f++] = color.getRGB();
        }
        return;
    } catch (Exception e) {
    }
}

From source file:verdandi.VerdandiConfiguration.java

public void setWorkdayEditorColor(Color c) {
    prefs.putInt(KEY_WORKDAY_COLOR1, c.getRGB());
    setChanged();
    notifyObservers();
}

From source file:verdandi.VerdandiConfiguration.java

public void setWorkdayEditorAlternativeColor(Color c) {
    prefs.putInt(KEY_WORKDAY_COLOR2, c.getRGB());
    setChanged();
    notifyObservers();
}