我工作的应用程序中,我有五种颜色混合:红,绿,蓝,黄,紫颜色android系统
我想要实现颜色从这些颜色混合:例如,像有五个按钮每种颜色。
用户触摸此颜色与以前绘制的颜色混合的颜色按钮。
我还没有任何线索如何添加两个颜色代码并获得第三种颜色。
编辑:
我也必须设置该颜色的ImageView位
如何设置呢?
我工作的应用程序中,我有五种颜色混合:红,绿,蓝,黄,紫颜色android系统
我想要实现颜色从这些颜色混合:例如,像有五个按钮每种颜色。
用户触摸此颜色与以前绘制的颜色混合的颜色按钮。
我还没有任何线索如何添加两个颜色代码并获得第三种颜色。
编辑:
我也必须设置该颜色的ImageView位
如何设置呢?
另一种答案:
您可以将位在hexs混合:
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;
}
如果颜色是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);
}
如果你想使用其他颜色空间,搜索并找到HSL色彩空间。你也有一些图书馆为你做。
然后,你将要读这样的疑问:Calculation of a mixed color in RGB
thnx for reply bt我有5种颜色不仅rgb。 – 2011-05-20 10:07:32
RGB是每种颜色的组成部分。所以你有5倍的RGB。你需要结合Luis Miguel Serrano和我的答案。 – 2011-05-20 10:15:27
Thanx for reply.But我需要更多的色彩空间,使用上面的功能五个混合它将保持在恒定的颜色。什么应该更好地解决更多的色彩空间混合? – 2011-05-27 05:59:06
在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之间,所以当你混合两种颜色放在一起,您应该除以二的值,以确保最终结果在相同的间隔内,或者应用另一种算法,记住每个颜色分量对于像素的亮度具有不同权重的事实。
加点Color.red,Color.green,Color.blue&c。应该使用这些。虽然接受的答案(mixTwoColors)提供了一个完整的实现,可以使用Color.xxx方法稍微更时尚。 – 2014-10-21 04:49:10
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);
}
什么是“比率”? – HaydenKai 2017-08-09 07:38:56
见ArgbEvaluator(自API 11) http://developer.android.com/reference/android/animation/ArgbEvaluator.html
太好了,我用它将色彩与白色混合,使色彩充分亮丽。很棒。 – Ridcully 2014-12-26 10:13:56
Bizzre由android开发人员选择API。无需要求一个实例。不必要的将Object参数立即转换为Integer。由于装箱和拆箱分配,不应该用于实时绘图或动画代码。所以你知道。 (感谢原创海报;不用感谢编写这个超级大作的Android开发者)。只需在本文中刷一下其他代码片段即可。 – 2015-07-13 15:12:34
如果你想混合两种颜色(前景和背景),这个例子可能会有用。基于奥兰多雷特answare和维基百科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;
}
在这种情况下α通道被用于计算在混合百分比RGB份额。背景颜色可以是可见的只有我前景Alpha超过100%
最佳答案呢! – 2015-09-02 00:32:24
自2015年4月,你可以使用blendARGB method从V4支持库较小:
int resultColor = ColorUtils.blendARGB(color1, color2, 0.5F);
比率值必须是0.5才达到均匀混合。
也适用于设置颜色的较亮/较暗的色调! – mjp66 2018-02-22 10:19:44
第三个参数是比率(混合颜色时的比例)。例如。如果你想要color1和70%的color2的30%,那么做ColorUtils.blendARGB(***,***,0.3F); – 2018-03-11 00:37:59
'金额'是什么? – HaydenKai 2017-08-09 07:38:28
你想要的颜色混合到另一个颜色。例如,你需要0.3的color1和0.7的color2,你可以使用mixTwoColors(...,...,0.3)。 – 2017-08-10 18:35:39