Java Utililty Methods Color Alpha

List of utility methods to do Color Alpha

Description

The list of methods to do Color Alpha are organized into topic(s).

Method

voidapplyColorFilter(Image image, Color color, float alpha)
Apply simple color filter with specified alpha factor to the image
if (image != null) {
    final Graphics2D g = (Graphics2D) image.getGraphics();
    final Rectangle rect = new Rectangle(image.getWidth(null), image.getHeight(null));
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    g.setColor(color);
    g.fill(rect);
    g.dispose();
Color[]buildColorRamp(Color startColor, Color endColor, int numColors, int alpha)
build Color Ramp
Color[] colors = new Color[numColors];
for (int j = 0; j < colors.length; j++) {
    float ratio = (float) j / (float) colors.length;
    int red = (int) (endColor.getRed() * ratio + startColor.getRed() * (1 - ratio));
    int green = (int) (endColor.getGreen() * ratio + startColor.getGreen() * (1 - ratio));
    int blue = (int) (endColor.getBlue() * ratio + startColor.getBlue() * (1 - ratio));
    colors[j] = new Color(red, green, blue, alpha);
return colors;
ColorchangeAlpha(Color c, double alpha)
change Alpha
return new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (alpha * 255));
ColorchangeAlpha(Color c, double newAlpha)
change Alpha
if (newAlpha < 0)
    newAlpha = 0;
if (newAlpha > 1)
    newAlpha = 1;
return new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) Math.round(newAlpha * 255));
ColorchangeAlpha(Color c, int alpha)
Changes the alpha of a given color.
if (c == null)
    return null;
return new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha);
ColorchangeColorAlpha(@Nonnull Color color, int newAlpha)
change Color Alpha
return new Color(color.getRed(), color.getGreen(), color.getBlue(), newAlpha);
ColorderiveWithAlpha(Color color, int alpha)
Creates a new color with the same color components but a different alpha value.
return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
ColornewColourWithAlpha(Color color, double alpha)
new Colour With Alpha
Color result = new Color(color.getRed(), color.getGreen(), color.getBlue(), (int) (255 * alpha));
return result;
ColornoAlpha(final Color col)
Removes the transparency of the given color.
return noAlpha(col, null);
ColoroverwriteAlpha(Color c, float alpha)
Overwrites alpha value for given color.
Color retVal = c;
if (c != null) {
    retVal = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (alpha * 255 + 0.5));
return retVal;