Java Color Blend blendColor(Color clOne, Color clTwo, double amount)

Here you can find the source of blendColor(Color clOne, Color clTwo, double amount)

Description

Calculate a brighter or darker version of the originalColor.

License

Open Source License

Parameter

Parameter Description
originalColor a parameter
brightness The brightness to use in %. 1 = unchanged.

Declaration

public static Color blendColor(Color clOne, Color clTwo, double amount) 

Method Source Code

//package com.java2s;
/*/*from w  ww.  ja v a2  s. co m*/
 * This file is part of smarthomatic, http://www.smarthomatic.org.
 * Copyright (c) 2013 Uwe Freese
 *
 * smarthomatic 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.
 *
 * smarthomatic 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 smarthomatic. If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;

public class Main {
    /**
     * Calculate a brighter or darker version of the originalColor.
     * @param originalColor
     * @param brightness The brightness to use in %. 1 = unchanged.
     */
    public static Color blendColor(Color clOne, Color clTwo, double amount) {
        float fAmount = (float) amount;
        float fInverse = 1.0f - fAmount;

        // I had to look up getting color components in java.  Google is good :)
        float afOne[] = new float[3];
        clOne.getColorComponents(afOne);
        float afTwo[] = new float[3];
        clTwo.getColorComponents(afTwo);

        float afResult[] = new float[3];
        afResult[0] = afOne[0] * fAmount + afTwo[0] * fInverse;
        afResult[1] = afOne[1] * fAmount + afTwo[1] * fInverse;
        afResult[2] = afOne[2] * fAmount + afTwo[2] * fInverse;

        return new Color(afResult[0], afResult[1], afResult[2]);
    }
}

Related

  1. blend(double factor, Color color1, Color color2)
  2. blend(final Color color1, final Color color2, final double ratio)
  3. blend(final Color first, final Color second, final float alpha)
  4. blendAlpha(Color under, Color over)
  5. blendcol(Color in, Color bl)
  6. blendColorKeepAlpha(Color source, Color dest)
  7. blendColors(Color color, Color color1, double d)
  8. blendColors(Color from, Color to, double toFraction)
  9. blendColorsUnsafe(final float[] fractions, final Color[] colors, final float progress)