Color Converter for PHP
Free online color converter with PHP code examples
Working with color converter in PHP? Our free online color converter helps PHP developers format, validate, and process data instantly. Below you will find PHP code examples using Built-in functions so you can achieve the same result programmatically in your own projects.
Try the Color Converter Online
Use our free Color Converter directly in your browser — no setup required.
Open Color ConverterPHP Code Example
<?php
// HEX to RGB
$hex = '#3498db';
list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
echo "RGB: ($r, $g, $b)\n";
// RGB to HSL
$r /= 255; $g /= 255; $b /= 255;
$max = max($r, $g, $b); $min = min($r, $g, $b);
$l = ($max + $min) / 2;
if ($max == $min) { $h = $s = 0; }
else {
$d = $max - $min;
$s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min);
if ($max == $r) $h = (($g - $b) / $d + ($g < $b ? 6 : 0)) / 6;
elseif ($max == $g) $h = (($b - $r) / $d + 2) / 6;
else $h = (($r - $g) / $d + 4) / 6;
}
printf("HSL: (%.0f, %.0f%%, %.0f%%)\n", $h*360, $s*100, $l*100);
// RGB to HEX
echo "HEX: " . sprintf("#%02x%02x%02x", $r*255, $g*255, $b*255);Quick Setup
Library: Built-in functions
// No installation neededPHP Tips & Best Practices
- sscanf with %02x parses hex color values
- PHP has no built-in color conversion functions
- spatie/color package provides a clean API for color conversions