如何在libGDX中指定具有色调、饱和度和亮度的颜色,而不是r、g、b、a值。我注意到Color构造函数只接受rgba,至少自动完成只提供rgba。这可能吗?我想创建一个从色调=0到色调255的渐变。
我不认为这是构建在Libgdx中的,除非它是在我上次检查后添加的。我为它做了一个实用的方法。它将色调、饱和度和值视为从0到1的值。(所以,如果你使用的是255,那么就用它来除数)。对于色调,它从红色到黄色,从绿色到青色,从蓝色到品红色,再回到红色。或者可能是相反的方向,忘记了。我从安卓的颜色类中调整了算法。
public static Color setColor (Color target, float hue, float saturation, float value){
saturation = MathUtils.clamp(saturation, 0.0f, 1.0f);
hue %= 1f;
if (hue < 0f) {
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);
}
这是我一直在使用的版本。所有值都基于0…1的范围,因此需要将色调除以255f。它是sam-hocevar为glsl提供的vec3-hsv2rgb(vec3c)方法的一个端口。
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.set(float r, float g, float b, float a)
重用Color
。
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);
}