Example usage for java.awt Color getAlpha

List of usage examples for java.awt Color getAlpha

Introduction

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

Prototype

public int getAlpha() 

Source Link

Document

Returns the alpha component in the range 0-255.

Usage

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/**
 * Returns <code>true</code> if the passed colors are the same, 
 * <code>false</code> otherwise.
 * //from w  w  w  .  ja v a2s .c o m
 * @param c1 One of the colors to check.
 * @param c2 One of the colors to check.
 * @param alpha Pass <code>true</code> to take into account the 
 *             alpha component, <code>false</code> otherwise.
 * @return See above.
 */
public static boolean isSameColors(Color c1, Color c2, boolean alpha) {
    if (c1 == null || c2 == null)
        return false;
    if (c1.getRed() != c2.getRed())
        return false;
    if (c1.getGreen() != c2.getGreen())
        return false;
    if (c1.getBlue() != c2.getBlue())
        return false;
    if (alpha) {
        if (c1.getAlpha() != c2.getAlpha())
            return false;
    }
    return true;
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

/**
 * Method contributed by Alexej Suchov//  w ww.  jav  a 2 s. c o  m
 *
 * @see Graphics2D#setComposite(Composite)
 */
@Override
public void setComposite(final Composite comp) {

    if (comp instanceof AlphaComposite) {

        final AlphaComposite composite = (AlphaComposite) comp;

        if (composite.getRule() == 3) {

            alpha = composite.getAlpha();
            this.composite = composite;

            if (realPaint != null && (realPaint instanceof Color)) {

                final Color c = (Color) realPaint;
                paint = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (c.getAlpha() * alpha));
            }
            return;
        }
    }

    this.composite = comp;
    alpha = 1.0F;

}

From source file:org.eclipse.birt.chart.device.svg.SVGGraphics2D.java

protected String alphaToString(Color color) {
    double a = 1;
    if (color.getAlpha() < 0xFF) {
        a = color.getAlpha() / 255.0;// w  ww  .  jav  a 2  s . c o m
    }
    return toString(a);
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

private void setPaint(final boolean invert, final double xoffset, final double yoffset, final boolean fill) {
    if (paint instanceof Color) {
        final Color color = (Color) paint;
        final int alpha = color.getAlpha();
        if (fill) {
            if (alpha != currentFillGState) {
                currentFillGState = alpha;
                PdfGState gs = fillGState[alpha];
                if (gs == null) {
                    gs = new PdfGState();
                    gs.setFillOpacity(alpha / 255.00f);
                    fillGState[alpha] = gs;
                }//ww  w  . jav  a2  s  .  c om
                cb.setGState(gs);
            }
            cb.setColorFill(color);
        } else {
            if (alpha != currentStrokeGState) {
                currentStrokeGState = alpha;
                PdfGState gs = strokeGState[alpha];
                if (gs == null) {
                    gs = new PdfGState();
                    gs.setStrokeOpacity(alpha / 255.0f);
                    strokeGState[alpha] = gs;
                }
                cb.setGState(gs);
            }
            cb.setColorStroke(color);
        }
    } else if (paint instanceof GradientPaint) {
        final GradientPaint gp = (GradientPaint) paint;
        final Point2D p1 = gp.getPoint1();
        transform.transform(p1, p1);
        final Point2D p2 = gp.getPoint2();
        transform.transform(p2, p2);
        final Color c1 = gp.getColor1();
        final Color c2 = gp.getColor2();
        final PdfShading shading = PdfShading.simpleAxial(cb.getPdfWriter(), (float) p1.getX(),
                normalizeY((float) p1.getY()), (float) p2.getX(), normalizeY((float) p2.getY()), c1, c2);
        final PdfShadingPattern pat = new PdfShadingPattern(shading);
        if (fill) {
            cb.setShadingFill(pat);
        } else {
            cb.setShadingStroke(pat);
        }
    } else if (paint instanceof TexturePaint) {
        try {
            final TexturePaint tp = (TexturePaint) paint;
            final BufferedImage img = tp.getImage();
            final Rectangle2D rect = tp.getAnchorRect();
            final com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null);
            final PdfPatternPainter pattern = cb.createPattern(image.getWidth(), image.getHeight());
            final AffineTransform inverse = this.normalizeMatrix();
            inverse.translate(rect.getX(), rect.getY());
            inverse.scale(rect.getWidth() / image.getWidth(), -rect.getHeight() / image.getHeight());
            final double[] mx = new double[6];
            inverse.getMatrix(mx);
            pattern.setPatternMatrix((float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4],
                    (float) mx[5]);
            image.setAbsolutePosition(0, 0);
            pattern.addImage(image);
            if (fill) {
                cb.setPatternFill(pattern);
            } else {
                cb.setPatternStroke(pattern);
            }

        } catch (Exception ex) {
            if (fill) {
                cb.setColorFill(Color.gray);
            } else {
                cb.setColorStroke(Color.gray);
            }
        }
    } else {
        try {
            int type = BufferedImage.TYPE_4BYTE_ABGR;
            if (paint.getTransparency() == Transparency.OPAQUE) {
                type = BufferedImage.TYPE_3BYTE_BGR;
            }
            final BufferedImage img = new BufferedImage((int) width, (int) height, type);
            final Graphics2D g = (Graphics2D) img.getGraphics();
            g.transform(transform);
            final AffineTransform inv = transform.createInverse();
            Shape fillRect = new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight());
            fillRect = inv.createTransformedShape(fillRect);
            g.setPaint(paint);
            g.fill(fillRect);
            if (invert) {
                final AffineTransform tx = new AffineTransform();
                tx.scale(1, -1);
                tx.translate(-xoffset, -yoffset);
                g.drawImage(img, tx, null);
            }
            g.dispose();
            // g = null;
            final com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null);
            final PdfPatternPainter pattern = cb.createPattern(width, height);
            image.setAbsolutePosition(0, 0);
            pattern.addImage(image);
            if (fill) {
                cb.setPatternFill(pattern);
            } else {
                cb.setPatternStroke(pattern);
            }
        } catch (Exception ex) {
            if (fill) {
                cb.setColorFill(Color.gray);
            } else {
                cb.setColorStroke(Color.gray);
            }
        }
    }
}

