Java Color Darker darker(Color color, float fraction)

Here you can find the source of darker(Color color, float fraction)

Description

Make a color darker.

License

Open Source License

Parameter

Parameter Description
color Color to make darker.
fraction Darkness fraction.

Return

Darker color.

Declaration

public static Color darker(Color color, float fraction) 

Method Source Code


//package com.java2s;
/*/*from w w  w  .j  a v  a2 s. c o  m*/
 *  "BSLib", Brainstorm Library.
 *  Copyright (C) 2015 by Sergey V. Zhdanovskih.
 *
 *  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, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  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, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;

public class Main {
    /**
     * Make a color darker.
     *
     * @param color Color to make darker.
     * @param fraction Darkness fraction.
     * @return Darker color.
     */
    public static Color darker(Color color, float fraction) {
        float factor = (1.0f - fraction);
        int rgb = color.getRGB();

        int red = (rgb >> 16) & 0xFF;
        int green = (rgb >> 8) & 0xFF;
        int blue = (rgb >> 0) & 0xFF;
        int alpha = (rgb >> 24) & 0xFF;

        red = (int) (red * factor);
        green = (int) (green * factor);
        blue = (int) (blue * factor);

        red = (red < 0) ? 0 : red;
        green = (green < 0) ? 0 : green;
        blue = (blue < 0) ? 0 : blue;

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

Related

  1. darker(Color col, double FACTOR)
  2. darker(Color color)
  3. darker(Color color)
  4. darker(Color color, double factor)
  5. darker(Color color, double fraction)
  6. darker(Color color, float ratio)
  7. darker(final Color color, final int rgbOffset)
  8. darker(final Color color, float factor)
  9. darker(int channel, int intensity)