Games Find dominant colours in a game graphic

Andrew Gray

Member
Licensed User
Longtime User
I have objects in my game that explode into coloured shrapnel. It would be nice if the colours of the shrapnel match the colours of the sprite, e.g. a mostly green object would explode into mostly green pieces.

Any suggestions on how to implement this? There are libraries such as ColourThief that extract the dominant colours from a bitmap, but how do I get the bitmap for a game object? I could presumably use BodyWrapper.GraphicName to find the name of the graphic and GraphicCache.GetGraphic to get that graphic as a CompressedBC. But how do I then turn a CompressedBC into a B4XBitmap?

There are quite a variety of objects so manually setting up a colour palette for each type fo exploding object would be tedious.
 

emexes

Expert
Licensed User
Choose random pixels from the game object (sprite?) until you've got say 10 samples with say alpha >50%. Or for something more deterministic, choose 20 random samples and sort them by alpha and use the "best" 10.

If that still sounds slow: precalculate it beforehand, so you have a ready-to-go array of say 10 explosive colours for each game object (sprite?).

Or can you slice up the sprite bitmap say 6 x 6 so that you have 36 pieces of shrapnel? Although they'd look odd being rectangular, so maybe try setting alpha of edge pixels to random 0..50% to give jagged edges?
 

emexes

Expert
Licensed User
Or are your explosions like mini sprite-sized 2-second movies, rather than individual pieces of shrapnel? And you want to change the movie's colour to make it more like the color of whatever it is exploding?
 

Andrew Gray

Member
Licensed User
Longtime User
Choose random pixels from the game object (sprite?) until you've got say 10 samples with say alpha >50%. Or for something more deterministic, choose 20 random samples and sort them by alpha and use the "best" 10.

That's the kind of thing I want to do, the question is, how to I actually get pixel colour values from the sprite?

If I had an ordinary bitmap object it would be straightforward, but the sprite comes in the form of a CompressedBC and I'm not sure how to work with that. (I can find documentation on how to convert a bitmap into a CompressedBC but not vice versa.)

I don't mind if the process is slow - as you say, I can precalculate beforehand if needed.
 

Sandman

Expert
Licensed User
Longtime User
Any suggestions on how to implement this?
I would use ColourThief (or something like it) to preprocess all graphics and get a short list of their dominant colours. That would eliminate the need to do it live.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…