Simple coordinate question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iyia12co
Forum Newbie
Posts: 1
Joined: Sun Jul 24, 2011 8:57 pm

Simple coordinate question

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Simple coordinate question

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Simple coordinate question

Post 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.
Post Reply