Java Color Brighten brighterColor(String hexValue)

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

Description

brighter Color

License

Open Source License

Declaration

public static final String brighterColor(String hexValue) 

Method Source Code

//package com.java2s;
/**/* w ww  . j a  v  a2 s  .co m*/
 * 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 brighterColor(String hexValue) {
        return brighterColor(hexValue, 0.1);
    }

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

    /**
     * Make a color lighter.
     *
     * @param color    Color to make lighter.
     * @param fraction Darkness fraction.
     * @return Lighter color.
     */
    public static Color lighter(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. brighter(Color color)
  2. brighter(Color color)
  3. brighter(Color color, double factor)
  4. brighter(Color color, float factor)
  5. brighterColor(Color c)
  6. brightness(Color c, float scale)
  7. brightness(java.awt.Color c)
  8. brightnessToAlpha(Image image, float alpha)
  9. changeBrightness(Color c, int percent)