4 Replies Latest reply on Sep 11, 2009 8:24 AM by Chunick

# fasted way to find best color match in colors list

i have a list (1000+) with color values rbg() -- [rgb(34,56,78),rgb(45,23,78)......]

what is the fastest way to find the closest match for a specific color.

i have to scan an image 640x480 pixels, and replace all pixels in this images with the best match color from my color list.

is it possible to have a script make a palette. and use the palette to remap the colors of an image. and then get the rgb color values of the remapped image?

• ###### 1. Re: fasted way to find best color match in colors list

If you sort your list then you can use the list method, findPosNear()... like so:

cList = [color( 255, 0, 0 ), color( 0, 0, 0 ), color( 127, 255, 0 ), color( 255, 255, 255 ) , color( 127, 0, 255 ), color( 127, 127, 127 )]

cList.sort()

put cList

-- [color( 0, 0, 0 ), color( 127, 0, 255 ), color( 127, 127, 127 ), color( 127, 255, 0 ), color( 255, 0, 0 ), color( 255, 255, 255 )]

put cList.findPosNear(color(124, 12, 240))

-- 2

put cList[2]

-- color( 127, 0, 255 )

This will still take a bit of time because you still have to iterate through each pixel, however, I mention it as a solution because you mention a list of 1000 entries... otherwise, I would go the route you mention of using a palette, but you cannot have 1000 colours in a palette; only a maximum of 256 colours.

• ###### 2. Re: fasted way to find best color match in colors list

thanks will give it a try.

hope to cut down 45 min. to under 10 min.

never thought a list of colors would listen to findPosNear.

will check if my match result is the same as the findPosNear result.

thanks.

If you sort your list then you can use the list method, findPosNear()...

like so:

>

cList = [color( 255, 0, 0 ), color( 0, 0, 0 ), color( 127, 255, 0 ), color(

255, 255, 255 ) , color( 127, 0, 255 ), color( 127, 127, 127 )]

cList.sort()

put cList

-- [color( 0, 0, 0 ), color( 127, 0, 255 ), color( 127, 127, 127 ), color(

127, 255, 0 ), color( 255, 0, 0 ), color( 255, 255, 255 )]

>

put cList.findPosNear(color(124, 12, 240))

-- 2

put cList[2]

-- color( 127, 0, 255 )

>

This will still take a bit of time because you still have to go through

each pixel, however, I mention it as a solution because you mention a list

of 1000 entries... otherwise, I would go the route you mention of using a

palette, but you cannot have 1000 colours in a palette; only a maximum of

256 colours.

>

• ###### 3. Re: fasted way to find best color match in colors list

a=[color(0,0,1),color(30,56,0)]

a.sort()

put a.findPosNear(color(30,56,2))

-- 3

why does it find a position outside the list ?

If you sort your list then you can use the list method, findPosNear()...

like so:

>

cList = [color( 255, 0, 0 ), color( 0, 0, 0 ), color( 127, 255, 0 ), color(

255, 255, 255 ) , color( 127, 0, 255 ), color( 127, 127, 127 )]

cList.sort()

put cList

-- [color( 0, 0, 0 ), color( 127, 0, 255 ), color( 127, 127, 127 ), color(

127, 255, 0 ), color( 255, 0, 0 ), color( 255, 255, 255 )]

>

put cList.findPosNear(color(124, 12, 240))

-- 2

put cList[2]

-- color( 127, 0, 255 )

>

This will still take a bit of time because you still have to go through

each pixel, however, I mention it as a solution because you mention a list

of 1000 entries... otherwise, I would go the route you mention of using a

palette, but you cannot have 1000 colours in a palette; only a maximum of

256 colours.

>

• ###### 4. Re: fasted way to find best color match in colors list

Remko_van_Dokkum wrote:

a=[color(0,0,1),color(30,56,0)]

a.sort()

put a.findPosNear(color(30,56,2))

--

why does it find a position outside the list ?

I suspect that's because findPosNear tests between two values, without going over the value.... ok, that's not very clear, but with that in mind you could cap the value like this:

-- set a.count to a variable outside your loop, so it's not being calculated every time

listCount = a.count

newColorIndex = a.findPosNear(color(30,56,2))

if newColorIndex > listCount then

newColorIndex = listCount

end if