Java Color Mix mixColors(List colors)

Here you can find the source of mixColors(List colors)

Description

mix Colors

License

LGPL

Declaration

public static Color mixColors(List<Color> colors) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import javax.annotation.Nonnull;
import java.awt.*;
import java.util.List;

public class Main {
    public static Color mixColors(List<Color> colors) {
        int red = 0, green = 0, blue = 0;

        for (Color color : colors) {

            double colorCount = 0;
            if (color.getRed() > 0)
                colorCount++;//w w  w.  ja  v a2 s.c o  m
            if (color.getGreen() > 0)
                colorCount++;
            if (color.getBlue() > 0)
                colorCount++;
            if (colorCount <= 0)
                continue;

            red += color.getRed() * color.getAlpha() / 255F / colorCount;
            green += color.getGreen() * color.getAlpha() / 255F / colorCount;
            blue += color.getBlue() * color.getAlpha() / 255F / colorCount;
        }

        if (!colors.isEmpty()) {
            red = Math.min(red / colors.size(), 255);
            green = Math.min(green / colors.size(), 255);
            blue = Math.min(blue / colors.size(), 255);
        }

        return new Color(red, green, blue);
    }

    public static Color mixColors(@Nonnull Color color1, @Nonnull Color color2) {

        float r = ((color1.getRed() / 255f) + (color2.getRed() / 255f)) / 2f;
        float g = ((color1.getGreen() / 255f) + (color2.getGreen() / 255f)) / 2f;
        float b = ((color1.getBlue() / 255f) + (color2.getBlue() / 255f)) / 2f;
        float a = ((color1.getAlpha() / 255f) + (color2.getAlpha() / 255f)) / 2f;

        if (Math.round(r * 255f) == color2.getRed())
            r = color2.getRed() / 255f;
        if (Math.round(g * 255f) == color2.getGreen())
            g = color2.getGreen() / 255f;
        if (Math.round(b * 255f) == color2.getBlue())
            b = color2.getBlue() / 255f;
        if (Math.round(a * 255f) == color2.getAlpha())
            a = color2.getAlpha() / 255f;

        return new Color(r, g, b, a);
    }
}

Related

  1. mix(final Color c1, final Color c2, final double factor)
  2. mix(int a, int b, int amt)
  3. mixColor(Paint p, double value)
  4. mixColors(Color a, Color b, double r)
  5. mixColors(Color c)
  6. mixColorWithAlpha(Color base, Color mix)
  7. mixedColor(Color originalColor, Color overlayColor)
  8. mixOver(Color backColor, Color frontColor)
  9. mixWith(Paint paint, Color mix)