2010-01-02 78 views
0

有沒有辦法將colorTransform應用於圓形而不是矩形中的BitmapData?圓形顏色轉換

而不是像下面的代碼那樣通過減少alpha通道來擦除圖像的矩形部分,我想用圓圈來完成。

_bitmap.colorTransform(new Rectangle(mouseX-d/2, mouseY-d/2, d, d), 
new ColorTransform(1, 1, 1, .5, 0, 0, 0, 1)); 

我確實有一些代碼,通過像素循環,提取α值,並使用setPixel但接縫比的ColorTransform功能顯著慢。

+0

by setPixel你正在使用BitmapData的權利? – chakrit 2010-01-02 16:16:14

+0

是的,在使用alpha通道時也需要使用setPixel32。 – 2010-01-02 17:39:26

回答

1

嘗試使用繪圖API(flash.display.Graphics)創建一個圓,然後使用BlendMode.ERASE將其繪製到位圖數據上。如果我理解正確,那可能會解決您的問題。

var circle : Shape = new Shape; 
circle.graphics.beginFill(0xffcc00, 1); 
circle.graphics.drawEllipse(-50, -50, 100, 100); 

// Create a transformation matrix for the draw() operation, with 
// a translation matching the mouse position. 
var mtx : Matrix = new Matrix(); 
mtx.translate(mouseX, mouseY); 

// Draw circle at mouse position with the ERASE blend mode, to 
// set affected pixels to alpha=0. 
myBitmap.draw(circle, mtx, null, BlendMode.ERASE); 

我不是100%肯定的ERASE混合模式與平局()命令的工作令人滿意,但我不明白爲什麼它不應該。請讓我知道它是如何運作的!

+0

工程魅力!我做的唯一更改是將圓的填充設置爲alpha .5或更低,以獲得與我使用的colorTransform相同的效果。這是爲了更慢地擦除圖像。非常感謝。 – 2010-01-02 17:12:52

+0

另一種方法是對draw方法_bitmap.draw(circle,mtx,new ColorTransform(1,1,1,.5),0,0,0,1)BlendMode使用3rd,ColorTransform參數。 ERASE) – 2010-01-02 17:37:59

+0

我很高興!關於使用ColorTransform只是爲了減少alpha,我的直覺告訴我,它不可能執行以及在圓圈填充上只有一個<1的alpha。簡單地說,因爲ColorTransform將在您每次繪製時應用(並在示例代碼中實例化),這意味着一次額外的操作。 – richardolsson 2010-01-02 20:11:25