我工作的應用程序中,我有五種顏色混合:紅,綠,藍,黃,紫顏色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