
Please update your flash player
Update (20 April 2009)Uploads are temporarily not working as my server configuration has changed. I’m working on a fix and will have an update ASAP.
This is now fixed, but Flash Player 10 is required to view the demo
If you want a very simple way of extracting a colour palette from an image, one technique would be to average the colour values within specific areas.
Averaging colour values is almost identical to averaging numbers, except with the added initial step of finding the red, green and blue components of the colour. To do this we can use bitwise operators, in this case bitwise shift, to perform fast operations on each bit inside the unsigned integer returned by getPixel or getPixel32. If you want to know more about bitwise operators, Moock has written a detailed and, as ever, very clear article on where, when and why to use bitewise operations. You can read it here.
So once you’re familiar with how to shift the bits of an integer, you can easily get the RGB values from a 24 bit hexadecimal by moving the bits to the right by a certain amount using the bitwise right shift operator (>>).
For example, getting the red, green and blue values from a 24 bit integer would look like:
var colour:uint = 0x33CC99; var R:Number = colour >> 16 & 0xFF; var G:Number = colour >> 8 & 0xFF; var B:Number = colour & 0xFF;
And finding the alpha, red, green and blue values of a 32 bit integer:
var colour:uint = 0xFF33CC99; var A:Number = colour >> 24 & 0xFF; var R:Number = colour >> 16 & 0xFF; var G:Number = colour >> 8 & 0xFF; var B:Number = colour & 0xFF;
So we know that by shifting the bits to the right, we can effectively break an unsigned integer representing a colour into its specific components, but what about changing these components back into an RGB or ARGB value? Well, it’s simply a matter of shifting the bits in the other direction, using the bitwise left shift operator, which looks like this << (two less than operators next to each other).
So using this bitwise shift left operator, you can take your RGB or ARGB values and cram the little buggers back into a usable format, with a little help from bitwise OR:
// RGB var colourRBG:uint = (R << 16 | G << 8 | B); // ARGB var colourARBG:uint = (A << 24 | R << 16 | G << 8 | B);
Right, so we know how to take an unsigned integer, for example one that’s been returned from getPixel or getPixel32 and break it down into its RGB or ARGB components - and then for the sake of universal harmony, put it back together again and release it back into its 24 or 32bit world with all its binary friends.
So now, finding the average colour within a set of colours is simply a question of averaging the red, green and blue values of all the colours, and then turning these averages into a new colour.
The method bellow does just this, by looping through the pixels in a BitmapData object, adding up all of the red, green and blue values, dividing them by the total number of pixels and then creating a new colour from the results.
public static function averageColour( source:BitmapData ):uint { var red:Number = 0; var green:Number = 0; var blue:Number = 0; var count:Number = 0; var pixel:Number; for (var x:int = 0; x < source.width; x++) { for (var y:int = 0; y < source.height; y++) { pixel = source.getPixel(x, y); red += pixel >> 16 & 0xFF; green += pixel >> 8 & 0xFF; blue += pixel & 0xFF; count++ } } red /= count; green /= count; blue /= count; return red << 16 | green << 8 | blue; }
So taking this a step further; say we want an array of 64 colours from an image, we can break the input image down into 64 chunks and find the averages of each. The easiest way of doing this is to build a grid, copy the pixels in each cell and use the averageColour function to return the average colour value of that cell.
public static function averageColours( source:BitmapData, colours:int ):Array { var averages:Array = new Array(); var columns:int = Math.round( Math.sqrt( colours ) ); var row:int = 0; var col:int = 0; var x:int = 0; var y:int = 0; var w:int = Math.round( source.width / columns ); var h:int = Math.round( source.height / columns ); for (var i:int = 0; i < colours; i++) { var rect:Rectangle = new Rectangle( x, y, w, h ); var box:BitmapData = new BitmapData( w, h, false ); box.copyPixels( source, rect, new Point() ); averages.push( averageColour( box ) ); box.dispose(); col = i % columns; x = w * col; y = h * row; if ( col == columns - 1 ) row++; } return averages; }
In terms of building an accurate colour palette from an image, this isn’t a great method, mainly because averaging the colours doesn’t give you a fair representation of the range of colours in the image. The palette returned can often be somewhat muddy; however it is still a technique that I’ve found a use for on a surprisingly regular basis and so thought it worth sharing. Becoming familiar with bitwise operators is also a useful exercise and so, if they aren’t already part of your life, I suggest you begin to woo them with your nerdy charm.
18 Responses to BitmapData Average Colours
AS3 Colour class for extracting the color palette from a BitmapData image or photo, ColourUtils.as
Oct 11th 2008
7:52 pm
[...] Comments BitmapData Average Colours 17% similar Finding the average colours in an image [...]
ynk
Oct 20th 2008
11:03 am
hey there !
very interesting stuff !
i’ve made a color conversion class a while ago… if u wanna try u’ll find it here :
http://code.google.com/p/martian-arts/source/browse/trunk/martian/soup/utils/Color.as
Adrian
Oct 20th 2008
2:52 pm
Great post, I’ve been working with color in AS3 and this tutorial helps me a lot with various tips. Thank you.
Just a question: How can you export the average colors as a hexagesimal list and append it to a text file when you click an “export button”?
Soulwire
Oct 20th 2008
8:38 pm
Hi YNK
Yeah, I’ve been using a colourConversion class myself for some time, its very useful. I’ve also been using classes for HSB, ARGB and RGB, though I see you have a method for converting to CMYK which is great! Thanks for sharing :)
I’ve just been looking around your other classes, there is some fantastic stuff there mate. Keep up the good work, I’m a fan!
Hi Adrian
Yeah, its very simple, just trace each colour in the array to a string, passing 16 as the parameter and it will return the hex, so a loop like this should do it:
The saving could be done with a server side script or through AIR. The button in my demo uses the setClipboard method, so if this is just for your personal use you can call this method then paste it into the txt file yourself.
If you were using AIR, there is all sorts of potential for looking at writing the colours into a Photoshop palette or something similar.
Hope that helps
Vadim
Nov 4th 2008
8:40 pm
I dont think this is good way to get average colors as you want. Two loops work much longer than just one bitmapData resize operation. Resize it to (for example) 3×4 pixels without smoothing - and you will det you 12 pixels with 12 different colors.
Anyway, both methods are rubbish, but simple.
Vadim
Nov 4th 2008
9:03 pm
I mean rubbish if you want GIF-alike palette
Soulwire
Nov 4th 2008
9:52 pm
Hi Vadim,
Yep, you can certainly do it that way, you’re right. It does actually yield very different results though (I just drew the source to a new BitmapData using a scaled Matrix), but I guess this would be to do with how flash player scales images, and I can’t claim to know how that works internally (bicubic interpolation I would assume?)
To get back to your general point though, I would aggree, a ton of getPixels isn’t ideal. The main purpose of this post was to talk about bitwise operators and getting the RGB values from a uint etc. In the post about getting the best color palette from an image I used paletteMap, maybe this is best for the GIF-alike look you mentioned? There’s also Ralph Hauwert’s image dithering technique.
Vadim
Nov 4th 2008
10:58 pm
Yes, that is exactly what I’m talking about, thanks for link!
You know, I used some work with pixels in my drawing game, Sketch2, that was interesting
AS3: Image Posterization + ColorUtils + Adaptively Colored UI’s : TroyWorks
Dec 26th 2008
7:47 am
[...] http://blog.soulwire.co.uk/flash/actionscript-3/colourutils-bitmapdata-extract-colour-palette/ [1b] http://blog.soulwire.co.uk/flash/actionscript-3/extract-average-colours-from-bitmapdata/#more-165 [2] http://en.nicoptere.net/?p=8 Color [...]
Bas Horsting
Feb 17th 2009
11:32 am
An idea: If you are lucky enough to work with OS X Leopard, you can clip this little application and put it on your Dashboard for future use, as I did. Great little widget this way! Thanks for sharing.
SUTUTUYET
Feb 27th 2009
9:17 am
i am working on the Coloring Project and that is exactly what I’m looking for, but my question now is that how can I trace the color code(or add the function to the box) when i click the color box
thanks
Color Palettes of Game Advertisements in German Video Game Entertainment Magazins | MINDsPAGE
Apr 19th 2009
9:47 pm
[...] Soulwire – Finding the average colours in an image [...]
Gaggo
Apr 24th 2009
12:16 pm
Hi,
just wanted to tell you that I really love your work, this blog, the code snippets and everything else around here! Keep up the good work!
best
Rasso
Ramón Illobre
Apr 27th 2009
4:09 pm
You dont need to upload anything. The FileReference class (FP10) has a load method that loads a local file into flash. When the file is load an Event.COMPLETE is dispatched and the file.data is filled with the image ByteArray.
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/net/FileReference.html#load()
Soulwire
Apr 27th 2009
5:34 pm
Cheers Ramon!
Yep, I’m aware of the FP10 capabilities. Unfortunately these demos were published for FP9 and using a different approach. I was hoping to grab an evening this week to change my .htaccess file etc, and get this working, though your suggestion for using FP10 is a good one and I think I’ll do just that!
The only problem is; if I go FP10 for these demos then I’ll feel like I should update the source too to use typed arrays (vectors) and maybe even Pixel bender to make these algorithms more efficient! ;)
Thanks for the comment - I’ll fix the fileSystem stuff asap and then can think about getting these algorithms running at top speed!
sekati
Jun 4th 2009
6:17 pm
You might be interested in the ColorUtil I wrote as part of my framework (used in a large colorful cosmetics application as well); it offers averaging for any display object (or portion of a display object) in R, G, B, saturation, contrast, lightness and value which can really come in handy.
src: http://code.google.com/p/sekati/source/browse/trunk/src/sekati/utils/ColorUtil.as
doc: http://docs.sekati.com/sekati/sekati/utils/ColorUtil.html
Luis
Jun 15th 2009
6:11 pm
Do you have a full working version? The demo only copies 16 colors to clipboard and I need at least 64 colors… Thanks
Soulwire
Jun 15th 2009
6:33 pm
Hi Luis,
Oops, my bad - this data copied to the clipboard wasn’t being updated when you select a new amount for the colours.
The demo is updated now and will give you your desired amount of colours. Clear your cache and you’re good to go!
Leave a Reply