Skip to content

Instantly share code, notes, and snippets.

@dpeek
Created June 17, 2013 03:43
Show Gist options
  • Save dpeek/5794527 to your computer and use it in GitHub Desktop.
Save dpeek/5794527 to your computer and use it in GitHub Desktop.
Abstract color.
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