imageColorAt() and imageColorsForIndex() seem redundant

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
NikatRA
Forum Newbie
Posts: 1
Joined: Fri May 16, 2003 8:10 pm

imageColorAt() and imageColorsForIndex() seem redundant

Post by NikatRA »

When looking at the documentation for these two functions, they seem to work together, but there is a redundancy that I don't understand.

The "imageColorAt()" function takes two parameters as explained in the documentation:

+ int imagecolorat ( resource image, int x, int y)

+ Returns the index of the color of the pixel at the specified location in the image specified by image.


The "imageColorsForIndex()" function also takes two parameters as noted:

+ array imagecolorsforindex ( resource image, int index)

+ This returns an associative array with red, green, and blue keys that contain the appropriate values for the specified color index.



Since you need to use the imageColorsAt() function to obtain the index, and the imageColorsAt() function has already accessed the resource image for this value, why does the imageColorsForIndex() function need to reaccess the resource image?

Shouldn't it just be able to return the RGB values by evaluating the color index?

If anyone can give some insight, it would be greatly appreciated.

Thanks.

Nik
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

It could on a 32bit truecolour jpeg image as the index exactly mimics the hex rrggbb - on images with reduced palettes the index is a numeric that stores the hex values - eg [12]=>EB56DA - so there is no quick way of backtracking from the colour to get the index (unlike on a truecolour palette which always has 16.7 million indices, one for each colour and each index converted to hex would represent the colour [16777216]=>FFFFFF)

Also, referencing the image pointer helps once you start pulling in multiple images for create schemes.

Realistically you could merge the calls to

$var = imagecolorsforindex($img,imagecolorat($img,$pos_x,$pos_y));

On a subnote: you cannot iterate the entire palette array of a truecolour image by using for($x=0; $x<imagecolorstotal($img); $x++) - no timeouts occur, it just won't let you.
Post Reply