Ancient Temple City

Convert hex color to RGB values in PHP

Question:

What would be a good way to convert hex color values like #ffffff into the single RGB values 255 255 255 using PHP?


Solution:

Example 1:
Check out PHP's hexdec() and dechex() functions.

<?php
$value = hexdec('ff'); // $value = 255
?>


Example 2:
If you want to convert hex to rgb you can use sscanf.

<?php
$hex = "#ff9900";
list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
echo "$hex -> $r $g $b"; // #ff9900 -> 255 153 0
?>
Labels:
Reactions:

Post a Comment

[blogger]

MKRdezign

{facebook#http://www.facebook.com/GTVCreations} {twitter#http://www.twitter.com/GTVCreations} {google-plus#http://www.google.com} {pinterest#http://www.pinterest.com} {youtube#http://www.youtube.com} {instagram#http://www.instagram.com}

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget