This post is in spite of the discussion, I had adapted this function from
before, but originally I decided to not post it. I liked the problem so I
spent some time on it. I noticed Joshua that you used ^ in place of Math.pow,
while ^ maybe pow in C or maybe even Java, ^ is the xor operator in
Actionscript. So, the following code is a set of different methods to
getting the closest color with the non-pythagoras technique preceding the
pythagoras technique.

function getClosestHex(a:Number, b:Array){
    var c=-1, d=b.length, e=768, f, a = [a>>>16, a>>>8&0x00FF, a&0x0000FF];
    while(++c<d) if(e!=(e=Math.min( e,
Math.abs(a[0]-(b[c]>>>16))+Math.abs(a[1]-(b[c]>>>8&0x00FF))+Math.abs(a[2]-(b[c]&0x0000FF))
) )) f=c;
    return b[f];
}

var a = 0x80807f;
var hexPalette:Array = [0x000000, 0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF,
0x00FFFF, 0xFF00FF, 0xFFFF00];
var getA = getClosestHex(a, hexPalette);
trace(getA.toString(16));

The former would be the method for hex numbers, however using RGB arrays is
quicker, I found converting the palette values to RGB arrays beforehand
would eliminate the redundancies of extracting color values, the following
code assumes the hexPalette array in the former code is defined.

function buildRGB(palette:Array){
    var a=[], i=-1, l=palette.length;
    while(++i<l) a[i]=[palette[i]>>>16, palette[i]>>>8&0x00FF,
palette[i]&0x0000FF];
    return a;
}

function getClosestColor(color:Array, palette:Array){
    var c=-1, d=palette.length, e=768, f;
    while(++c<d) if(e!=(e=Math.min( e,
Math.abs(color[0]-palette[c][0])+Math.abs(color[1]-palette[c][1])+Math.abs(color[2]-palette[c][2]))))
f=c;
    return palette[f];
}

var b = [128, 128, 127];
var rgbPalette:Array = buildRGB(hexPalette);
var getB = getClosestColor(b, rgbPalette);
trace(getB);

This is what the code would look like using pythagoras:

function getClosestHex(a:Number, b:Array){
    var c=-1, d=b.length, e=256, f, g, h, a = [a>>>16, a>>>8&0x00FF,
a&0x0000FF];
    while(++c<d) {
        g=0, h=-1;
        while(++h<3) g+= Math.abs(Math.pow(a[h]-(b[c]<<8<<(h*8)>>>24), 2));
        if(e!=(e=Math.min(e, Math.sqrt(g)))) f=c;
    }
    return b[f];
}

var b = 0x80807f;
var hexPalette:Array = [0x000000, 0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF,
0x00FFFF, 0xFF00FF, 0xFFFF00];
var getB = getClosestHex(b, hexPalette);
trace(getB.toString(16));

function buildRGB(palette:Array){
    var a=[], i=-1, l=palette.length;
    while(++i<l) a[i]=[palette[i]>>>16, palette[i]>>>8&0x00FF,
palette[i]&0x0000FF];
    return a;
}

function getClosestColor(color:Array, palette:Array){
    var c=-1, d=palette.length, e=256, f, w, z, x=color.length;
    while(++c<d) {
        w=0, z=-1;
        while(++z<x) w += Math.abs(Math.pow(color[z]-palette[c][z], 2));
        if(e!=(e=Math.min( e, Math.sqrt(w)))) f=c;
    }
    return palette[f];
}

var a = [255, 128, 127];
var hexPalette:Array = [0x000000, 0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF,
0x00FFFF, 0xFF00FF, 0xFFFF00];
var rgbPalette:Array = buildRGB(hexPalette);
var getA = getClosestColor(a, rgbPalette);
trace(getA);

So, like Darren mentions, you could drop pythagoras and it would still be
accurate.

M.

