Skip to content

Instantly share code, notes, and snippets.

@Creta5164
Last active September 25, 2017 00:19
Show Gist options
  • Save Creta5164/4674a11193ab703beb2728bd463c90cb to your computer and use it in GitHub Desktop.
Save Creta5164/4674a11193ab703beb2728bd463c90cb to your computer and use it in GitHub Desktop.
AS3 Color friendly Vector4
/////////////////////////////////////////////////////////////////////////
// Vector4 by Creta (creta5164@gmail.com)
/////////////////////////////////////////////////////////////////////////
package
{
public class Vector4
{
public var x:Number, y:Number, z:Number, w:Number;
///<summary>
///x y z를 각각 r g b 색상으로 치환된 값을 가진 정수입니다.
///</summary>
public function get color():uint {
return int(x * 255) << 16 | int(y * 255) << 8 | int(z * 255);
}
///<summary>
///x y z를 각각 r g b 색상으로 치환된 값을 가진 정수입니다.
///</summary>
public function set color(value:uint):void {
w = 1;
x = ((value >> 16) & 0xFF) / 255;
y = ((value >> 8 ) & 0xFF) / 255;
z = (value & 0xFF) / 255;
}
///<summary>
///x y z를 각각 r g b 색상으로 치환된 값을 가진 colorHex입니다.
///</summary>
public function get scolor():String {
var s:String = color.toString(16);
while (s.length < 6) s = "0" + s;
return "0x" + s;
}
///<summary>
///x y z w를 각각 a r g b 색상으로 치환된 값을 가진 정수입니다.
///</summary>
public function get color32():uint {
return int(w * 255) << 24 | int(x * 255) << 16 | int(y * 255) << 8 | int(z * 255);
}
///<summary>
///x y z w를 각각 a r g b 색상으로 치환된 값을 가진 정수입니다.
///</summary>
public function set color32(value:uint):void {
w = ((value >> 24) & 0xFF) / 255;
x = ((value >> 16) & 0xFF) / 255;
y = ((value >> 8 ) & 0xFF) / 255;
z = (value & 0xFF) / 255;
}
///<summary>
///x y z w를 각각 a r g b 색상으로 치환된 값을 가진 colorHex입니다.
///</summary>
public function get scolor32():String {
var s:String = color.toString(16);
while (s.length < 8) s = "0" + s;
return "0x" + s;
}
///<summary>
///x y z w를 가진 객체를 인스턴스화 합니다.
///</summary>
public function Vector4(x:Number = 0, y:Number = 0,
z:Number = 0, w:Number = 0):void
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
///<summary>
///다른 Vector4 객체로부터 x y z w를 가져옵니다.
///<para>액션스크립트는 연산자 오버로딩이 지원되지 않기 때문에 메서드를 따로 구현합니다.</para>
///</summary>
public function Cast(a:Vector4):Vector4
{
this.x = a.x;
this.y = a.y;
this.z = a.z;
this.w = a.w;
return this;
}
///<summary>
///다른 Vector4 객체로부터 x y z w를 더합니다.
///<para>액션스크립트는 연산자 오버로딩이 지원되지 않기 때문에 메서드를 따로 구현합니다.</para>
///</summary>
public function Add(a:Vector4):Vector4
{
this.x += a.x;
this.y += a.y;
this.z += a.z;
this.w += a.w;
return this;
}
///<summary>
///다른 Vector4 객체로부터 x y z w를 뺍니다.
///<para>액션스크립트는 연산자 오버로딩이 지원되지 않기 때문에 메서드를 따로 구현합니다.</para>
///</summary>
public function Subtract(a:Vector4):Vector4
{
this.x -= a.x;
this.y -= a.y;
this.z -= a.z;
this.w -= a.w;
return this;
}
///<summary>
///다른 Vector4 객체로부터 x y z w를 곱합니다.
///<para>액션스크립트는 연산자 오버로딩이 지원되지 않기 때문에 메서드를 따로 구현합니다.</para>
///</summary>
public function Multiply(a:Vector4):Vector4
{
this.x *= a.x;
this.y *= a.y;
this.z *= a.z;
this.w *= a.w;
return this;
}
///<summary>
///다른 Vector4 객체로부터 x y z w를 나눕니다.
///<para>액션스크립트는 연산자 오버로딩이 지원되지 않기 때문에 메서드를 따로 구현합니다.</para>
///</summary>
public function Division(a:Vector4):Vector4
{
this.x /= a.x;
this.y /= a.y;
this.z /= a.z;
this.w /= a.w;
return this;
}
///<summary>
///다른 Vector4 객체로부터 x y z w를 이용해 나머지를 구합니다.
///<para>액션스크립트는 연산자 오버로딩이 지원되지 않기 때문에 메서드를 따로 구현합니다.</para>
///</summary>
public function Remainder(a:Vector4):Vector4
{
this.x %= a.x;
this.y %= a.y;
this.z %= a.z;
this.w %= a.w;
return this;
}
///<summary>
///다른 두 Vector4 객체로부터 보간한 값을 적용합니다.
///</summary>
public function doLerp(a:Vector4, b:Vector4, pp:Number = 0):Vector4
{
var np:Number = (1-pp);
this.x = np*a.x+pp*b.x;
this.y = np*a.y+pp*b.y;
this.z = np*a.z+pp*b.z;
this.w = np*a.w+pp*b.w;
return this;
}
///<summary>
///다른 두 Vector4 객체로부터 보간한 값을 가진 Vector4 객체를 인스턴스화합니다.
///</summary>
public static function Lerp(a:Vector4, b:Vector4, pp:Number = 0):Vector4
{
var np:Number = (1-pp);
return new Vector4(np*a.x+pp*b.x,
np*a.y+pp*b.y,
np*a.z+pp*b.z,
np*a.w+pp*b.w);
}
///<summary>
///색상 코드를 가진 정수를 Vector4 객체로 변환해 인스턴스화합니다.
///</summary>
public static function Color(color:uint):Vector4 {
var a:Vector4 = new Vector4();
a.color = color;
return a;
}
///<summary>
///색상 코드를 가진 정수를 Vector4 객체로 변환해 인스턴스화합니다.
///</summary>
public static function Color32(color:uint):Vector4 {
var a:Vector4 = new Vector4();
a.color32 = color;
return a;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment