Java Color Mix mixWith(Paint paint, Color mix)

Here you can find the source of mixWith(Paint paint, Color mix)

Description

mix With

License

Open Source License

Declaration

private static Paint mixWith(Paint paint, Color mix) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 German Federal Institute for Risk Assessment (BfR)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.//from w ww .j  a v a2  s .c om
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *     Department Biological Safety - BfR
 *******************************************************************************/

import java.awt.Color;
import java.awt.Paint;
import java.awt.Rectangle;

import java.awt.TexturePaint;

import java.awt.image.BufferedImage;

public class Main {
    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());
                }
            }

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

Related

  1. mixColors(Color c)
  2. mixColors(List colors)
  3. mixColorWithAlpha(Color base, Color mix)
  4. mixedColor(Color originalColor, Color overlayColor)
  5. mixOver(Color backColor, Color frontColor)