Java Color Blend blend(Color col1, Color col2, float weight1)

Here you can find the source of blend(Color col1, Color col2, float weight1)

Description

Blend two colours, in the given proportions.

License

Apache License

Parameter

Parameter Description
col1 the first colour
col2 the second colour
weight1 the weight given to col1 (from 0.0-1.0)

Declaration

public static Color blend(Color col1, Color col2, float weight1) 

Method Source Code


//package com.java2s;
/*/* w w w.  java  2s . c o m*/
 *    Copyright 2010-2012 University of Toronto
 *
 *    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.
 */

import java.awt.Color;

public class Main {
    /**
     * Blend two colours, in the given proportions. Resulting alpha is always
     * 1.0.
     *
     * @param col1 the first colour
     * @param col2 the second colour
     * @param weight1 the weight given to col1 (from 0.0-1.0)
     */
    public static Color blend(Color col1, Color col2, float weight1) {

        float weight2 = (1.0F - weight1) / 255;
        weight1 /= 255;

        // This constructor expects values from 0.0F to 1.0F, so weights have to be scaled appropriately.
        return new Color(col1.getRed() * weight1 + col2.getRed() * weight2,
                col1.getGreen() * weight1 + col2.getGreen() * weight2,
                col1.getBlue() * weight1 + col2.getBlue() * weight2);
    }
}

Related

  1. blend(Color c0, Color c1)
  2. blend(Color c1, Color c2)
  3. blend(Color c1, Color c2)
  4. blend(Color c1, Color c2, double v)
  5. blend(Color cFrom, Color cTo, float factor)
  6. blend(Color color1, Color color2, float ratio)
  7. blend(Color color1, Color color2, int factor)
  8. blend(Color origin, Color over)
  9. blend(Color pColor, Color pOther)