darker Color by value - Java 2D Graphics

Java examples for 2D Graphics:Color Dark

Description

darker Color by value

Demo Code


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

public class Main {
    public static Color darker(Color c, int value) {
        if (value < 0)
            return c;

        float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(),
                new float[3]);
        int h = (int) (hsb[0] * 360);
        int s = (int) (hsb[1] * 100);
        int b = (int) (hsb[2] * 100);

        int rm = 0;
        b -= value;/*from w  w  w  .  ja  va2  s .c o  m*/
        if (b < 0) {
            rm = b * (-1);
            b = 0;
        }
        s += rm;
        if (s > 100)
            s = 100;

        int rgb = Color.HSBtoRGB(h / 360.0f, s / 100.0f, b / 100.0f);
        return new Color(rgb);
    }
}

Related Tutorials