Page 1 of 1

Simple coordinate question

Posted: Sun Jul 24, 2011 9:00 pm
by iyia12co
I wish to retrieve the x/y-coordinates of a specific color (where ever it is found; first occurrence would be fine). Now, I could write a bunch of 'for' loops to parse the image until the color is found, but that will use a great deal of cpu and time. Is there any easy way of getting color coordinates. For instance, I could call something like:

Code: Select all

function findcolor($imagehandle,$red,$green,$blue,$alpha=false) {
   ...
   ...
   return array('x'=>$x,'y'=>$y);
}
OR EVEN

Code: Select all

function findcolor($imagehandle,$colorindex) {
   ...
   ...
   return array('x'=>$x,'y'=>$y);
}
I would appreciate any help.

- Marco

Re: Simple coordinate question

Posted: Sun Jul 24, 2011 10:18 pm
by McInfo
First, ask imagecolorexact() whether the color exists in the image. If it says yes, do a multiple-pass search that examines pixels that are far apart and gradually narrows the distance. Generally, like colors will be grouped together, so searching this way will reduce redundant comparisons. It might be helpful to research binary search or Adam7.

Re: Simple coordinate question

Posted: Sun Jul 24, 2011 10:23 pm
by Jonah Bron
Could you tell us the application of this? Perhaps we could recommend something better. And if speed is important, you could write a C extension to do it.