How to appropriately write pixels to a Bitmap and not break the color

Hello everyone,

I am working on a project where I need to draw pixels to a bitmap. I think I am doing it wrong because the color is completely off.

Here is the code I am using:

	var r = target.transparentColor.r;
	var g = target.transparentColor.g;
	var b = target.transparentColor.b;
	var data = new BitmapData(target.width, target.height, false, 
	AGIColorConverter.convertRGBAToInteger(r, g, b));

	try {
		// This draws correctly but the color is WAAAAAAAAAAAAY off!
		for (x in {
			for (y in {
				var pixel =[y * target.width + x];
				data.setPixel32(x, y, pixel);
	} catch (e:String) {

	var bitmap = new Bitmap(data);
	bitmap.scaleX = 4;
	bitmap.scaleY = 4;

I am not sure what I am doing wrong but the colors I am drawing (such as rgb(168,0,0)) shows up way darker and more purple than they should be. I am building against windows. Is there a way to define the color space or perform RGB mapping on the bitmap?

Are you using ARGB?

You can also use data.getPixels() and data.setPixels() if you would rather get a ByteArray you can manipulate at once

1 Like

Nope - that fixed the problem. I will look into the data.getPixels() and data.setPixels() suggestions. Thank you for your help!

1 Like

Alright another question, this time about setPixels(). It appears the ByteArray returned by getPixels is not returned as a reference since modifying those pixels don’t get drawn. When I use setPixels() I get this error: When I make a call I get the error “Error : End of file was encountered.”

I have seen code getting the ByteArray from BitmapData and writing it back, is this correct?

var rect = new Rectangle(0, 0, target.width, target.height);
var byteArray = data.getPixels(rect);
for (i in
data.setPixels(rect, byteArray);

I think that setPixels might respect the position on the ByteArray instance.

Try adding byteArray.position = 0; just before setPixels :slight_smile:

1 Like

You sir are a gentleman and a scholar, much appreciated!

1 Like