如何在libgdx中指定具有色调,饱和度和亮度的颜色

时间:2015-12-23 18:38:01

标签: libgdx

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

2 个答案:

答案 0 :(得分:5)

我不认为这是内置于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);
    }

答案 1 :(得分:0)

这是我一直在使用的版本。所有值都基于 0...1 范围,因此您需要将色调除以 255f。这是 sam hocevar 为 glsl 提供的此 vec3 hsv2rgb(vec3 c) 方法的一个端口。

public static Color fromHSV(final float hue, final float saturation, final float value) {
    //vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    final float
        Kx=1f,
        Ky=2f/3f,
        Kz=1f/3f,
        Kw=3f;
    //vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    final float
        px=Math.abs(fract(hue+Kx)*6f-Kw),
        py=Math.abs(fract(hue+Ky)*6f-Kw),
        pz=Math.abs(fract(hue+Kz)*6f-Kw);
    //return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
    return new Color(
        value*mix(Kx,clamp(px-Kx,0f,1f),saturation),
        value*mix(Kx,clamp(py-Kx,0f,1f),saturation),
        value*mix(Kx,clamp(pz-Kz,0f,1f),saturation),
        1f
    );
}
public static float fract(final float x) {
    return x-(int)x;
}
public static float mix(final float x, final float y, final float a) {
    return x*(1f-a)+y*a;
}
public static float clamp(final float x, final float minVal, final float maxVal) {
    return Math.min(Math.max(x,minVal),maxVal);
}

您还可以使用 Color 重复使用 Color.set(float r, float g, float b, float a)

public static Color fromHSV(final Color target, final float hue, final float saturation, final float value) {
    //vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    final float
        Kx=1f,
        Ky=2f/3f,
        Kz=1f/3f,
        Kw=3f;
    //vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    final float
        px=Math.abs(fract(hue+Kx)*6f-Kw),
        py=Math.abs(fract(hue+Ky)*6f-Kw),
        pz=Math.abs(fract(hue+Kz)*6f-Kw);
    //return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
    return target.set(
        value*mix(Kx,clamp(px-Kx,0f,1f),saturation),
        value*mix(Kx,clamp(py-Kx,0f,1f),saturation),
        value*mix(Kx,clamp(pz-Kz,0f,1f),saturation),
        target.a
    );
}
public static float fract(final float x) {
    return x-(int)x;
}
public static float mix(final float x, final float y, final float a) {
    return x*(1f-a)+y*a;
}
public static float clamp(final float x, final float minVal, final float maxVal) {
    return Math.min(Math.max(x,minVal),maxVal);
}