Java Swing Icon blendIcon(Icon icon, float iconWeight, int rgb, float rgbWeight)

Here you can find the source of blendIcon(Icon icon, float iconWeight, int rgb, float rgbWeight)

Description

Add a color layer to the icon (not including alpha)

License

Open Source License

Declaration

public static ImageIcon blendIcon(Icon icon, float iconWeight, int rgb, float rgbWeight) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w w w .j  a v a 2s.c o m
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class Main {
    /**
     * Add a color layer to the icon (not including alpha)
     */
    public static ImageIcon blendIcon(Icon icon, float iconWeight, int rgb, float rgbWeight) {
        if (icon == null || !(icon instanceof ImageIcon)) {
            return null;
        }
        BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(((ImageIcon) icon).getImage(), 0, 0, null);
        g.dispose();
        float wsum = iconWeight + rgbWeight;
        if (wsum > 0) {
            iconWeight = iconWeight / wsum;
            rgbWeight = rgbWeight / wsum;
        }
        int rmask = (rgb >> 16) & 0xff;
        int gmask = (rgb >> 8) & 0xff;
        int bmask = rgb & 0xff;
        int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
        for (int i = 0; i < pixels.length; i++) {
            int c = pixels[i];
            pixels[i] = (c & 0xff000000) | (mergeComponent((c >> 16) & 0xff, iconWeight, rmask, rgbWeight) << 16)
                    | (mergeComponent((c >> 8) & 0xff, iconWeight, gmask, rgbWeight) << 8)
                    | (mergeComponent(c & 0xff, iconWeight, bmask, rgbWeight));
        }
        image.setRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
        return new ImageIcon(image);
    }

    private static int mergeComponent(int a, float wa, int b, float wb) {
        int c = (int) (wa * a + wb * b);
        if (c >= 0 && c <= 255) {
            return c;
        }
        if (c < 0) {
            return 0;
        }
        if (c > 255) {
            return 255;
        }
        return c;
    }
}

Related

  1. areSizesEqual(javax.swing.Icon prevIcon, javax.swing.Icon nextIcon)
  2. blitBitmap(Graphics g, Icon icon, int x, int y, int w, int h)
  3. buildIcon(final String relativePath, final Class loader)
  4. changeBrightness(Icon icon, float factor)
  5. clearMergedIconsCache()