我正在开发一款应用程序,其中有五种颜色:红色、绿色、蓝色、黄色和紫色。
我想要实现这些颜色的混合:就像有五个按钮分别代表每种颜色。
用户点击任何一个颜色按钮,它将与先前绘制的颜色混合。
我不知道如何将两种颜色代码相加并得到第三种颜色。
编辑:
我还需要将这种颜色设置为ImageView的位图。
如何进行设置呢?
int resultColor = ColorUtils.blendARGB(color1, color2, 0.5F);
比例值必须为0.5才能实现均匀混合。SlidingTabStrip有一个非常有用的方法可以混合颜色,在与ViewPager一起使用时效果很棒:
private static int blendColors(int color1, int color2, float ratio) {
final float inverseRation = 1f - ratio;
float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
return Color.rgb((int) r, (int) g, (int) b);
}
另一种答案:
您可以将十六进制中的比特混合:
public static int mixTwoColors( int color1, int color2, float amount )
{
final byte ALPHA_CHANNEL = 24;
final byte RED_CHANNEL = 16;
final byte GREEN_CHANNEL = 8;
final byte BLUE_CHANNEL = 0;
final float inverseAmount = 1.0f - amount;
int a = ((int)(((float)(color1 >> ALPHA_CHANNEL & 0xff )*amount) +
((float)(color2 >> ALPHA_CHANNEL & 0xff )*inverseAmount))) & 0xff;
int r = ((int)(((float)(color1 >> RED_CHANNEL & 0xff )*amount) +
((float)(color2 >> RED_CHANNEL & 0xff )*inverseAmount))) & 0xff;
int g = ((int)(((float)(color1 >> GREEN_CHANNEL & 0xff )*amount) +
((float)(color2 >> GREEN_CHANNEL & 0xff )*inverseAmount))) & 0xff;
int b = ((int)(((float)(color1 & 0xff )*amount) +
((float)(color2 & 0xff )*inverseAmount))) & 0xff;
return a << ALPHA_CHANNEL | r << RED_CHANNEL | g << GREEN_CHANNEL | b << BLUE_CHANNEL;
}
amount
是什么? - HaydenKaipublic int mixColors(int col1, int col2) {
int r1, g1, b1, r2, g2, b2;
r1 = Color.red(col1);
g1 = Color.green(col1);
b1 = Color.blue(col1);
r2 = Color.red(col2);
g2 = Color.green(col2);
b2 = Color.blue(col2);
int r3 = (r1 + r2)/2;
int g3 = (g1 + g2)/2;
int b3 = (b1 + b2)/2;
return Color.rgb(r3, g3, b3);
}
如果您想使用其他颜色空间,请搜索维基百科并找到HSL颜色空间。您还可以使用一些库来帮助您完成此操作。
然后您需要阅读这个问题:计算RGB混合颜色
请查看ArgbEvaluator (自API 11起) http://developer.android.com/reference/android/animation/ArgbEvaluator.html
如果您希望混合两种颜色(前景色和背景色),则此示例可能有用。基于Orlando Leite的回答和维基百科http://en.wikipedia.org/wiki/Alpha_compositing,使用透明度混合两种颜色的正确方式是:
public static int MergeColors(int backgroundColor, int foregroundColor) {
final byte ALPHA_CHANNEL = 24;
final byte RED_CHANNEL = 16;
final byte GREEN_CHANNEL = 8;
final byte BLUE_CHANNEL = 0;
final double ap1 = (double)(backgroundColor >> ALPHA_CHANNEL & 0xff) / 255d;
final double ap2 = (double)(foregroundColor >> ALPHA_CHANNEL & 0xff) / 255d;
final double ap = ap2 + (ap1 * (1 - ap2));
final double amount1 = (ap1 * (1 - ap2)) / ap;
final double amount2 = amount1 / ap;
int a = ((int)(ap * 255d)) & 0xff;
int r = ((int)(((float)(backgroundColor >> RED_CHANNEL & 0xff )*amount1) +
((float)(foregroundColor >> RED_CHANNEL & 0xff )*amount2))) & 0xff;
int g = ((int)(((float)(backgroundColor >> GREEN_CHANNEL & 0xff )*amount1) +
((float)(foregroundColor >> GREEN_CHANNEL & 0xff )*amount2))) & 0xff;
int b = ((int)(((float)(backgroundColor & 0xff )*amount1) +
((float)(foregroundColor & 0xff )*amount2))) & 0xff;
return a << ALPHA_CHANNEL | r << RED_CHANNEL | g << GREEN_CHANNEL | b << BLUE_CHANNEL;
}
Color
类处理颜色。int color = Color.BLACK;
int red, green, blue;
red = Color.red(color);
green = Color.green(color);
blue = Color.blue(color);
final double amount2 = ap2 / ap;
因此,整个代码将如下所示:
public static int MergeColors(int backgroundColor, int foregroundColor) {
final byte ALPHA_CHANNEL = 24;
final byte RED_CHANNEL = 16;
final byte GREEN_CHANNEL = 8;
final byte BLUE_CHANNEL = 0;
final double ap1 = (double)(backgroundColor >> ALPHA_CHANNEL & 0xff) / 255d;
final double ap2 = (double)(foregroundColor >> ALPHA_CHANNEL & 0xff) / 255d;
final double ap = ap2 + (ap1 * (1 - ap2));
final double amount1 = (ap1 * (1 - ap2)) / ap;
final double amount2 = ap2 / ap;
int a = ((int)(ap * 255d)) & 0xff;
int r = ((int)(((float)(backgroundColor >> RED_CHANNEL & 0xff )*amount1) +
((float)(foregroundColor >> RED_CHANNEL & 0xff )*amount2))) & 0xff;
int g = ((int)(((float)(backgroundColor >> GREEN_CHANNEL & 0xff )*amount1) +
((float)(foregroundColor >> GREEN_CHANNEL & 0xff )*amount2))) & 0xff;
int b = ((int)(((float)(backgroundColor & 0xff )*amount1) +
((float)(foregroundColor & 0xff )*amount2))) & 0xff;
return a << ALPHA_CHANNEL | r << RED_CHANNEL | g << GREEN_CHANNEL | b << BLUE_CHANNEL;
}