allRGB Entry – PHP Image Manipulation

The objective of allRGB is simple: To create images with one pixel for every rgb-color (16777216 to be exact); not one color missing, and not one color twice.

What a cool project! As regular readers will know, I love messing about with image manipulation in PHP, so when I heard about the allRGB project I knew I had to make an entry for it. A few false starts and about half an hour later, I proudly submitted my first entry, a 4096×4096 PNG image containing every single possible RGB colour. As one redditor put it, “It’s like poetry, just without words.”

Click for the high resolution (only 173Kb)

And now on to the code:

The authors of the allRGB project state that;

The most obvious example is a series of 16 by 16 squares, each of which consists of a gradient.

But to me the most obvious approach was to simply loop over red (0-255), green (0-255) and blue (0-255) adding a pixel for each colour; as the PHP code below illustrates:

$w = 4096;
$h = 4096;

$x = 0;
$y = 0;

$im = ImageCreateTrueColor($w, $h);

for($r=0 ; $r<=255 ; $r++) {
	for($g=0 ; $g<=255 ; $g++) {
		for($b=0 ; $b<=255 ; $b++) {
			$col = ImageColorAllocate($im,$r,$g,$b);
			ImageSetPixel($im, $x, $y, $col);

			if($x<($w-1)) {
			} else {
				$x = 0;

header("Content-type: image/png");

You'll need to up max_execution_time and memory_limit, but it's not as hungry as you might imagine; under a minute of processing time on my meager home server.

My entry, which I've imaginatively entitled "Grid One", is still undergoing review with the folks at allRGB, but I hope to be listed in their hall of fame shortly (now listed) :0) It's similar to the existing "suave" and "tvlines" entries.

And for my next trick? I'm working on a script to convert any arbitrary image into an allRGB entry. But loops with 16 million iterations need to be carefully optimised. It's a fun challenge, especially in PHP.

Update: Many thanks to Alexander for pointing out that my first attempt didn't actually contain all the colours (*blush*) - code and images updated now :D

Related Posts:

, , , , , , ,

  1. #1 by ACJ on February 10, 2010 - 2:49 am

    You might want to start drawing at 0,0 rather than 1,1; your render is missing some colours. ;-)

    • #2 by Howard Yeend on February 10, 2010 - 2:58 am

      Noooooo! Fixing…

      (now fixed)

  2. #3 by Trung on March 17, 2010 - 7:11 am

    Hey, I played with this when I was 12 :P

  3. #4 by xander722 on June 11, 2011 - 11:08 am

    The color combination were too nice as open there high resolution. I could say that there patterns were perfect.

Comments are closed.