1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

PHP Hex Color Chart


2 of 3

Creating the Chart
PHP Hex Color Chart
To actually create the chart we start with all three values (R, G, B) set to 0. Through a series of three while loops we proceed to add 3 to each color value and convert to hexadecimal giving us 00, 33, 66, 99, CC, and FF values for each color. Using the three loops this will cycle though all 216 web safe color combinations before exiting the loops and closing the table. The code below is commented to help you understand what it is doing.


 //This is our first loop, changing the R value
 while ($r <= 15)
 echo "<tr>"; 

//Our second loop (G value) occurs 6 times for ever R value, 36 times $g=0; while ($g <= 15) { echo "<tr>";

//Our third loop (B value) occurs 6 times for ever G value, or 216 times $b=0; while ($b <= 15) {

//Here we actually generate the color blocks $background = dechex($r) . dechex($r) . dechex($g) .dechex($g) . dechex($b) . dechex ($b); echo "<td bgcolor=\"#$background\">#$background</td>";

//At the end of each loop we add 3 $b = $b+3; } $g = $g+3; } $r = $r+3; } ?> </table>

Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Step By Steps
  5. Generate a PHP Chart of Hex Colors - PHP Web Color Chart

©2014 About.com. All rights reserved.