Create a brighter color by changing the b component of a hsb-color (b=brightness, h=hue, s=saturation) - Java 2D Graphics

Java examples for 2D Graphics:Color HSB

Description

Create a brighter color by changing the b component of a hsb-color (b=brightness, h=hue, s=saturation)

Demo Code


//package com.java2s;
import java.awt.*;

public class Main {
    public static Color brighter(Color color) {
        return brighter(color, 0.05f);
    }/*from w  w w.j  a  va 2 s.  c om*/

    /** Create a brighter color by changing the b component of a
     *    hsb-color (b=brightness, h=hue, s=saturation)
     */
    public static Color brighter(Color color, float factor) {
        float[] hsbvals;

        hsbvals = new float[3];
        Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(),
                hsbvals);

        return setBrightness(color, hsbvals[2] + factor);
    }

    public static Color setBrightness(Color color, float brightness) {
        float[] hsbvals;

        hsbvals = new float[3];
        Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(),
                hsbvals);
        hsbvals[2] = brightness;
        hsbvals[2] = Math.min(hsbvals[2], 1.0f);
        hsbvals[2] = Math.max(hsbvals[2], 0.0f);

        color = new Color(
                Color.HSBtoRGB(hsbvals[0], hsbvals[1], hsbvals[2]));

        return color;
    }
}

Related Tutorials