On 4/4/06, Ron Wheeler <[EMAIL PROTECTED]> wrote:
>
> Another minor optimization
>
> Initialize tempDistance to a huge number (greater than the distance from
> ff000 to0000ff) and then simplify the if
>
> Ron
>
> Darren Cook wrote:
> > Hi,
> > A minor optimization, but you can drop the Math.sqrt(). I.e.
> >    x^2 > y^2  == x > y
> >
> > for all positive values of x and y.
> >
> > Darren
> >
> >
> >
> >> That's what I actually ended up doing, and it worked out pretty good:
> >>
> >> private function getClosest (red1:Number, green1:Number,
> >> blue1:Number):Number {
> >>
> >>         var totalColors:Number = __palette.length;
> >>         var tempDistance:Number;
> >>         var closestColor:Number;
> >>
> >>         for (var i:Number = 0; i < totalColors; i++) {
> >>             // first, break up the color to check
> >>             var red2:Number = (__palette[i] & 0xFF0000) >>> 16;
> >>             var green2:Number = (__palette[i] & 0x00FF00) >>> 8;
> >>             var blue2:Number = __palette[i] & 0x0000FF;
> >>
> >>             // now, get the distance from the source
> >>             var tempD:Number = Math.sqrt ((Math.abs(red1 - red2) ^ 2) +
> >> (Math.abs(green1 - green2) ^ 2) + (Math.abs(blue1 - blue2) ^ 2));
> >>
> >>             if ((tempD <= tempDistance) || tempDistance == undefined) {
> >>                 tempDistance = tempD;
> >>                 closestColor = __palette[i];
> >>             }
> >>         }
> >>
> >>         return closestColor;
> >>     }
> >>
> >>
> >> - Josh
> >>
> >>
> >> On Mar 16, 2006, at 7:59 PM, Ron Wheeler wrote:
> >>
> >>
> >>> If a color can be treated as a point in a 3-d cube 256 units on  each
> >>> side, you can use the formula in this reference
> >>> http://www.uwm.edu/~ericskey/TANOTES/Ageometry/node10.html
> >>> to calculate the "distance" between 2 colors.
> >>>
> >>> I have not tried this but it would seem logical that this would work
> >>>
> >>> Ron
> >>>
> >>> elibol wrote:
> >>>
> >>>
> >>>> K, here is some better math for getRGB, performance will probably be
> >>>> important:
> >>>>
> >>>>     return [c>>>16, c>>8&~0xFF00, c&~0xFFFFF00];
> >>>>
> >>>> You can also try using the ColorTransform or Color class to get  the
> rgb
> >>>> values, except they might be slower than getting the raw math right.
> >>>>
> >>>> M.
> >>>>
> >>>> On 3/15/06, Josh Buhler <[EMAIL PROTECTED]> wrote:
> >>>>
> >>>>
> >>>>> Thanks - I'll give it a shot and see how it goes.
> >>>>>
> >>>>>
> >>>>> - Josh
> >>>>>
> >>>>> On Mar 15, 2006, at 3:10 PM, elibol wrote:
> >>>>>
> >>>>>
> >>>>>
> >>>>>> I tried comparing the hex values directly, but there were
> >>>>>> inaccuracies,
> >>>>>> maybe the same ones you've been having. I think since the value  of
> a
> >>>>>> particular color precedes with a 0 even when it's below 17(0F), the
> >>>>>> preceding 0 causes a shifting in the comparison. It would, for
> >>>>>> example,
> >>>>>> cause 0x000000 to seem farther to 0x123456 than 0x00FF00, where
> >>>>>> visually you
> >>>>>> can see clearly that black is closer to 0x000000.
> >>>>>>
> >>>>>> btw in my example, var a = 0x12345 where it should be 0x123456.
> >>>>>>
> >>>>>> The numbers hold to be accurate after correcting this typo.
> >>>>>>
> >>>>>> 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
> >>>>>>> _______________________________________________
> >>>>>>> Flashcoders@chattyfig.figleaf.com
> >>>>>>> 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
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>> _______________________________________________
> >>>>>> Flashcoders@chattyfig.figleaf.com
> >>>>>> 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
> >>>>>>
> >>>>>>
> >>>>> _______________________________________________
> >>>>> Flashcoders@chattyfig.figleaf.com
> >>>>> 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
> >>>>>
> >>>>>
> >>>>>
> >>>> _______________________________________________
> >>>> Flashcoders@chattyfig.figleaf.com
> >>>> 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
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
> >>> _______________________________________________
> >>> Flashcoders@chattyfig.figleaf.com
> >>> 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
> >>>
> >> _______________________________________________
> >> Flashcoders@chattyfig.figleaf.com
> >> 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
> >>
> >>
> >>
> >
> >
> > _______________________________________________
> > Flashcoders@chattyfig.figleaf.com
> > 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
> >
> >
> >
> _______________________________________________
> Flashcoders@chattyfig.figleaf.com
> 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
>
_______________________________________________
Flashcoders@chattyfig.figleaf.com
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

Reply via email to