简体   繁体   中英

Changing brightness/saturation of a color in java

Currently I am working on a simulation project using javax.swing and I want to draw "grass" on my map based on it's food value. My food value is a double between 0 < 1, and I want to make it brighter as it gets bigger. I have been reading about HSB/HSV but cannot figure out how it works / the syntax of it. What is a good solution to this problem? Or can someone direct me somewhere that has a good tutorial?

class Grass{

    private void setColor(){
        grassColor = new Color(107, 142, 35); //RGB value I want to start with as "dead grass"
    }
    public Color getColor(){
        return grassColor; //this is what i want to change based on food value;
    }
    private double growthRate = 0.1;
    public void grow(){
        foodValue += (foodValue < 1.0) ? growthRate : 0; //grows the grass to max size of 1.0
    }

I guess I should also mention my food value is set to a random float when it initializes. Appreciate any advice.

    private void setColor(){
        grassColor = new Color(107, 142, 35);
    }

See those numbers used to create your custom colour? They can be variable names pointing to numbers which get updated elsewhere based on whatever else in your program is supposed to influence the colour.

I sometimes refer to https://www.google.com/search?client=safari&rls=en&q=rgb+color+picker&ie=UTF-8&oe=UTF-8 to pick a suitable colour for the occasion. It neatly shows the values for different systems, not just RGB.

https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#getHSBColor(float,%20float,%20float) will give you a way to use HSB inputs if you don't want to go with the RGB system. There may be some other useful stuff there if you want to try yet another colour system.

You are probably best using the HSB colour model.

  • H = Hue; represents the colour circle:
    Red, Yellow, Green, Cyan, Blue, Magenta, and back to Red.

  • S = Saturation; how pure the colour is:
    Zero saturation is always a shade of grey. (R,G,B values are all equal)
    Maximum saturation always has at least one of RGB value equal to zero.

  • B = Brightness; how dark the colour is:
    Zero brightness is always Black

Using java.awt.Color.getHSBColor(h, s, b) you might start at (0.166, 0.8, 0.5)
which would be (yellow, slightly muted, medium dark), that is brownish,
then progress to (0.333, 1.0, 1.0) for a pure spring green
and perhaps end up with (0.4, 1.0, 0.9) for that darker, slightly bluer, summer result.

Play with the numbers to suit your needs.

You could bind your "green" value of your color directly to your foodValue. This will increase the green intensity of your color according to your foodValue. This would require no additional changes.

Example:

class Grass {
    
    private double foodValue = 0.0;
    private double growthRate = 0.1;
    private Color grassColor;
    
    public Grass() {
        setColor();
    }
    
    private void setColor(){ // this could be removed if grassColor is initialized on top
        grassColor = new Color(107, 142 + (int)(foodValue * 100), 35); 
    }
    public Color getColor(){
        return grassColor;
    }
    
    public void grow(){
        foodValue += (foodValue < 1.0) ? growthRate : 0; //grows the grass to max size of 1.0
    }
}

This is really simple and may work for your purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM