Java examples for 2D Graphics:Color
Blends two colors and returns the result
/* Copyright (c) 2011 Danish Maritime Authority. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License./*from w ww .java2s . co m*/ */ //package com.java2s; import java.awt.Color; public class Main { /** * Blends two colors and returns the result * * @param c0 the first color * @param c1 the second color * @return the blended result */ public static Color blendColors(Color c0, Color c1) { double totalAlpha = c0.getAlpha() + c1.getAlpha(); double weight0 = c0.getAlpha() / totalAlpha; double weight1 = c1.getAlpha() / totalAlpha; double r = weight0 * c0.getRed() + weight1 * c1.getRed(); double g = weight0 * c0.getGreen() + weight1 * c1.getGreen(); double b = weight0 * c0.getBlue() + weight1 * c1.getBlue(); double a = Math.max(c0.getAlpha(), c1.getAlpha()); return new Color((int) r, (int) g, (int) b, (int) a); } }