Created
June 17, 2013 03:43
-
-
Save dpeek/5794527 to your computer and use it in GitHub Desktop.
Abstract color.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Test | |
{ | |
static function main() | |
{ | |
draw(0xFF); | |
draw(function(x,y){ return 0xFF0000; }); | |
// draw(function(x,y){ return "foo"; }); // compile error, not color | |
} | |
static function draw(color:Color) | |
{ | |
trace(color.getValue(0, 0)); | |
} | |
} | |
abstract Color(Int->Int->Int) | |
{ | |
public function new(f:Int -> Int -> Int) | |
this = f; | |
public inline function getValue(x:Int, y:Int) | |
return Reflect.callMethod(null, this, [x,y]); | |
@:from static public inline function fromInt(v:Int) | |
return new Color(function(_,_){ return v; }); | |
@:from static public inline function fromFunc(v:Int->Int->Int) | |
return new Color(v); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment