尝试将整数范围转换为RGB颜色

Aar*_*ler 4 java colors

我知道你们都很忙,所以我要简短一点。

我目前正在开发一款有趣的小游戏。游戏中有敌人。为简单起见,请将其视为彩色正方形。我想最小化任何HUD,所以我决定通过颜色平滑显示生物的生命值(绿色代表健康,黄色代表受损,红色代表几乎死亡)。

查看图片: 在此处输入图片说明

但是,我真的很难想出一种像样的高效方法将hp值转换为RGB颜色。从0到255映射到单个int根本不是问题-这是一个示例函数,正是这样:

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;
}
Run Code Online (Sandbox Code Playgroud)

有没有快速便捷的方法来实现我想要的目标?我将不胜感激。

Zab*_*uza 5

说明

首先,我的答案是对此的适应版本:计算从绿色到红色的颜色值。我决定为该JavaScript解决方案创建Java版本。您可以直接将其插入代码中的代码,而无需自己转移所有内容。

这个想法是使用HSL色相,饱和度,亮度)颜色空间而不是RGB(红色,绿色,蓝色)。有红色是由色调值表示和由绿色120°与黄色之间在60°平滑过渡在色调方面:

色相色圈 HSL色彩空间

我们将修复的饱和度100%和的亮度,50%但是如果您愿意,可以使用这些值。


用法

这是在代码中使用它的方式:

// A value between 1.0 (green) to 0.0 (red)
double percentage = ...
// Get the color (120° is green, 0° is red)
Color color = transitionOfHueRange(percentage, 120, 0);
Run Code Online (Sandbox Code Playgroud)

这就是结果范围:

色相范围示例


其他方法

这是transitionOfHueRange方法。它接受percentage介于0.0和之间的值,以及之间和之间1.0hue范围:0360

public static Color transitionOfHueRange(double percentage, int startHue, int endHue) {
    // From 'startHue' 'percentage'-many to 'endHue'
    // Finally map from [0°, 360°] -> [0, 1.0] by dividing
    double hue = ((percentage * (endHue - startHue)) + startHue) / 360;

    double saturation = 1.0;
    double lightness = 0.5;

    // Get the color
    return hslColorToRgb(hue, saturation, lightness);
}
Run Code Online (Sandbox Code Playgroud)

这是hslColorToRgb功能。它接受从到的HSL值:0.01.0

public static Color hslColorToRgb(double hue, double saturation, double lightness) {
    if (saturation == 0.0) {
        // The color is achromatic (has no color)
        // Thus use its lightness for a grey-scale color
        int grey = percToColor(lightness);
        return new Color(grey, grey, grey);
    }

    double q;
    if (lightness < 0.5) {
        q = lightness * (1 + saturation);
    } else {
        q = lightness + saturation - lightness * saturation;
    }
    double p = 2 * lightness - q;

    double oneThird = 1.0 / 3;
    double red = percToColor(hueToRgb(p, q, hue + oneThird));
    double green = percToColor(hueToRgb(p, q, hue));
    double blue = percToColor(hueToRgb(p, q, hue - oneThird));

    return new Color(red, green, blue);
}
Run Code Online (Sandbox Code Playgroud)

hueToRgb方法:

public static double hueToRgb(double p, double q, double t) {
    if (t < 0) {
        t += 1;
    }
    if (t > 1) {
        t -= 1;
    }

    if (t < 1.0 / 6) {
        return p + (q - p) * 6 * t;
    }
    if (t < 1.0 / 2) {
        return q;
    }
    if (t < 2.0 / 3) {
        return p + (q - p) * (2.0 / 3 - t) * 6;
    }
    return p;
}
Run Code Online (Sandbox Code Playgroud)

最后是小实用程序方法percToColor

public static int percToColor(double percentage) {
    return Math.round(percentage * 255);
}
Run Code Online (Sandbox Code Playgroud)