Re-mapping colours on a bitmap to specific values using pallete

i’m attempting to take a bitmap and re-colour it during an event. It appear that two instances of the bitmap in the resources xml, one with no pallete options and one with specific pallete options can be called at different times.

My question is whetheriI can force a color to be re-coloured to another color other than the closest value. e.g. during my event I would like pixels with value FFFFFF drawn as 000000 but pixels coded as 555555 still drawn as 555555. If I just enter 000000 and 555555 as the only two colors in the pallete everything ends up 555555 (which is closer to FFFFFF than 000000). Does anyone have a way of forcing that similar to “draw FFFFFF as 000000”?
  • You can't change the colors in a bitmap. If you want two different color options, you can use two different bitmaps. (the same, just different colors). But if you want to allow the screen background color to change, you could find you suddenly have a bunch of bitmaps to work with the different screen backgrounds (which is harder to maintain if you ever want to change the bitmap)

    Another common options, is do the images in a custom font, as with a font, you can set the color in the code, as you can with a native font, and for something you want that's two colors, use two different "characters" as the two parts, and set the color of the first, put it on the screen and then the color of the second, and put it on top of the first using a transparent background. Or use the first, and then direct dc calls to change the color of part of it. An example of the two color, could be a battery Icon, where you draw the battery, then change the color/size of the inside of the battery to show the level. You can of course use direct dc calls to draw everything - a back rectangle, with red or green inside to show the battery state and level.

    I use the custom font approach for icons used in watchfaces, as you can easily make them any color the user selects.
  • Former Member
    Former Member over 6 years ago
    Hey Jim you don't miss very often, but this is a tricky one, so I'll let it slide. ;)

    This can be done using the BufferedBitmap class.

    Hopefully I have your details correct here.
    - You have a bitmap that consists of pixels that are #FFFFFF and #555555
    - You want it to draw with #FFFFFF and #555555 sometimes but #000000 and #555555 at other times.

    You will want to define your bitmap resource with the palette [0xFFFFFF, 0x5555555] (this will implicitly add TRANSPARENT to the end unless you use the disableTransparency attribute)

    In your code, you can load this bitmapResource, and use it to initialize a BufferedBitmap object. Once you have done that you should be able to pass that BufferedBitmap object to dc.drawBitmap in the same way you can with the bitmapResource. If you call getPalette() on the BufferedBitmap object you should get this array back: [0xFFFFFF,0x555555,Graphics.COLOR_TRANSPARENT]. Now you can set the first index of this array to 0x000000, which results in this array: [0x000000,0x555555,Graphics.COLOR_TRANSPARENT]. Passing that array to BufferedBitmap.setPalette() will change the color palette of the buffered bitmap such that all pixels that were previously 0xFFFFFF are now 0x000000. You can do the same process changing index 0 back to 0xFFFFFF to return to the original color palette.
  • Ok, Brian, all I can say in my defense is sometimes old dogs DO learn new tricks! :) I hadn't even considered buffered bitmaps!
  • Woah thanks both of you, that’s awesome! I shall go and try this