This sounded interesting so I wrote something up for you Josh:
//color that is being compared
var a:Number = 0x12345;
//color set to use
var b:Array = [0x000000, 0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0x00FFFF,
0xFF00FF, 0xFFFF00];
//get the distance between two colors
function getDistance(a:Number, b:Number){
var i =-1, a = getRGB(a), b = getRGB(b), r=[];
while(++i<3)
r[i] = a[i]<b[i]?b[i]-a[i]:a[i]-b[i];
return r;
}
//convert hex color to a number
function getRGB(c:Number):Array {
return [(c&~0x00FFFF)>>16, (c&~0xFF00FF)>>8, (c&~0xFFFFF00)];
}
//you can use (c&~0x00FFFFFF)>>32 to get red for rgba colors, and using the
pattern below, you can get the rest.
//I don't know how good my math is in this, there might be a better way to
get the rgb values
//the lowest average is the closest match, in this example, it's black.
for(var i in b){
var j = getDistance(a, b[i]), k=(j[0]+j[1]+j[2])/3;
trace(j+' average: '+k);
}
Hope this works,
M.
On 3/15/06, Josh Buhler <[EMAIL PROTECTED]> wrote:
>
> I'm working on a project that requires that I take an uploaded image,
> and convert it to use a limited palette of colors - around 5-10 colors.
>
> I've got the custom palette I have to work with stored in an array,
> and for each color in my image, I've got it finding the color in the
> array it's closest to numerically, but the results aren't exactly
> what I'm looking for.
>
> Does anybody know of any formulas available for comparing multiple
> colors and finding the ones that are the closest matches? I've been
> searching Google for a while, with no luck. Any good resources on
> color formulas & such would be appreciated.
>
> - Josh
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com