From source file:SWTGraphics2D.java

/**
 * Sets the current color for this graphics context.
 *
 * @param color  the color.//from w  w w.ja v a2s. c o m
 *
 * @see #getColor()
 */
public void setColor(Color color) {
    org.eclipse.swt.graphics.Color swtColor = getSwtColorFromPool(color);
    this.gc.setForeground(swtColor);
    // handle transparency and compositing.
    if (this.composite instanceof AlphaComposite) {
        AlphaComposite acomp = (AlphaComposite) this.composite;
        switch (acomp.getRule()) {
        case AlphaComposite.SRC_OVER:
            this.gc.setAlpha((int) (color.getAlpha() * acomp.getAlpha()));
            break;
        default:
            this.gc.setAlpha(color.getAlpha());
            break;
        }
    }
}

From source file:umontreal.iro.lecuyer.charts.HistogramSeriesCollection.java

public String toLatex(double XScale, double YScale, double XShift, double YShift, double xmin, double xmax,
        double ymin, double ymax) {

    // Calcule les bornes reelles du graphique, en prenant en compte la position des axes
    xmin = Math.min(XShift, xmin);
    xmax = Math.max(XShift, xmax);
    ymin = Math.min(YShift, ymin);
    ymax = Math.max(YShift, ymax);

    CustomHistogramDataset tempSeriesCollection = (CustomHistogramDataset) seriesCollection;
    Formatter formatter = new Formatter(Locale.US);
    double var;
    double margin = ((XYBarRenderer) renderer).getMargin();

    for (int i = tempSeriesCollection.getSeriesCount() - 1; i >= 0; i--) {
        List temp = tempSeriesCollection.getBins(i);
        ListIterator iter = temp.listIterator();

        Color color = (Color) renderer.getSeriesPaint(i);
        String colorString = detectXColorClassic(color);
        if (colorString == null) {
            colorString = "color" + i;
            formatter.format("\\definecolor{%s}{rgb}{%.2f, %.2f, %.2f}%n", colorString, color.getRed() / 255.0,
                    color.getGreen() / 255.0, color.getBlue() / 255.0);
        }/*from   w ww.ja  v  a  2  s.c  o  m*/

        HistogramBin currentBin = null;
        while (iter.hasNext()) {
            double currentMargin;
            currentBin = (HistogramBin) iter.next();
            currentMargin = ((margin * (currentBin.getEndBoundary() - currentBin.getStartBoundary()))) * XScale;
            if ((currentBin.getStartBoundary() >= xmin && currentBin.getStartBoundary() <= xmax)
                    && (currentBin.getCount() >= ymin && currentBin.getCount() <= ymax)) {
                var = Math.min(currentBin.getEndBoundary(), xmax);
                if (filled[i]) {
                    formatter.format(
                            "\\filldraw [line width=%.2fpt, opacity=%.2f, color=%s] ([xshift=%.4f] %.4f, %.4f) rectangle ([xshift=-%.4f] %.4f, %.4f); %%%n",
                            lineWidth[i], (color.getAlpha() / 255.0), colorString, currentMargin,
                            (currentBin.getStartBoundary() - XShift) * XScale, 0.0, currentMargin,
                            (var - XShift) * XScale, (currentBin.getCount() - YShift) * YScale);
                } else {
                    formatter.format(
                            "\\draw [line width=%.2fpt, color=%s] ([xshift=%.4f] %.4f, %.4f) rectangle ([xshift=-%.4f] %.4f, %.4f); %%%n",
                            lineWidth[i], colorString, currentMargin,
                            (currentBin.getStartBoundary() - XShift) * XScale, 0.0, currentMargin,
                            (var - XShift) * XScale, (currentBin.getCount() - YShift) * YScale);
                }
            } else if ((currentBin.getStartBoundary() >= xmin && currentBin.getStartBoundary() <= xmax)
                    && (currentBin.getCount() >= ymin && currentBin.getCount() > ymax)) { // Cas ou notre rectangle ne peut pas etre affiche en entier (trop haut)
                var = Math.min(currentBin.getEndBoundary(), xmax);
                if (filled[i]) {
                    formatter.format(
                            "\\filldraw [line width=%.2fpt,  opacity=%.2f, color=%s] ([xshift=%.4f] %.4f, %.4f) rectangle ([xshift=-%.4f] %.4f, %.4f); %%%n",
                            lineWidth[i], (color.getAlpha() / 255.0), colorString, currentMargin,
                            (currentBin.getStartBoundary() - XShift) * XScale, 0.0, currentMargin,
                            (var - XShift) * XScale, (ymax - YShift) * YScale);
                    formatter.format(
                            "\\draw [line width=%.2fpt, color=%s, style=dotted] ([xshift=%.4f] %.4f, %.4f) rectangle ([yshift=3mm, xshift=-%.4f] %.4f, %.4f); %%%n",
                            lineWidth[i], colorString, currentMargin,
                            (currentBin.getStartBoundary() - XShift) * XScale, (ymax - YShift) * YScale,
                            currentMargin, (var - XShift) * XScale, (ymax - YShift) * YScale);
                } else {
                    formatter.format(
                            "\\draw [line width=%.2fpt, color=%s] ([xshift=%.4f] %.4f, %.4f) rectangle ([xshift=-%.4f] %.4f, %.4f); %%%n",
                            lineWidth[i], colorString, currentMargin,
                            (currentBin.getStartBoundary() - XShift) * XScale, 0.0, currentMargin,
                            (var - XShift) * XScale, (ymax - YShift) * YScale);

                    formatter.format(
                            "\\draw [line width=%.2fpt, color=%s, style=dotted] ([xshift=%.4f] %.4f, %.4f) rectangle ([yshift=3mm, xshift=-%.4f] %.4f, %.4f); %%%n",
                            lineWidth[i], colorString, currentMargin,
                            (currentBin.getStartBoundary() - XShift) * XScale, (ymax - YShift) * YScale,
                            currentMargin, (var - XShift) * XScale, (ymax - YShift) * YScale);
                }
            }
        }
    }
    return formatter.toString();
}

