Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert hex color to RGB values in PHP

Tags:

php

hex

colors

rgb

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

like image 748
user123_456 Avatar asked Mar 04 '13 12:03

user123_456


People also ask

How to convert hex to rgb in PHP?

$output = sprintf('%06x', 0xffffff - hexdec(ltrim($input, '#')); however that's probably overly simplified and you'll probably want to analyse the RGB components separately, please explain exactly what it is you are wanting to do.


1 Answers

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

Output:

#ff9900 -> 255 153 0 
like image 53
John Avatar answered Sep 26 '22 21:09

John