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:ImageProcessing.ImageProcessing.java

public static double[] extractAlphaValue(BufferedImage source) {
    //Extracts the Alpha value from the RGB value of the source pixels.
    int imageWidth = source.getWidth();
    int imageHeight = source.getHeight();
    double[] values = new double[imageWidth * imageHeight];

    for (int i = 0; i < imageHeight; i++) {
        for (int j = 0; j < imageWidth; j++) {
            int rgbValue = source.getRGB(j, i);
            Color currentPixel = new Color(rgbValue, true);
            int value = currentPixel.getAlpha();
            values[(i * imageWidth) + j] = value;
        }/*from   w  w w .j a  v  a  2s  . c  o m*/
    }

    return values;
}

From source file:ColorUtil.java

public static final Color mult(Color c, double amount) {
    return c == null ? null
            : new Color(Math.min(255, (int) (c.getRed() * amount)),
                    Math.min(255, (int) (c.getGreen() * amount)), Math.min(255, (int) (c.getBlue() * amount)),
                    c.getAlpha());
}

From source file:com.teambrmodding.neotech.client.models.ModelItemFluidStorage.java

/**
 * Change standard color into color MC wants for vertex data
 * @param input The input//w w w  .ja  v  a2s.c  o m
 * @return The correct color to add to the vertex data
 */
public static int fixColorForMC(int input) {
    // ABGR, MC is reverse for some reason
    Color outputColor = new Color(input);
    int a = outputColor.getAlpha();
    int r = outputColor.getRed();
    int g = outputColor.getGreen();
    int b = outputColor.getBlue();

    return new Color(b, g, r, a).getRGB();
}

From source file:Main.java

/**
 * Linear interpolation.// w w  w. ja  v  a 2  s . co m
 * @param color0
 * @param color1
 * @param amount
 * @return
 */
public static Color Lerp(Color color0, Color color1, double amount) {
    int r = (int) Lerp(color0.getRed(), color1.getRed(), amount);
    int g = (int) Lerp(color0.getGreen(), color1.getGreen(), amount);
    int b = (int) Lerp(color0.getBlue(), color1.getBlue(), amount);
    int a = (int) Lerp(color0.getAlpha(), color1.getAlpha(), amount);
    if (r > 255)
        r = 255;
    if (r < 0)
        r = 0;
    if (g > 255)
        g = 255;
    if (g < 0)
        g = 0;
    if (b > 255)
        b = 255;
    if (b < 0)
        b = 0;
    if (a > 255)
        a = 255;
    if (a < 0)
        a = 0;
    return new Color(r, g, b, a);
}

From source file:Gradient.java

/**
 * Creates an array of Color objects for use as a gradient, using a linear
 * interpolation between the two specified colors.
 * //w  w  w  .ja  v  a  2s.  co m
 * @param one
 *            Color used for the bottom of the gradient
 * @param two
 *            Color used for the top of the gradient
 * @param numSteps
 *            The number of steps in the gradient. 250 is a good number.
 */
public static Color[] createGradient(final Color one, final Color two, final int numSteps) {
    int r1 = one.getRed();
    int g1 = one.getGreen();
    int b1 = one.getBlue();
    int a1 = one.getAlpha();

    int r2 = two.getRed();
    int g2 = two.getGreen();
    int b2 = two.getBlue();
    int a2 = two.getAlpha();

    int newR = 0;
    int newG = 0;
    int newB = 0;
    int newA = 0;

    Color[] gradient = new Color[numSteps];
    double iNorm;
    for (int i = 0; i < numSteps; i++) {
        iNorm = i / (double) numSteps; // a normalized [0:1] variable
        newR = (int) (r1 + iNorm * (r2 - r1));
        newG = (int) (g1 + iNorm * (g2 - g1));
        newB = (int) (b1 + iNorm * (b2 - b1));
        newA = (int) (a1 + iNorm * (a2 - a1));
        gradient[i] = new Color(newR, newG, newB, newA);
    }

    return gradient;
}