From source file:org.jfree.experimental.swt.SWTGraphics2D.java

/**
 * Sets the current color for this graphics context.
 *
 * @param color  the color (<code>null</code> permitted but ignored).
 *
 * @see #getColor()/*from ww  w. ja  va2  s.c  o  m*/
 */
public void setColor(Color color) {
    if (color == null) {
        return;
    }
    org.eclipse.swt.graphics.Color swtColor = getSwtColorFromPool(color);
    this.gc.setForeground(swtColor);
    // handle transparency and compositing.
    if (this.composite instanceof AlphaComposite) {
        AlphaComposite acomp = (AlphaComposite) this.composite;
        switch (acomp.getRule()) {
        case AlphaComposite.SRC_OVER:
            this.gc.setAlpha((int) (color.getAlpha() * acomp.getAlpha()));
            break;
        default:
            this.gc.setAlpha(color.getAlpha());
            break;
        }
    }
}

From source file:slash.navigation.mapview.browser.BrowserMapView.java

float asOpacity(Color color) {
    return MINIMUM_OPACITY + color.getAlpha() / 256f * (1 - MINIMUM_OPACITY);
}

From source file:forge.toolbox.FSkin.java

private static BufferedImage testPreferredSprite(final FSkinProp s0) {
    tempCoords = s0.getCoords();/*from  w  w w  .  ja v  a  2 s.  c om*/
    x0 = tempCoords[0];
    y0 = tempCoords[1];
    w0 = tempCoords[2];
    h0 = tempCoords[3];

    if (s0.equals(FSkinProp.IMG_QUEST_DRAFT_DECK)) {
        final Color c = getColorFromPixel(bimQuestDraftDeck.getRGB((x0 + w0 / 2), (y0 + h0 / 2)));
        if (c.getAlpha() != 0) {
            return bimQuestDraftDeck;
        }
    }

    // Test if requested sub-image in inside bounds of preferred sprite.
    // (Height and width of preferred sprite were set in loadFontAndImages.)
    if (x0 > preferredW || x0 + w0 > preferredW || y0 > preferredH || y0 + h0 > preferredH) {
        return bimDefaultSprite;
    }

    // Test if various points of requested sub-image are transparent.
    // If any return true, image exists.
    int x, y;
    Color c;

    // Center
    x = (x0 + w0 / 2);
    y = (y0 + h0 / 2);
    c = getColorFromPixel(bimPreferredSprite.getRGB(x, y));
    if (c.getAlpha() != 0) {
        return bimPreferredSprite;
    }

    x += 2;
    y += 2;
    c = getColorFromPixel(bimPreferredSprite.getRGB(x, y));
    if (c.getAlpha() != 0) {
        return bimPreferredSprite;
    }

    x -= 4;
    c = getColorFromPixel(bimPreferredSprite.getRGB(x, y));
    if (c.getAlpha() != 0) {
        return bimPreferredSprite;
    }

    y -= 4;
    c = getColorFromPixel(bimPreferredSprite.getRGB(x, y));
    if (c.getAlpha() != 0) {
        return bimPreferredSprite;
    }

    x += 4;
    c = getColorFromPixel(bimPreferredSprite.getRGB(x, y));
    if (c.getAlpha() != 0) {
        return bimPreferredSprite;
    }

    return bimDefaultSprite;
}

