Java Color Mix mix(Color c1, Color c2)

Here you can find the source of mix(Color c1, Color c2)

Description

Mix colors visually: red + green = yellow, etc.

License

Open Source License

Declaration

static public final Color mix(Color c1, Color c2) 

Method Source Code

//package com.java2s;
/**//  w  w w .j  a v a 2s  . c o  m
    
TrakEM2 plugin for ImageJ(C).
Copyright (C) 2005-2009 Albert Cardona and Rodney Douglas.
    
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 (http://www.gnu.org/licenses/gpl.txt )
    
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
    
You may contact Albert Cardona at acardona at ini.phys.ethz.ch
Institute of Neuroinformatics, University of Zurich / ETH, Switzerland.
    
*/

import java.awt.Color;

public class Main {
    /** Mix colors visually: red + green = yellow, etc.*/
    static public final Color mix(Color c1, Color c2) {
        final float[] b = Color.RGBtoHSB(c1.getRed(), c1.getGreen(), c1.getBlue(), new float[3]);
        final float[] c = Color.RGBtoHSB(c2.getRed(), c2.getGreen(), c2.getBlue(), new float[3]);
        final float[] a = new float[3];
        // find to which side the hue values are closer, since hue space is a a circle
        // hue values all between 0 and 1
        float h1 = b[0];
        float h2 = c[0];
        if (h1 < h2) {
            float tmp = h1;
            h1 = h2;
            h2 = tmp;
        }
        float d1 = h2 - h1;
        float d2 = 1 + h1 - h2;
        if (d1 < d2) {
            a[0] = h1 + d1 / 2;
        } else {
            a[0] = h2 + d2 / 2;
            if (a[0] > 1)
                a[0] -= 1;
        }

        for (int i = 1; i < 3; i++)
            a[i] = (b[i] + c[i]) / 2; // only Saturation and Brightness can be averaged
        return Color.getHSBColor(a[0], a[1], a[2]);
    }
}

Related

  1. mix(Color a, Color b, double percent)
  2. mix(Color c0, Color c1, float amount)
  3. mix(Color c1, Color c2)
  4. mix(Color c1, Color c2, boolean useAlpha)
  5. mix(Color c1, Color c2, double f)
  6. mix(Color c1, Color c2, float bias)
  7. mix(Color src, Color dst, double factor)