From source file:ColorUtil.java

public static final Color add(Color c1, Color c2) {
    return c1 == null ? c2
            : c2 == null ? c1//from w w  w .ja v a  2  s  .c o m
                    : new Color(Math.min(255, c1.getRed() + c2.getRed()),
                            Math.min(255, c1.getGreen() + c2.getGreen()),
                            Math.min(255, c1.getBlue() + c2.getBlue()), c1.getAlpha());
}

From source file:com.igormaznitsa.jhexed.swing.editor.ui.exporters.SVGImageExporter.java

private static String alphaToSVG(final Color color) {
    final int a = color.getAlpha();
    if (a == 0) {
        return "0.0";
    }//from  w ww.ja va2 s . c  o m
    if (a >= 255) {
        return "1.0";
    }
    return num2str((float) a / (float) 255);
}

From source file:Main.java

/**
 * Make a color scaled using a defined factor.
 *
 * @param color/*from   ww  w.  ja  va 2 s  .com*/
 *          the color to scale.
 * @param factor
 *          the factor to use.
 * @return the scaled color.
 */
public static Color getScaledColor(Color color, double factor) {
    if (color == null) {
        return null;
    }
    if (factor <= 1) {
        return new Color(Math.max((int) (color.getRed() * factor), 0),
                Math.max((int) (color.getGreen() * factor), 0), Math.max((int) (color.getBlue() * factor), 0),
                color.getAlpha());
    }
    int r = color.getRed();
    int g = color.getGreen();
    int b = color.getBlue();

    int i = (int) (1.0 / (factor - 1));
    if (r == 0 && g == 0 && b == 0) {
        return new Color(i, i, i);
    }
    if (r > 0 && r < i) {
        r = i;
    }
    if (g > 0 && g < i) {
        g = i;
    }
    if (b > 0 && b < i) {
        b = i;
    }

    return new Color(Math.min((int) (r / (2 - factor)), 255), Math.min((int) (g / (2 - factor)), 255),
            Math.min((int) (b / (2 - factor)), 255));

}

From source file:com.igormaznitsa.jhexed.swing.editor.ui.Utils.java

public static Color getListBackground(final boolean selected) {
    if (selected) {
        if (isUnderNimbusLookAndFeel()) {
            return UIManager.getColor("List[Selected].textBackground"); // Nimbus
        }//from   w  w w.  ja  va2  s.  c o m
        return UIManager.getColor("List.selectionBackground");
    } else {
        if (isUnderNimbusLookAndFeel()) {
            final Color color = UIManager.getColor("List.background");
            //noinspection UseJBColor
            return new Color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
        }
        // Under GTK+ L&F "Table.background" often has main panel color, which looks ugly
        return isUnderGTKLookAndFeel() ? getTreeTextBackground() : UIManager.getColor("List.background");
    }
}

From source file:de.bund.bfr.jung.JungUtils.java

private static Paint mixWith(Paint paint, Color mix) {
    if (paint instanceof Color) {
        Color c = (Color) paint;

        return new Color((c.getRed() + mix.getRed()) / 2, (c.getGreen() + mix.getGreen()) / 2,
                (c.getBlue() + mix.getBlue()) / 2, (c.getAlpha() + mix.getAlpha()) / 2);
    } else if (paint instanceof TexturePaint) {
        BufferedImage texture = ((TexturePaint) paint).getImage();
        BufferedImage mixed = new BufferedImage(texture.getWidth(), texture.getHeight(), texture.getType());

        for (int x = 0; x < texture.getWidth(); x++) {
            for (int y = 0; y < texture.getHeight(); y++) {
                mixed.setRGB(x, y, ((Color) mixWith(new Color(texture.getRGB(x, y)), mix)).getRGB());
            }/*from w  w  w .  j  a  v a 2s .  co m*/
        }

        return new TexturePaint(mixed, new Rectangle(mixed.getWidth(), mixed.getHeight()));
    } else {
        return paint;
    }
}