RGB + Alpha = ARGB
RGB(0x000000~0xffffff) に アルファ(0~1) の値を足してARGB(0x00000000~0xffffffff) にする方法。
alpha * 0xff で 0~1 のアルファを 0x00~0xff の16進数に置き換える。 << 24 で左に24ビットシフトする。 + rgbで、右に空いた24ビットを 0x000000 ~ 0xffffff のrgb値で埋める。
- ActionScript
- ColorUtil.as
- Source
package {
public class ColorUtil {
public static function toARGB(rgb:uint, alpha:Number):uint {
return ((alpha * 0xff) << 24) + rgb;
}
public static function to16String(color:uint, length:int = 8):String {
var color16String:String = color.toString(16);
while (color16String.length < length) {
color16String = '0' + color16String;
}
return color16String;
}
}
}- package {
- public class ColorUtil {
- public static function toARGB(rgb:uint, alpha:Number):uint {
- return ((alpha * 0xff) << 24) + rgb;
- }
- public static function to16String(color:uint, length:int = 8):String {
- var color16String:String = color.toString(16);
- while (color16String.length < length) {
- color16String = '0' + color16String;
- }
- return color16String;
- }
- }
- }
こっちはサンプル用の実行クラス
- ActionScript
- TestClass_ColorUtil.as
- Source
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
[SWF(backgroundColor = 0xffffff, width = 600, height = 400, frameRate = 30)]
public class TestClass_ColorUtil extends Sprite {
public function TestClass_ColorUtil() {
var g:Graphics = graphics;
g.beginFill(0xffffff);
g.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
g.endFill();
initInstances(10);
initInstances(310);
}
private function initInstances(x:Number):void {
var textFormat:TextFormat = new TextFormat('_等幅', 11, 0x333333);
var i:int, rgbColor:uint, alpha:Number, argbColor:uint, bitmapData:BitmapData, bitmap:Bitmap, textField:TextField;
for (i = 0; i < 13; i ++) {
rgbColor = Math.random() * 0xffffff;
alpha = Math.round(Math.random() * 100) / 100;
argbColor = ColorUtil.toARGB(rgbColor, alpha);
bitmapData = new BitmapData(12, 12, true, 0x00000000);
bitmapData.floodFill(0, 0, ColorUtil.toARGB(rgbColor, 1));
bitmap = new Bitmap(bitmapData);
bitmap.x = x;
bitmap.y = 10 + 30 * i;
addChild(bitmap);
textField = new TextField();
textField.autoSize = TextFieldAutoSize.LEFT;
textField.defaultTextFormat = textFormat;
textField.text = '0x' + ColorUtil.to16String(rgbColor, 6) + ' + ' + alpha.toString() + ' = 0x' + ColorUtil.to16String(argbColor, 8);
textField.x = x + 20;
textField.y = bitmap.y;
addChild(textField);
bitmapData = new BitmapData(12, 12, true, 0x00000000);
bitmapData.floodFill(0, 0, argbColor);
bitmap = new Bitmap(bitmapData);
bitmap.x = x + 200;
bitmap.y = textField.y;
addChild(bitmap);
}
}
}
}- package {
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.Graphics;
- import flash.display.Sprite;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFormat;
- [SWF(backgroundColor = 0xffffff, width = 600, height = 400, frameRate = 30)]
- public class TestClass_ColorUtil extends Sprite {
- public function TestClass_ColorUtil() {
- var g:Graphics = graphics;
- g.beginFill(0xffffff);
- g.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
- g.endFill();
- initInstances(10);
- initInstances(310);
- }
- private function initInstances(x:Number):void {
- var textFormat:TextFormat = new TextFormat('_等幅', 11, 0x333333);
- var i:int, rgbColor:uint, alpha:Number, argbColor:uint, bitmapData:BitmapData, bitmap:Bitmap, textField:TextField;
- for (i = 0; i < 13; i ++) {
- rgbColor = Math.random() * 0xffffff;
- alpha = Math.round(Math.random() * 100) / 100;
- argbColor = ColorUtil.toARGB(rgbColor, alpha);
- bitmapData = new BitmapData(12, 12, true, 0x00000000);
- bitmapData.floodFill(0, 0, ColorUtil.toARGB(rgbColor, 1));
- bitmap = new Bitmap(bitmapData);
- bitmap.x = x;
- bitmap.y = 10 + 30 * i;
- addChild(bitmap);
- textField = new TextField();
- textField.autoSize = TextFieldAutoSize.LEFT;
- textField.defaultTextFormat = textFormat;
- textField.text = '0x' + ColorUtil.to16String(rgbColor, 6) + ' + ' + alpha.toString() + ' = 0x' + ColorUtil.to16String(argbColor, 8);
- textField.x = x + 20;
- textField.y = bitmap.y;
- addChild(textField);
- bitmapData = new BitmapData(12, 12, true, 0x00000000);
- bitmapData.floodFill(0, 0, argbColor);
- bitmap = new Bitmap(bitmapData);
- bitmap.x = x + 200;
- bitmap.y = textField.y;
- addChild(bitmap);
- }
- }
- }
- }
About this entry
You’re currently reading “RGB + Alpha = ARGB,” an entry on jp.ferv.blog
- Published:
- Sun, May 24th, 2009 at 2:20 PM
- Author:
- dsk
- Category:
- Web
- Tags:
- ActionScript 3.0
No comments
Jump to comment form | comments rss | trackback uri