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値で埋める。

TestClass_ColorUtil.swf

  • 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;
		}


	}

}
  1. package {
  2.  
  3.     public class ColorUtil {
  4.  
  5.         public static function toARGB(rgb:uint, alpha:Number):uint {
  6.             return ((alpha * 0xff) << 24) + rgb;
  7.         }
  8.  
  9.         public static function to16String(color:uint, length:int = 8):String {
  10.             var color16String:String = color.toString(16);
  11.             while (color16String.length < length) {
  12.                 color16String = '0' + color16String;
  13.             }
  14.             return color16String;
  15.         }
  16.  
  17.  
  18.     }
  19.  
  20. }

こっちはサンプル用の実行クラス

  • 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);
			}
		}


	}

}
  1. package {
  2.     import flash.display.Bitmap;
  3.     import flash.display.BitmapData;
  4.     import flash.display.Graphics;
  5.     import flash.display.Sprite;
  6.     import flash.text.TextField;
  7.     import flash.text.TextFieldAutoSize;
  8.     import flash.text.TextFormat;
  9.  
  10.     [SWF(backgroundColor = 0xffffff, width = 600, height = 400, frameRate = 30)]
  11.     public class TestClass_ColorUtil extends Sprite {
  12.  
  13.         public function TestClass_ColorUtil() {
  14.             var g:Graphics = graphics;
  15.             g.beginFill(0xffffff);
  16.             g.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
  17.             g.endFill();
  18.  
  19.             initInstances(10);
  20.             initInstances(310);
  21.         }
  22.  
  23.         private function initInstances(x:Number):void {
  24.             var textFormat:TextFormat = new TextFormat('_等幅', 11, 0x333333);
  25.  
  26.             var i:int, rgbColor:uint, alpha:Number, argbColor:uint, bitmapData:BitmapData, bitmap:Bitmap, textField:TextField;
  27.             for (i = 0; i < 13; i ++) {
  28.                 rgbColor = Math.random() * 0xffffff;
  29.                 alpha = Math.round(Math.random() * 100) / 100;
  30.                 argbColor = ColorUtil.toARGB(rgbColor, alpha);
  31.  
  32.                 bitmapData = new BitmapData(12, 12, true, 0x00000000);
  33.                 bitmapData.floodFill(0, 0, ColorUtil.toARGB(rgbColor, 1));
  34.                 bitmap = new Bitmap(bitmapData);
  35.                 bitmap.x = x;
  36.                 bitmap.y = 10 + 30 * i;
  37.                 addChild(bitmap);
  38.  
  39.                 textField = new TextField();
  40.                 textField.autoSize = TextFieldAutoSize.LEFT;
  41.                 textField.defaultTextFormat = textFormat;
  42.                 textField.text = '0x' + ColorUtil.to16String(rgbColor, 6) + ' + ' + alpha.toString() + ' = 0x' + ColorUtil.to16String(argbColor, 8);
  43.                 textField.x = x + 20;
  44.                 textField.y = bitmap.y;
  45.                 addChild(textField);
  46.  
  47.                 bitmapData = new BitmapData(12, 12, true, 0x00000000);
  48.                 bitmapData.floodFill(0, 0, argbColor);
  49.                 bitmap = new Bitmap(bitmapData);
  50.                 bitmap.x = x + 200;
  51.                 bitmap.y = textField.y;
  52.                 addChild(bitmap);
  53.             }
  54.         }
  55.  
  56.  
  57.     }
  58.  
  59. }


About this entry