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
>>>>>> _______________________________________________
>>>>>> [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
>>>>>
>>>> _______________________________________________
>>>> [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
>>>
>>>
>>>
>>>
>> _______________________________________________
>> [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
>
>
_______________________________________________
[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