Jump to content

convert decimal to html hex


iwarlord

Recommended Posts

Hello. I hope someone here can help.

Basically I want to convert a truecolor number to html hex.

This javascript function does it:

 

      function decimalColorToHTMLcolor(number) {

          var intnumber = number - 0;

          var red, green, blue;

          var template = "#000000";

          red = (intnumber&0x0000ff) << 16;

          green = intnumber&0x00ff00;

          blue = (intnumber&0xff0000) >>> 16;

          intnumber = red|green|blue;

          var HTMLcolor = intnumber.toString(16); 

          HTMLcolor = template.substring(0,7 - HTMLcolor.length) + HTMLcolor;

          return HTMLcolor;

      }

 

But I need a PHP version and I don't know where to begin.

Any suggestions?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/116495-convert-decimal-to-html-hex/
Share on other sites

do you mean

<?php
$decColor = 16776960;
$HTMLcolor = sprintf ('#%06X', $decColor);
echo $HTMLcolor;                                     // #FFFF00
?>

 

(If $decColor is 255, dechex only gives FF, not 0000FF)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.