這裏是我的功能,希望它幫助。
function averageColors(colorArray){
var red = 0, green = 0, blue = 0;
for (var i = 0; i < colorArray.length; i++){
red += hexToR("" + colorArray[ i ] + "");
green += hexToG("" + colorArray[ i ] + "");
blue += hexToB("" + colorArray[ i ] + "");
}
//Average RGB
red = (red/colorArray.length);
green = (green/colorArray.length);
blue = (blue/colorArray.length);
console.log(red + ", " + green + ", " + blue);
return new THREE.Color("rgb("+ red +","+ green +","+ blue +")");
}
//get the red of RGB from a hex value
function hexToR(h) {return parseInt((cutHex(h)).substring(0, 2), 16)}
//get the green of RGB from a hex value
function hexToG(h) {return parseInt((cutHex(h)).substring(2, 4), 16)}
//get the blue of RGB from a hex value
function hexToB(h) {return parseInt((cutHex(h)).substring(4, 6), 16)}
//cut the hex into pieces
function cutHex(h) {if(h.charAt(1) == "x"){return h.substring(2, 8);} else {return h.substring(1,7);}}
什麼是你定義的「平均」?根據你工作的色彩空間等等,這有多種可能的含義。 – Amber 2011-06-16 04:11:58
@Amber,我更新了這個問題,以澄清 – samccone 2011-06-16 04:15:18
*嗅*關於這個...的作業的明顯差異 – 2011-06-16 04:17:28