2013-01-10 141 views
1

我使用下面的代碼來獲取顏色,將其更改爲灰度並保存。問題在於它總是藍色的,而不是灰色的。如何獲取顏色的灰度值?

newBitmapData = new BitmapData(bitmapWidth, bitmapHeight, true); 

// Extract individual red, green, and blue values for the pixel: 
rgbVal = bitmapData.getPixel(x, y); 

redVal = (rgbVal & 0xFF0000) >> 16; 
greenVal = (rgbVal & 0x00FF00) >> 8; 
blueVal = rgbVal & 0x0000FF; 


grayVal = Math.floor(0.3 * redVal + 0.59 * greenVal + 0.11 * blueVal); 

newBitmapData.setPixel(x, y, grayVal); 

我錯過了什麼?

回答

4

你「grayVal」只是它的亮度,使色彩出其亮度的事:

newBitmapData.setPixel(x, y, Math.min(255,uint(grayVal))*0x10101); 
0

你沒有轉移的紅色和綠色回來:

redVal = (rgbVal & 0xFF0000) >> 16; 
greenVal = (rgbVal & 0x00FF00) >> 8; 
blueVal = rgbVal & 0x0000FF; 

//shift red and green back to original positions 
redVal = int(0.3*redVal)<<16; 
greenVal = int(0.59*greenVal)<<8; 

grayVal = Math.floor(redVal + greenVal + 0.11*blueVal);