Java Color Darker darkerColor(String hexValue)

Here you can find the source of darkerColor(String hexValue)

Description

darker Color

License

Open Source License

Declaration

public static final String darkerColor(String hexValue) 

Method Source Code

//package com.java2s;
/**//w  w  w  . j  a  v a 2 s . com
 * This file is part of mycollab-core.
 *
 * mycollab-core 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.
 *
 * mycollab-core 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 mycollab-core.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.*;

public class Main {
    public static final String darkerColor(String hexValue) {
        return darkerColor(hexValue, 0.1);
    }

    public static final String darkerColor(String hexValue, double fraction) {
        Color color = Color.decode(hexValue);
        Color brighter = darker(color, fraction);
        return toHexString(brighter);
    }

    /**
     * Make a color darker.
     *
     * @param color    Color to make darker.
     * @param fraction Darkness fraction.
     * @return Darker color.
     */
    public static Color darker(Color color, double fraction) {
        int red = (int) Math.round(color.getRed() * (1.0 - fraction));
        int green = (int) Math.round(color.getGreen() * (1.0 - fraction));
        int blue = (int) Math.round(color.getBlue() * (1.0 - fraction));

        if (red < 0)
            red = 0;
        else if (red > 255)
            red = 255;
        if (green < 0)
            green = 0;
        else if (green > 255)
            green = 255;
        if (blue < 0)
            blue = 0;
        else if (blue > 255)
            blue = 255;

        int alpha = color.getAlpha();

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

    public final static String toHexString(Color colour) throws NullPointerException {
        String hexColour = Integer.toHexString(colour.getRGB() & 0xffffff);
        if (hexColour.length() < 6) {
            hexColour = "000000".substring(0, 6 - hexColour.length()) + hexColour;
        }
        return "#" + hexColour;
    }
}

Related

  1. darker(final Color color, float factor)
  2. darker(int channel, int intensity)
  3. darkerColor(Color c)
  4. darkerColor(Color c, double amount)
  5. darkerColor(Color color, double factor)
  6. deriveColor(Color orig, int darker)
  7. draw3DFrame(Graphics g, Rectangle rect, Color colorBackground, Color colorDark, Color colorLight)
  8. getDarkColor()
  9. getDarkColors(Color[] colors, double fraction)