Java Color Darker darken(Color c, double f)

Here you can find the source of darken(Color c, double f)

Description

Darken a color by a given amount

License

GNU General Public License

Declaration

public static Color darken(Color c, double f) 

Method Source Code

//package com.java2s;
/*//from w w  w.  j  a v a 2  s .c  o m
 * Copyright (C) 2015  University of Oregon
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Apache License, as specified in the LICENSE file.
 *
 * For more information, see the LICENSE file.
 */

import java.awt.*;

public class Main {
    /**
     * Darken a color by a given amount  
     */
    public static Color darken(Color c, double f) {
        return mix(c, Color.black, f);
    }

    /**
     * Mix two colors by a given amount  
     */
    public static Color mix(Color c1, Color c2, double f) {
        if (f >= 1.0)
            return c2;

        if (f <= 0)
            return c1;

        double r1 = c1.getRed();
        double g1 = c1.getGreen();
        double b1 = c1.getBlue();
        double r2 = c2.getRed();
        double g2 = c2.getGreen();
        double b2 = c2.getBlue();
        double r = f * r2 + (1 - f) * r1;
        double g = f * g2 + (1 - f) * g1;
        double b = f * b2 + (1 - f) * b1;

        Color c3 = new Color(Math.min((int) (r), 255), Math.min((int) (g),
                255), Math.min((int) (b), 255));

        return c3;
    }
}

Related

  1. darken(Color col)
  2. darken(final Color color, final double percentage)
  3. darken(final Color color, final int amount)
  4. darkenColor(Color color)