Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to convert Integer range to RGB color

Tags:

java

colors

I know you guys are all very busy, so I'm gonna keep it short and to the point.

I'm currently developing a little game for fun. There's enemys in the game. For simplicity, think about them as colored squares. I want to minimize any HUD, so I decided to display the hit points of the creatures smoothly via their color (green for healthy, yellow for damaged, red for nearly dead).

See image : enter image description here

I however really struggle to come up with a decently efficient method that converts an (int) hp-value to an RGB-color. Mapping to a single int from 0-255 would not be a problem at all - here's an example function that does exactly that:

public int mapHpToGreyscale(int input) {

    //input = current hp
    double minHealth = 0;
    double maxHealth = hpmax;
    double minColValue = 0;
    double maxColValue = 255;

    int output = (int) ((input - minHealth) / (maxHealth - minHealth) * (maxColValue - minColValue) + minColValue);

    return output;
}

Is there a quick and easy way to achieve what I want to do? I'd appreciate any input.

like image 429
Aaron Kübler Avatar asked Oct 25 '17 09:10

Aaron Kübler


People also ask

How do you convert numbers to RGB?

Hex to RGB conversionGet the 2 left digits of the hex color code and convert to decimal value to get the red color level. Get the 2 middle digits of the hex color code and convert to decimal value to get the green color level.

What is the range of integer values of each RGB color?

RGB Color Values Each parameter (red, green, and blue) defines the intensity of the color with a value between 0 and 255. This means that there are 256 x 256 x 256 = 16777216 possible colors!

Are RGB values integers?

RGB color space or RGB color system, constructs all the colors from the combination of the Red, Green and Blue colors. The red, green and blue use 8 bits each, which have integer values from 0 to 255. This makes 256*256*256=16777216 possible colors.

Why do RGB values range from 0 to 255 can they be any other range?

RGB (Red, Green, Blue) are 8 bit each. The range for each individual colour is 0-255 (as 2^8 = 256 possibilities). The combination range is 256*256*256. By dividing by 255, the 0-255 range can be described with a 0.0-1.0 range where 0.0 means 0 (0x00) and 1.0 means 255 (0xFF).


1 Answers

import java.lang.Math;

public class Color {
    public int r;
    public int g;
    public int b;
    public Color() {
        r = 0;
        g = 0;
        b = 0;
    }
};

public static Color hpToColor(float hp, float maxhp) {
    Color color = new Color();
    float alpha = hp / maxhp;

    if (alpha <= 0.5) {
        color.r = 255;
        color.g = Math.round((alpha * 2) * 255);
        color.b = 0;
    } else {
        color.r = Math.round(((1 - alpha) * 2) * 255);
        color.g = 255;
        color.b = 0;
    }

    return color;
}
like image 80
Ardavast Dayleryan Avatar answered Sep 20 '22 15:09

Ardavast Dayleryan