Skip to content

Instantly share code, notes, and snippets.

@th3d0g
Created November 26, 2014 16:40
Show Gist options
  • Save th3d0g/8678500a3a1f6e8e8889 to your computer and use it in GitHub Desktop.
Save th3d0g/8678500a3a1f6e8e8889 to your computer and use it in GitHub Desktop.
Flambe - Get Pixel Colour
// Returns the colour of a pixel.
// ImageUtils.hx
import flambe.display.Texture;
import haxe.io.Bytes;
import haxe.io.BytesData;
class ImageUtils
{
public static function getPixelColour( x:Float, y:Float, texture:Texture ):Int {
var bytes:Bytes = texture.readPixels( Math.round( x ), Math.round( y ), 1, 1 );
var bytesData:BytesData = bytes.getData();
var colourHex:String = bytes.toHex();
var hexString = decode(colourHex.substring(0, 6)).toHex();
return Std.parseInt("0x" + decode(colourHex.substring(0, 6)).toHex())
}
private static function decode( str:String ):Bytes {
var base = Bytes.ofString("0123456789abcdef");
// not using `decode` or `decodeString` because the result may contain \0
// and that is not accepted in strings on all targets
return new BaseCode(base).decodeBytes(Bytes.ofString(str.toLowerCase()));
}
}
// Example
imageSprite.pointerDown.connect(function(event : PointerEvent){
var imageSprite:ImageSprite = cast( event.hit, ImageSprite );
var pixelColour = ImageUtils.getPixelColour( event.viewX - imageSprite.x._,
event.viewY - imageSprite.y._,
imageSprite.texture );
trace( pixelColour );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment