A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: AS3 BitmapData color replacing within a range

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Location
    Dublin, Ireland
    Posts
    24

    Question AS3 BitmapData color replacing within a range

    Hi,

    I'm trying to replace a range of colours programmatically - I have sprites for a game that have parts of them in shades of magenta specifically so they can be recoloured to show their team colour (blue,red,etc).

    I've been able to use BitmapData.threshold() to replace a single specific colour with another. What I need to be able to do though is set some sort of range of colours and appropriately shade them blue/green/red while keeping their relative light/dark shades.

    Here's what I've used to do a single colour:
    Code:
    var thisBitmap:Bitmap = MySpriteBitmap; //source bitmap
    var colouredBitmapData:BitmapData = new BitmapData(thisBitmap.bitmapData.width, thisBitmap.bitmapData.height, true, 0x00000000);
    colouredBitmapData.threshold(thisBitmap.bitmapData, thisBitmap.bitmapData.rect, new Point(0,0), "==", 0xFFd6007f, 0xFFFF0000, 0xffffffff, true);
    thisBitmap.bitmapData = colouredBitmapData;
    Anyone able to point me the right way?

  2. #2
    Member
    Join Date
    May 2009
    Posts
    62
    If you convert the color from RGB to HSL, you can relatively easily maintain the same hue and saturation, while changing only the lightness.

    If you're tricky, you can prolly write a single function to take a color from the bitmap, test to see if it's magenta, and then calculate the correct tone based on the orginal tone (Or maybe just encode the data into the bitmap from the get-go), and just run that on each pixel of the image.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You can still use a couple of threshold ops to copy only the pixels you want into a second bitmapdata (one that copies >=, and one that copies <= from that one). From there, you can apply a ColorMatrixFilter or do other color manipulation, then copy the manipulated pixels back into the original bitmap or a copy of it.

  4. #4
    Junior Member
    Join Date
    Mar 2009
    Location
    Dublin, Ireland
    Posts
    24
    Quote Originally Posted by 5TonsOfFlax View Post
    You can still use a couple of threshold ops to copy only the pixels you want into a second bitmapdata (one that copies >=, and one that copies <= from that one). From there, you can apply a ColorMatrixFilter or do other color manipulation, then copy the manipulated pixels back into the original bitmap or a copy of it.
    A good idea, but one I'm having trouble getting to work. It seems to pick up similar colours as well as the magenta shades. One thing I found out was that all the sprites use a specific set of 17 shades of magenta for all team-colour sections; so I can just specify those exact colours one after the other to get a bitmap with just the pixels that need to be colour-shifted. Still working on the colour manipulation though.
    Last edited by gambrinous; 06-15-2009 at 04:01 PM.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yeah, if you're not dealing with a single color channel, then the threshold operations are non-trivial. You could move it to another color space like sharsnik suggested, which would make finding nearby colors easier. But if you have your list of 17 colors, that's probably your best bet.

  6. #6
    Junior Member
    Join Date
    Mar 2009
    Location
    Dublin, Ireland
    Posts
    24
    OK, in the end I used a loop to threshold the 17 shades of magenta to a new bitmapdata, and then I used this nice ColorMatrix helper class to rotate the hue:
    http://www.quasimondo.com/archives/000565.php
    And then just used copypixels() to copy back onto the source image. Works a charm.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center