Jump to content

hexadecimal in PHP


interpim

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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";
          }
      }
  }
?>

Link to comment
Share on other sites

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";
          }
      }
  }
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.