interpim Posted April 1, 2007 Share Posted April 1, 2007 Im trying to create a simple script to display EVERY hexadecimal color code in a table then make the background of each table cell that color, basically to give myself a reference to look at when I am looking for a particular color. I have the idea how I want to do it, but... I don't know how to get PHP to count in hexadecimal. for instance, how do i declare a number as being Hex, and how do i force all of the mathematical functions to add in Hex? Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 1, 2007 Share Posted April 1, 2007 dechex() Quote Link to comment Share on other sites More sharing options...
interpim Posted April 1, 2007 Author Share Posted April 1, 2007 I know how to convert from decimal to hex... i want to do math in hex Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 1, 2007 Share Posted April 1, 2007 How is it ?? <?php function dz($r) { if($r<=9) { $r = (string)($r); $r = '0'.$r; } return $r; } $jump = 50; $top = 0; for($r=0;$r<=255;$r+=$jump) { for($g=0;$g<=255;$g+=$jump) { for($b=0;$b<=255;$b+=$jump) { $color = '#'.dz(dechex($r)).dz(dechex($g)).dz(dechex($b)); $div = '<div style="position: absolute; height: 22px; background-color:'.$color.'; left:173px; top:'.$top.'px; border: 1px solid #5050B4";" id="color_block'.dz($r).dz($g).dz($b).'">'.$color.'</div>'; $top += 25; echo $div."<br />\n"; } } } ?> Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 1, 2007 Share Posted April 1, 2007 No Try This Its More Modified Decrease $jump To gect More Colors -------------------------------- <?php function dz($r) { if(hexdec($r)<=9) { $r = (string)($r); $r = '0'.$r; } return $r; } $jump = 50; $top = 0; for($r=0;$r<=255;$r+=$jump) { for($g=0;$g<=255;$g+=$jump) { for($b=0;$b<=255;$b+=$jump) { $color = '#'.dz(dechex($r)).dz(dechex($g)).dz(dechex($b)); $div = '<div style="position: absolute; height: 22px; background-color:'.$color.'; left:173px; top:'.$top.'px; border: 1px solid #5050B4";" id="color_block'.dz($r).dz($g).dz($b).'">'.$color.'</div>'; $top += 25; echo $div."<br />\n"; } } } ?> Quote Link to comment Share on other sites More sharing options...
interpim Posted April 1, 2007 Author Share Posted April 1, 2007 Im looking for something like this... http://www.bmk.com.au/cgi-bin/colors.php When i drop $jump in your code, some of the numbers are dropping some of the trailing 0's and basically only showing the "short" number Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 1, 2007 Share Posted April 1, 2007 Some of the Numbers are dropping in the first Code. Please use the second Code. <?php function dz($r) { if(hexdec($r)<=9) { $r = (string)($r); $r = '0'.$r; } return $r; } $jump = 25; $top = 0; for($r=0;$r<=255;$r+=$jump) { for($g=0;$g<=255;$g+=$jump) { for($b=0;$b<=255;$b+=$jump) { $color = '#'.dz(dechex($r)).dz(dechex($g)).dz(dechex($b)); $div = '<div style="position: absolute; height: 22px; background-color:'.$color.'; left:173px; top:'.$top.'px; border: 1px solid #5050B4";" id="color_block'.dz($r).dz($g).dz($b).'">'.$color.'</div>'; $top += 25; echo $div."<br />\n"; } } } ?> Both of this site and this code is doing the samething The Site is using Table and i am using div You can change Th size of the divs Quote Link to comment Share on other sites More sharing options...
interpim Posted April 1, 2007 Author Share Posted April 1, 2007 what is the ? Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 1, 2007 Share Posted April 1, 2007 Its The Spaces. You should Replace those by spaces When I Posted Php freaks Converted spaces to  . Find All   and replace by <space> Quote Link to comment Share on other sites More sharing options...
Barand Posted April 1, 2007 Share Posted April 1, 2007 As an alternative you can also use sprintf() to construct the hex color values. As another variation on a theme this uses floating divs instead of positioning with absolute values (change the "51" to "17" for a chart like the sample site) <html> <head> <style type='text/css'> div { width: 50; height: 18; font-size: 8pt; font-family: arial; text-align: center; float: left; } </style> </head> <body> <?php for ($r=0; $r<256; $r+=51) { for ($g=0; $g<256; $g+=51) { for ($b=0; $b<256; $b+=17) { $color = sprintf('%02X%02X%02X', $r,$g,$b); $tcol = ($r+$g+$g)/3 > 160 ? '#000' : '#FFF'; // make text legible on light backgrounds echo "<div style='background-color:#$color; color:$tcol'>$color</div>\n"; } echo '<br style="clear: both" />'; } } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.