2012-06-05 32 views
1

如何設置顏色BLACK:0x000000是透明的,通常魔術粉色是透明的,但我想將BLACK設置爲透明。如何在AS2中將導入圖像的顏色設置爲透明度

如果你不明白: http://j.imagehost.org/0829/WoodyGX_0.jpg

我有圖像,並轉換80×80的精靈,當我想要的背景是透明的,意思是:沒有背景,只有文字。

+0

如果它幫助你的過程中,考慮使用原非壓縮的BMP圖像。 'http://j.imagehost.org/download/0829/WoodyGX_0 http://j.imagehost.org/download/0829/WoodyGX_1 http:// j.imagehost.org/download/0829/WoodyGX_2' – arttronics

回答

1

注意:如果您決定遷移到ActionScript 3,但對於其他人和一般信息,這是ActionScript 3的答案。



可以從源BitmapData除去了黑像素(轉化成α-信道)創建新BitmapData

我創造了這個功能對你:

// Takes a source BitmapData and converts it to a new BitmapData, ignoring 
// dark pixels below the specified sensitivity. 
function removeDarkness(source:BitmapData, sensitivity:uint = 10000):BitmapData 
{ 
    // Define new BitmapData, with some size constraints to ensure the loop 
    // doesn't time out/crash. 
    // This is for demonstration only, consider creating a class that manages 
    // portions of the BitmapData at a time (up to say 50,000 iterations per 
    // frame) and then dispatches an event with the new BitmapData when done. 
    var fresh:BitmapData = new BitmapData(
     Math.min(600, source.width), 
     Math.min(400, source.height), 
     true, 0xFFFFFFFF 
    ); 

    fresh.lock(); 

    // Remove listed colors. 
    for(var v:int = 0; v < fresh.height; v++) 
    { 
     for(var h:int = 0; h < fresh.width; h++) 
     { 
      // Select relevant pixel for this iteration. 
      var pixel:uint = source.getPixel(h, v); 

      // Check against colors to remove. 
      if(pixel <= sensitivity) 
      { 
       // Match - delete pixel (fill with transparent pixel). 
       fresh.setPixel32(h, v, 0x00000000); 

       continue; 
      } 

      // No match, fill with expected color. 
      fresh.setPixel(h, v, pixel); 
     } 
    } 


    // We're done modifying the new BitmapData. 
    fresh.unlock(); 


    return fresh; 
} 

正如你所看到的,它需要:你要從中刪除較暗的像素

  • 的BitmapData。
  • uint表示要刪除多少個黑色/灰色陰影。

下面是使用源圖像演示:

var original:Loader = new Loader(); 
original.load(new URLRequest("http://j.imagehost.org/0829/WoodyGX_0.jpg")); 
original.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded); 


// Original image has loaded, continue. 
function imageLoaded(e:Event):void 
{ 
    // Capture pixels from loaded Bitmap. 
    var obmd:BitmapData = new BitmapData(original.width, original.height, false, 0); 
    obmd.draw(original); 


    // Create new BitmapData without black pixels. 
    var heroSheet:BitmapData = removeDarkness(obmd, 1200000); 
    addChild(new Bitmap(heroSheet)); 
} 
2

在這一點上,你可能會更好,只需將它帶入Fireworks,使用魔術棒選擇黑色像素,刪除它們,然後將其保存爲透明png。然後使用它。但是,如果你想讓自己的生活更加艱難,那麼你可以使用getPixel來獲取所有黑色像素,然後使用setPixel將它們設置爲透明。但是blitting的重點在於速度,而不是像素逐像素的操作。

相關問題