公式:
R*0.299 + G*0.587 + B*0.114
示例:
function toGray(alpha) { var imageData = canvas2dContext.getImageData(0, 0, width, height).data var r, g, b, v // 转灰度公式: R*0.299 + G*0.587 + B*0.114 for (var i = 0; i < imageData.length - 4; i += 4) { r = imageData[i] g = imageData[i + 1] b = imageData[i + 2] v = (r * 0.299 + g * 0.587 + b * 0.2973) imageData[i] = v // red imageData[i + 1] = v // green imageData[i + 2] = v // blue imageData[i + 3] = imageData[i + 3] * alpha // Alpha } }