October 22, 2024
Chicago 12, Melborne City, USA
PHP

How to reuse colour palettes across multiple PHP GD canvases/images?


How can I draw and refer to the same set of colours across multiple GD canvasses?

I am new to using PHP-GD for images, I have only done a couple very basic things with it.

I am creating a map generator that will have 3+ scales as GD canvases. Each scale represents zooming in to the previous scale, and adding more details. The first scale is a 20×20 image with land and water colours. The second scale is 60×60 pixels (each pixel turns into a 3×3 grid of pixels), and does some interpolation to round out the pixels of the first scale.

Everything is working fine, except PHP doesn’t seem to provide any way to reuse and reference colours across multiple images. My map generation is entirely based on the colour of pixels read from the image canvas. I want to reference the same colours in all 3 scales of my map, but I am being forced to create these silly "colour references" as returned by imagecolorallocate(). True colour images do not have palettes, you just use any colour you want. Why doesn’t PHP allow proper true colour images without palettes?

I need to be able to do the following:

  • Check pixel colours in all 3 of my GD canvasses to see if they match a specific list of colours (land, water, etc.)
  • Draw pixels in any of the 3 GD canvasses using the same list of colours (land, water, etc.)

I’m looking for a way to avoid repeating variables for each colour, like I currently have:

$Colour_Water_1 = imagecolorallocate($Scale_1, 68, 97, 147);
$Colour_Land_1  = imagecolorallocate($Scale_1, 99, 67, 33);

$Colour_Water_2 = imagecolorallocate($Scale_2, 68, 97, 147);
$Colour_Land_2  = imagecolorallocate($Scale_2, 99, 67, 33);

I would be fine with one $Variable for each individual colour across every scale, but not different variables for every colour at every scale! What approach does anyone suggest I take?

Ideal would be one global array of colours usable by every GD canvas:

$Map_Colours = array(
   "Water" => array(68, 97, 147),
   "Land"  => array(99, 67, 33),
);

The documentation for imagecolorallocate() does not even specify what is returned by the function. What is the "colour reference" that is returned?

I have considered creating one giant canvas with the 3 smaller images drawn onto different locations of the giant canvas, which avoids having multiple palettes, but this is non-ideal and kind of stupid, and will be more and more wasteful as I keep adding further zoomed-in scales to my map generator.

It doesn’t matter to me if the map images are in true colour or palette based. I originally made the images as palette based, but once I realised the palettes being used are not the same between different GD canvasses, and I can’t reference the same colours in each canvas, that idea fell apart. I then switched to true colour images because I thought they would "just work", because of not having a palette internally, but PHP GD still requires the use of imagecolorallocate() on true colour images!

I found the function imagepalettecopy(), but it has the same issue that each canvas requires its own separate colour reference variables.

I also found this comment on php.net which stated you can reference any colour in a true colour image as if it were already allocated, but the code they provided wasn’t very clear to me:

jessiedeer at hotmail dot com - 11 years ago

There is no need to allocate colors with imagecreatetruecolor. All [256 x 256 x 256 x 128] true colors are already allocated, and you can use the color indexes directly.

Examples :
Blue => color index 255.
White => color index 16777215 (= 255*256² + 255*256+255).
Full transparent => color index 2130706432 (= 127*256^3).

This looks similar to how QBASIC referenced 6-bit VGA colours (RGB values from 0 to 63):

(Red) + (Green * 256) + (Blue * 65536)

Also, why do they list the alpha as 128 (only 7bits instead of 8bits)?

If this method does work using the correct formula, it would also need to be usable when checking a pixel on the canvas to see if it matches a particular colour (land, water, etc.).



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video