如何在libGDX中指定具有色相,饱和度和亮度的颜色,而不是r,g,b,a值。我注意到Color构造函数仅接受rgba,至少自动补全仅提供rgba。这有可能吗?我想创建一个从hue = 0到hue = 255的渐变。

最佳答案

我不认为这是Libgdx内置的,除非自上次检查以来已添加了它。我为此做了一个实用方法。它将色相,饱和度和值视为0到1之间的值。(因此,如果您使用的是比例尺,请预先除以255)。对于色调,它从红色到黄色到绿色到青色到蓝色到洋红色再回到红色。或者,也许方向相反,忘记了。我改编自Android颜色类的算法。

public static Color setColor (Color target, float hue, float saturation, float value){
        saturation = MathUtils.clamp(saturation, 0.0f, 1.0f);
        while (hue < 0) hue++;
        while (hue >= 1) hue--;
        value = MathUtils.clamp(value, 0.0f, 1.0f);

        float red = 0.0f;
        float green = 0.0f;
        float blue = 0.0f;

        final float hf = (hue - (int) hue) * 6.0f;
        final int ihf = (int) hf;
        final float f = hf - ihf;
        final float pv = value * (1.0f - saturation);
        final float qv = value * (1.0f - saturation * f);
        final float tv = value * (1.0f - saturation * (1.0f - f));

        switch (ihf) {
            case 0:         // Red is the dominant color
                red = value;
                green = tv;
                blue = pv;
                break;
            case 1:         // Green is the dominant color
                red = qv;
                green = value;
                blue = pv;
                break;
            case 2:
                red = pv;
                green = value;
                blue = tv;
                break;
            case 3:         // Blue is the dominant color
                red = pv;
                green = qv;
                blue = value;
                break;
            case 4:
                red = tv;
                green = pv;
                blue = value;
                break;
            case 5:         // Red is the dominant color
                red = value;
                green = pv;
                blue = qv;
                break;
        }

        return target.set(red, green, blue, target.a);
    }

09-11 21:55