Java android颜色介于两种颜色之间,基于百分比?
我想根据百分比值计算颜色:Java android颜色介于两种颜色之间,基于百分比?,java,android,colors,Java,Android,Colors,我想根据百分比值计算颜色: float percentage = x/total; int color; if (percentage >= 0.95) { color = Color.GREEN; } else if (percentage <= 0.5) { color = Color.RED; } else { // color = getColor(Color.Green, Color.RED, percentage); } 好吧,这是可行的,但是我希望在灰色背
float percentage = x/total;
int color;
if (percentage >= 0.95) {
color = Color.GREEN;
} else if (percentage <= 0.5) {
color = Color.RED;
} else {
// color = getColor(Color.Green, Color.RED, percentage);
}
好吧,这是可行的,但是我希望在灰色背景上使用50%左右的颜色时更亮一些。。我怎样才能做到这一点
谢谢
更新
我试着像评论中建议的那样转换成YUV。但我仍然有同样的问题,在50%时,它是黑暗的。
另外,在这个溶液中,我在转化为yuv、hsv等2小时后,atOk。。。我放弃了。我现在就这样做:
public class ColorUtils {
private static int FIRST_COLOR = Color.GREEN;
private static int SECOND_COLOR = Color.YELLOW;
private static int THIRD_COLOR = Color.RED;
public static int getColor(float p) {
int c0;
int c1;
if (p <= 0.5f) {
p *= 2;
c0 = FIRST_COLOR;
c1 = SECOND_COLOR;
} else {
p = (p - 0.5f) * 2;
c0 = SECOND_COLOR;
c1 = THIRD_COLOR;
}
int a = ave(Color.alpha(c0), Color.alpha(c1), p);
int r = ave(Color.red(c0), Color.red(c1), p);
int g = ave(Color.green(c0), Color.green(c1), p);
int b = ave(Color.blue(c0), Color.blue(c1), p);
return Color.argb(a, r, g, b);
}
private static int ave(int src, int dst, float p) {
return src + java.lang.Math.round(p * (dst - src));
}
}
公共类ColorUtils{
私有静态int FIRST_COLOR=COLOR.GREEN;
私有静态int SECOND_COLOR=COLOR.YELLOW;
私有静态int第三种颜色=COLOR.RED;
公共静态int-getColor(浮点p){
int c0;
int c1;
if(p这里是一个伪代码函数,它在两种颜色之间进行线性插值(停留在RGB空间中)。为了清晰起见,我在这里使用了一个名为Color的类,而不是int
bAmount介于0和1之间(用于插值)
这样的插值最好在颜色空间(而不是YUV)中完成
中档颜色看起来“浑浊”的原因是,如果你简单地线性增加红色(#ff0000
),同时减少绿色(#00ff00
),那么中档颜色最终会变成#808000
,而不是#ff00
相反,找到起始颜色的HSL(或HSV)等价物,以及结束颜色的HSL(或HSV)等价物。在该颜色空间中插值,然后对每个点再次转换回RGB
由于完全饱和的红色和绿色的S
和L
(或V
)值相同,因此只有H
(色调)变量会改变,从而产生光谱的适当效果。我的$0.02,我找到了这个答案并编写了适当的解决方案。(感谢Alnitak的HSV提示!)
复制+粘贴:
private float interpolate(float a, float b, float proportion) {
return (a + ((b - a) * proportion));
}
/** Returns an interpoloated color, between <code>a</code> and <code>b</code> */
private int interpolateColor(int a, int b, float proportion) {
float[] hsva = new float[3];
float[] hsvb = new float[3];
Color.colorToHSV(a, hsva);
Color.colorToHSV(b, hsvb);
for (int i = 0; i < 3; i++) {
hsvb[i] = interpolate(hsva[i], hsvb[i], proportion);
}
return Color.HSVToColor(hsvb);
}
您可以尝试使用android API中的ArgbeEvaluator类:
请注意,alpha通道计算中有一个bug(),因此您应该使用没有alpha值的值。这里有两种插值方法:
private static float interpolate(final float a, final float b, final float proportion) {
return a + (b - a) * proportion;
}
/** Returns an interpoloated color, between <code>a</code> and <code>b</code> */
public static int interpolateColorHsv(final int a, final int b, final float proportion) {
final float[] hsva = new float[3];
final float[] hsvb = new float[3];
Color.colorToHSV(a, hsva);
Color.colorToHSV(b, hsvb);
for (int i = 0; i < 3; ++i) {
hsvb[i] = interpolate(hsva[i], hsvb[i], proportion);
}
return Color.HSVToColor(hsvb);
}
public static int interpolateRGB(final int colorA, final int colorB, final float bAmount) {
final float aAmount = 1.0f - bAmount;
final int red = (int) (Color.red(colorA) * aAmount + Color.red(colorB) * bAmount);
final int green = (int) (Color.green(colorA) * aAmount + Color.green(colorB) * bAmount);
final int blue = (int) (Color.blue(colorA) * aAmount + Color.blue(colorB) * bAmount);
return Color.rgb(red, green, blue);
}
我只是想更新以处理alpha通道:
private float interpolate(float a, float b, float proportion) {
return (a + ((b - a) * proportion));
}
/**
* Returns an interpolated color, between <code>a</code> and <code>b</code>
* proportion = 0, results in color a
* proportion = 1, results in color b
*/
private int interpolateColor(int a, int b, float proportion) {
if (proportion > 1 || proportion < 0) {
throw new IllegalArgumentException("proportion must be [0 - 1]");
}
float[] hsva = new float[3];
float[] hsvb = new float[3];
float[] hsv_output = new float[3];
Color.colorToHSV(a, hsva);
Color.colorToHSV(b, hsvb);
for (int i = 0; i < 3; i++) {
hsv_output[i] = interpolate(hsva[i], hsvb[i], proportion);
}
int alpha_a = Color.alpha(a);
int alpha_b = Color.alpha(b);
float alpha_output = interpolate(alpha_a, alpha_b, proportion);
return Color.HSVToColor((int) alpha_output, hsv_output);
}
这个线程的一些解决方案对我不起作用,所以我创建了另一个解决方案。也许它对某人有用
/**
* Get the color between two given colors
* @param colorStart int color start of degradate
* @param colorEnd int color end of degradate
* @param percent int percent to apply (0 to 100)
* @return int color of degradate for given percent
*/
public static int getColorOfDegradate(int colorStart, int colorEnd, int percent){
return Color.rgb(
getColorOfDegradateCalculation(Color.red(colorStart), Color.red(colorEnd), percent),
getColorOfDegradateCalculation(Color.green(colorStart), Color.green(colorEnd), percent),
getColorOfDegradateCalculation(Color.blue(colorStart), Color.blue(colorEnd), percent)
);
}
private static int getColorOfDegradateCalculation(int colorStart, int colorEnd, int percent){
return ((Math.min(colorStart, colorEnd)*(100-percent)) + (Math.max(colorStart, colorEnd)*percent)) / 100;
}
作为一个更新的解决方案,您可以从或API使用ColorUtils#blendARGB
:
您是否考虑过转换为YUV并比较亮度值?是的,我将其编辑为问题…这与此答案+1的问题相同。将结果转换为int:int color=(整数)new argbeevaluator()。评估(0.75,0x00ff00,0xff0000)
注意,该类需要API级别11+!若要为此使用颜色资源,请使用ContextCompat.getColor(context,R.color.my_color)
而不仅仅是R.color.my_color
intI我想知道为什么他们让argbeevaluator处理对象而不是int。动画过程中如此多的强制转换和包络都是不必要的开销。导入android.animation.argbeevaluator;应该注意,这不会处理alpha通道。从color.colorToHSV
文档:“颜色-要转换的argb颜色。忽略alpha组件。”我更新了您的答案以处理alpha:几乎,当在蓝色和红色之间插值时,我得到黄色,但想要粉色。太棒了!工作非常完美
new ArgbEvaluator().evaluate(0.75, 0x00ff00, 0xff0000);
private static float interpolate(final float a, final float b, final float proportion) {
return a + (b - a) * proportion;
}
/** Returns an interpoloated color, between <code>a</code> and <code>b</code> */
public static int interpolateColorHsv(final int a, final int b, final float proportion) {
final float[] hsva = new float[3];
final float[] hsvb = new float[3];
Color.colorToHSV(a, hsva);
Color.colorToHSV(b, hsvb);
for (int i = 0; i < 3; ++i) {
hsvb[i] = interpolate(hsva[i], hsvb[i], proportion);
}
return Color.HSVToColor(hsvb);
}
public static int interpolateRGB(final int colorA, final int colorB, final float bAmount) {
final float aAmount = 1.0f - bAmount;
final int red = (int) (Color.red(colorA) * aAmount + Color.red(colorB) * bAmount);
final int green = (int) (Color.green(colorA) * aAmount + Color.green(colorB) * bAmount);
final int blue = (int) (Color.blue(colorA) * aAmount + Color.blue(colorB) * bAmount);
return Color.rgb(red, green, blue);
}
private float interpolate(float a, float b, float proportion) {
return (a + ((b - a) * proportion));
}
/**
* Returns an interpolated color, between <code>a</code> and <code>b</code>
* proportion = 0, results in color a
* proportion = 1, results in color b
*/
private int interpolateColor(int a, int b, float proportion) {
if (proportion > 1 || proportion < 0) {
throw new IllegalArgumentException("proportion must be [0 - 1]");
}
float[] hsva = new float[3];
float[] hsvb = new float[3];
float[] hsv_output = new float[3];
Color.colorToHSV(a, hsva);
Color.colorToHSV(b, hsvb);
for (int i = 0; i < 3; i++) {
hsv_output[i] = interpolate(hsva[i], hsvb[i], proportion);
}
int alpha_a = Color.alpha(a);
int alpha_b = Color.alpha(b);
float alpha_output = interpolate(alpha_a, alpha_b, proportion);
return Color.HSVToColor((int) alpha_output, hsv_output);
}
/**
* Get the color between two given colors
* @param colorStart int color start of degradate
* @param colorEnd int color end of degradate
* @param percent int percent to apply (0 to 100)
* @return int color of degradate for given percent
*/
public static int getColorOfDegradate(int colorStart, int colorEnd, int percent){
return Color.rgb(
getColorOfDegradateCalculation(Color.red(colorStart), Color.red(colorEnd), percent),
getColorOfDegradateCalculation(Color.green(colorStart), Color.green(colorEnd), percent),
getColorOfDegradateCalculation(Color.blue(colorStart), Color.blue(colorEnd), percent)
);
}
private static int getColorOfDegradateCalculation(int colorStart, int colorEnd, int percent){
return ((Math.min(colorStart, colorEnd)*(100-percent)) + (Math.max(colorStart, colorEnd)*percent)) / 100;
}
val startColor = ContextCompat.getColor(context, R.color.white)
val endColor = ContextCompat.getColor(context, R.color.yellow)
ColorUtils.blendARGB(startColor, endColor, 0.75)