Java Color Mix mix(Color c0, Color c1, float amount)

Here you can find the source of mix(Color c0, Color c1, float amount)

Description

Mixes two colors.

License

Open Source License

Declaration

public static Color mix(Color c0, Color c1, float amount) 

Method Source Code

//package com.java2s;
/**/*from   w ww. j a v  a2s.c o  m*/
 * Copyright (c) 2010-2014, Jean-Daniel Fekete, Pierre Dragicevic, and INRIA.
 * All rights reserved.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

import java.awt.Color;

public class Main {
    /**
     * Mixes two colors.
     */
    public static Color mix(Color c0, Color c1, float amount) {
        float r0 = c0.getRed() / 255f;
        float g0 = c0.getGreen() / 255f;
        float b0 = c0.getBlue() / 255f;
        float a0 = c0.getAlpha() / 255f;
        float r1 = c1.getRed() / 255f;
        float g1 = c1.getGreen() / 255f;
        float b1 = c1.getBlue() / 255f;
        float a1 = c1.getAlpha() / 255f;
        return new Color(bound01(r0 + (r1 - r0) * amount), bound01(g0 + (g1 - g0) * amount),
                bound01(b0 + (b1 - b0) * amount), bound01(a0 + (a1 - a0) * amount));
    }

    private static float bound01(float x) {
        if (x < 0)
            x = 0;
        if (x > 1)
            x = 1;
        return x;
    }
}

Related

  1. mix(Color a, Color b, double percent)
  2. mix(Color c1, Color c2)
  3. mix(Color c1, Color c2)
  4. mix(Color c1, Color c2, boolean useAlpha)
  5. mix(Color c1, Color c2, double f)