[AS3.0] 数値・演算子アラカルト (4)

そうだ! AS3.0の数値・演算子まわりを学習しよう! :bouzu:

黒魔術じゃないよ! :boy:

:caution: 要 Flash Player 9 以上


ビット単位論理積演算子 + ビット単位シフト演算子(右シフト)

This movie requires Flash Player 9.0.0
This movie requires Flash Player 9.0.0
// RGB を R, G, B に
var color:uint = 0x3366CC;
var result:String = transformColorToRGB(color);

var rgb:Object = calc(color);
var result1:String = "{r: " + rgb.r + ", g: " + rgb.g + ", b: " + rgb.b + "}";
var result2:String = "{R: " + rgb.R + ", G: " + rgb.G + ", B: " + rgb.B + "}";

function calc(color:uint):Object {
  var r:uint = (color >> 16) & 0xFF;
  var g:uint = (color >> 8) & 0xFF;
  var b:uint = color & 0xFF;
  var R:String = ("00" + r.toString(16)).substr(-2).toUpperCase();
  var G:String = ("00" + g.toString(16)).substr(-2).toUpperCase();
  var B:String = ("00" + b.toString(16)).substr(-2).toUpperCase();
  return {r: r, g: g, b: b, R: "0x" + R, G: "0x" + G, B: "0x" + B};
}
function transformColorToRGB(color:uint):String {
  var RGB:String = ("00000" + color.toString(16)).substr(-6).toUpperCase();
  return "0x" + RGB;
}

// ARGB を A, R, G, B に
var color:uint = 0x803366CC;
var result:String = transformColorToRGB(color);

var argb:Object = calc(color);
var result1:String = "{a: " + argb.a + ", r: " + argb.r + ", g: " + argb.g + ", b: " + argb.b + "}";
var result2:String = "{A: " + argb.A + ", R: " + argb.R + ", G: " + argb.G + ", B: " + argb.B + "}";

function calc(color:uint):Object {
  var a:uint = (color >> 24) & 0xFF;
  var r:uint = (color >> 16) & 0xFF;
  var g:uint = (color >> 8) & 0xFF;
  var b:uint = color & 0xFF;
  var A:String = ("00" + a.toString(16)).substr(-2).toUpperCase();
  var R:String = ("00" + r.toString(16)).substr(-2).toUpperCase();
  var G:String = ("00" + g.toString(16)).substr(-2).toUpperCase();
  var B:String = ("00" + b.toString(16)).substr(-2).toUpperCase();
  return {a: a, r: r, g: g, b: b, A: "0x" + A, R: "0x" + R, G: "0x" + G, B: "0x" + B};
}
function transformColorToARGB(color:uint):String {
  var ARGB:String = ("0000000" + color.toString(16)).substr(-8).toUpperCase();
  return "0x" + ARGB;
}


ビット単位論理和演算子 + ビット単位シフト演算子(左シフト)

This movie requires Flash Player 9.0.0
This movie requires Flash Player 9.0.0
// R, G, B を RGB に
var rgb:Object = {R: 0x33, G: 0x66, B: 0xCC};

var color:uint = calc(rgb);
var result1:uint = color;
var result2:String = transformColorToRGB(color);

function calc(rgb:Object):uint {
  return (rgb.R << 16) | (rgb.G << 8) | rgb.B;
}
function transformColorToRGB(color:uint):String {
  var RGB:String = ("00000" + color.toString(16)).substr(-6).toUpperCase();
  return "0x" + RGB;
}

// A, R, G, B を ARGB に
var argb:Object = {A: 0x80, R: 0x33, G: 0x66, B: 0xCC};

var color:uint = calc(argb);
var result1:uint = color;
var result2:String = transformColorToARGB(color);

function calc(argb:Object):uint {
  return (argb.A << 24) | argb.R << 16) | (argb.G << 8) | argb.B;
}
function transformColorToARGB(color:uint):String {
  var ARGB:String = ("0000000" + color.toString(16)).substr(-8).toUpperCase();
  return "0x" + ARGB;
}