From source file:forge.toolbox.FSkin.java

private static void assembleAvatars() {
    avatars = new HashMap<>();
    int counter = 0;
    Color pxTest;

    if (bimPreferredAvatars != null) {
        final int pw = bimPreferredAvatars.getWidth();
        final int ph = bimPreferredAvatars.getHeight();

        for (int j = 0; j < ph; j += 100) {
            for (int i = 0; i < pw; i += 100) {
                if (i == 0 && j == 0) {
                    continue;
                }/*w  w  w  . j  av a 2 s . c o  m*/
                pxTest = getColorFromPixel(bimPreferredAvatars.getRGB(i + 50, j + 50));
                if (pxTest.getAlpha() == 0) {
                    continue;
                }
                avatars.put(counter++, new SkinImage(bimPreferredAvatars.getSubimage(i, j, 100, 100)));
            }
        }
    } else {

        final int aw = bimDefaultAvatars.getWidth();
        final int ah = bimDefaultAvatars.getHeight();

        for (int j = 0; j < ah; j += 100) {
            for (int i = 0; i < aw; i += 100) {
                if (i == 0 && j == 0) {
                    continue;
                }
                pxTest = getColorFromPixel(bimDefaultAvatars.getRGB(i + 50, j + 50));
                if (pxTest.getAlpha() == 0) {
                    continue;
                }
                avatars.put(counter++, new SkinImage(bimDefaultAvatars.getSubimage(i, j, 100, 100)));
            }
        }
    }
}