Hello.

I convert a hex colour code to RGB using this code;

    $Red = hexdec(substr($_POST['colour'], 0, 2));
    $Green = hexdec(substr($_POST['colour'], 2, 2));
    $Blue = hexdec(substr($_POST['colour'], 4, 2));

The problem i am having is trying to change it back. I nearly got it, but not quite.

The 000's get stored as just a single 0, and 004 as 4 etc. This resulted in '00FF00' getting changed back as '0FF0' which isnt correct.

I tried to fix the problem with something like the following;

$OneHex = DecHex(str_pad($myrow['Red'],3,"0"));
$TwoHex = DecHex(str_pad($myrow['Green'],3,"0"));
$ThreeHex = DecHex(str_pad($myrow['Blue'],3,"0"));

But i end up with the same as before.

Any ideas?
Thanks!

    Instead of:

    $OneHex = DecHex(str_pad($myrow['Red'],3,"0"));
    $TwoHex = DecHex(str_pad($myrow['Green'],3,"0"));
    $ThreeHex = DecHex(str_pad($myrow['Blue'],3,"0"));

    try using:

    <?php
    $color = "0F06DD";

    echo ($Red = hexdec(substr($color, 0, 2))) . " : red dec pad <br>";
    echo ($Green = hexdec(substr($color, 2, 2))) . " : green dec pad <br>";
    echo ($Blue = hexdec(substr($color, 4, 2))) . " : blue dec pad <br>";

    echo ($OneHex = str_pad( dechex($Red) , 3, "0", STR_PAD_LEFT) ) . " : red hex pad <br>";
    echo ($TwoHex = str_pad( dechex($Green), 3, "0", STR_PAD_LEFT) ) . " : green hex pad <br>";
    echo ($ThreeHex = str_pad( dechex($Blue), 3, "0", STR_PAD_LEFT) ) . " : blue hex pad <br>";

    ?>

    -Geoffrey

      Thanks.

      Just changed the '3' to a '2' as it has already been hexed.

      Thanks again!

        Write a Reply...