Pre-multiplied Alphaの仕組み

by moyashipan
♥0 | Line 21 | Modified 2010-03-01 12:47:19 | MIT License
play

ActionScript3 source code

/**
 * Copyright moyashipan ( http://wonderfl.net/user/moyashipan )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/44lN
 */

package {
	import flash.text.TextField;
	import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.text.TextFormat;
    public class FlashTest extends Sprite {
        public function FlashTest() {
        		var textField:TextField = new TextField();
        		textField.height = 
        		textField.width = 465;
        		this.addChildAt(textField, 0);
        		var tf:TextFormat = new TextFormat("MS Gothic", 20);
        		textField.defaultTextFormat = tf;
        		
        		// 透過のBitmapDataを用意
			var bitmapData:BitmapData = new BitmapData(1, 1, true, 0);
			
			// Alphaに小さい値を設定する
			// A1, R255, G128, B127
			textField.text = "setPixel32: 0x" + 0x01ff807f.toString(16) + "\n";
			bitmapData.setPixel32(0, 0, 0x01ff807f);
			
			textField.appendText("↓\n");
			// Alphaが設定された時点で、
			// R,G,BはAlphaが乗算されてMath.round()されてる。
			// 	color = Math.round(color * (Alpha / 255));
			// つまり内部的には A1, R1, G1, B0
			// 元のRGBはどこにも保持されない。 
			
			// A1, R255, G255, B0
			textField.appendText("getPixel32: 0x" + bitmapData.getPixel32(0,0).toString(16));
			// getPixel32で得られるR,G,Bは
			// Alphaの逆数から求めているだけなのでこんな結果になる。
			// 	color = color * (255 / Alpha);
        }
    }
}