我正在开发有五种颜色的应用程序:红色、绿色、蓝色、黄色、紫色
我想从这些颜色中实现颜色混合:就像每种颜色有五个按钮一样。
用户触摸该颜色与先前绘制的颜色混合的任何颜色按钮。
我不知道如何添加两种颜色代码并获得第三种颜色。
编辑:
我还必须将此颜色设置为 imageview 的位图
我该如何设置?
自 2015 年 4 月起,您可以使用v4 支持库中的blendARGB 方法:
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;
}
请参阅ArgbEvaluator(自 API 11 起) http://developer.android.com/reference/android/animation/ArgbEvaluator.html
如果颜色在 RGB 空间中,则非常简单(但结果有时并不那么令人满意):
public 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);
}
如果您想使用其他颜色空间,请搜索 Wikipedia 并找到 HSL 颜色空间。你也有一些图书馆可以为你做这件事。
那么你将不得不阅读这个问题:计算RGB中的混合颜色
如果您想混合两种颜色(前景和背景),此示例可能很有用。基于 Orlando Leite answare 和维基百科 http://en.wikipedia.org/wiki/Alpha_compositing,将两种颜色与 alpha 混合的正确方法是:
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;
}
在这种情况下,Alpha 通道用于计算混合中 RGB 份额的百分比。只有当前景 alpha 小于 100% 时,才能看到背景颜色
在 Android 中,您可以使用Color
类来处理颜色。
使用此类,您可以访问颜色的红色、绿色和蓝色分量,然后您可以对它们执行操作并应用颜色算法。您可以通过以下方式从颜色 int 中提取颜色分量:
int color = Color.BLACK;
int red, green, blue;
red = Color.red(color);
green = Color.green(color);
blue = Color.blue(color);
每个值必须介于 0 和 255 之间,因此当您将两种颜色混合在一起时,您应该将该值除以 2,以确保最终结果在相同的区间内,或者应用另一种算法,记住每种颜色分量对于像素的亮度具有不同的权重。