Hudzilla.org - the homepage of Paul Hudson
Contents > Practical PHP > ASCII art Wish List | Report Bug | About Me ]

22.5.3     Analysis: ASCII art in colour

This is NOT the latest copy of this book; click here for the latest version.

Although our pictures look nice enough, the real deal is working in colour: the end results offered are much more attractive, and finally make ASCII art look worthwhile.

Surprisingly, doing colour is easier than greyscale for the large part. This is because with greyscale we needed the array of symbols to be used for relative lightness of characters. Not so in colour. However, in colour we do need one thing that warrants me explaining it after pure ASCII art: you need to use the sprintf() function to convert RGB values into hex.

Because we'll be using the same ASCII character for every output pixel in our ASCII art image (we'll be using #), we need to use colour to distinguish the lightness of each pixel. So, when printing out each #, we need to also print out the relevant HTML code to make it coloured. In ou r previous code, the return value of imagecoloursforindex() was used to grab an array of red, green, and blue values between 0 and 255 for each pixel in the image. However, HTML takes its colour values as hexadecimal between 00 (decimal 0) and FF (decimal 255). As a result, we need to use sprintf() to convert our numbers into hexadecimal.

Parameter one to sprintf() is the format you'd like to output, and the subsequent parameters are values you'd like inserted into that format. For example, %s in a format string means "convert the relevant parameter to a string" before sending it to output. %X in a format string means "convert the relevant parameter to a hexadecimal number" before sending it to output. There are also modifiers for this parameter that allow us to control the length of the converted value, for example %2X means "convert this parameter to a hexadecimal value that must contain two digits". So if we supply the number 11, %2X will first convert it to hexadecimal, giving B, then, noting that it's less than two digits wide, will prepend a 0, giving 0B, which is just what we want for our HTML.

Here's the final code. Note that it also demonstrates loading a picture from a PNG file that also happens to be remote, and uses a different font size to blow the picture up:

<?php
  $image
= imagecreatefrompng("http://ftp.gnome.org/pub/GNOME/teams/marketing/en/2004/two-six-screenshots/html/large/Darren_Adams.png");
  if (
$image) {
    echo
'<PRE STYLE="font: 4px/2px Courier New;">';
    
$width = imagesx($image);
    
$height = imagesy($image);
    for(
$y = 0; $y < $height; ++$y) {
      for(
$x = 0; $x < $width; ++$x) {
        
$thiscol = imagecolorat($image, $x, $y);
        
$rgb = imagecolorsforindex($image, $thiscol);
        
$htmlcol = sprintf("#%2X%2X%2X", $rgb['red'], $rgb['green'], $rgb['blue']);
        
$char = "<FONT COLOR=\"$htmlcol\">#</FONT>";
        echo
$char;
      }
      echo
"\n";
    }
    echo
'</PRE>';
  }
?>

Because we've used # for every character in there, all the pixels have the same, squarish shape. You're welcome to try merging that code with the black-and-white code of multiple symbols, but you'll soon see the problem - using symbol complexity to mimic lightness mixed with using actual colour lightness means the lightness is exaggerated, making the detail hard to see.









<< 22.5.2 Development   22.6 Further Reading >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
skonealone - 29 Aug 2008

this is nice article.. :-)

http://skonealone.co.nr



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow