I want to have a single field in my database that holds an Integer value. I want to then convert the integer value i get from the database into a Hex color. I don't want to get into the RGB stuff, because it would be unnecessary i believe. I simply want to take a Hex Color, convert it to a decimal, and then allow the decimal value to be transformed back into it's hex value.

I'm not sure how to do this, and I've tried several methods. I usually end up with

bgcolor="#0"

or

bgcolor="#FF"

neither do what their supposed to.. can anybody help me?

    WHAT exactly holds the integer???

    i would use a 9 digit integer - 3 x 3 digits - and each 3 digit group stands for a value of each color (RG😎 - and each 3 digit group must be a number between 0 and 255 - ie -

    $colRgb = "127233015";
    

    the groups are: 127, 233, 015

    then you pull them out with the substring() function:

    you get 3 numbers as substrings...

    $col[r] = substr($colRgb, 0, 3);
    $col[g] = substr($colRgb, 2, 3);
    $col[b] = substr($colRgb, 5, 3);
    

    then you convert them into the hex code - check http://www.php.net/manual/en/function.dechex.php for that -

    $col[r] = dechex($col[r]);
    $col[g] = dechex($col[g]);
    $col[b] = dechex($col[b]);
    

    and then join the 3 hexadecimal values.

    $colHex = "#" . "$col[r]" . "$col[g]" . "$col[b]";

    the quick version of all the above would be:

    $colHex = "#" . dechex(substr($colRgb, 0, 3)) . dechex(substr($colRgb, 2, 3)) . dechex(substr($colRgb, 5, 3));

    that should give you the corresponding hex color value - which i am too lazy to convert for the example now...

    finally:

    print "<body bgcolor=\"" . "$colHex" . "\">";

    should print the right code...

    hope this helps - is what you wanted - and works ;-)

      I am thinking, that because White in hex is FFFFFF. in decimal FFFFFF is 16777215 and Green would be 00FF00 in hex and 65280 in decimal. 16711680 would be red, ect.. There should be a way to convert from one to the other, and maintain optimum efficiency. without seperating the data into three seperate integers, but simply converting a decimal value into a hex value.

        aaah... now i gotcha...

        $colRgb = "65280";
        

        then you convert it into the hex code - check http://www.php.net/manual/en/function.dechex.php for that -

        $colHex = dechex($colDec);
        

        possible problem: leading zeros... so... what we do: check for the string length - solution: append as many zeros on the left side of the int -> now string, so the length eventually is 6:

        if (strlen($colHex) < 6) {
            $diff = 6 - strlen($colHex);
            for ($i = 0; $i < $diff; $i++) {
                $colHex = "0" . "$colHex";
            }
        }
        

        and thats it... any questions?

        $colHex = "#" . "$colHex";

        finally:

        print "<body bgcolor=\"" . "$colHex" . "\">";

        should print the right code...

        is that what you wanted?

        sid

          Thanks!, from what I read, it's exactly what i'm looking for. Unfortunatly my host is having some problems, and i can't connect to my site for some reason.. :mad: but as soon as I get access again, I'll get it working.

            no problem!

            let me know if it worked! - and dont worry - youre not the only one with provider problems 😉

              yeah, it works, my host is working again. just had to send a message to support. Thanx a lot. My Themes work correcty now instead of giving me black!

                Write a Reply...