Last active
December 31, 2022 16:39
-
-
Save mrmolaei/f75ddfdf36432389b07f9759151646db to your computer and use it in GitHub Desktop.
Convert Hex to HSL in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// To avoid the complexity, I use two separate functions. | |
function hexToHsl($hex) | |
{ | |
// Convert hex to RGB | |
if (strlen($hex) == 7) { | |
$rgb = array_map('hexdec', str_split(ltrim($hex, '#'), 2)); | |
} else { | |
$hex = '#' . implode( "", array_map( function($digit) { | |
return str_repeat($digit, 2); | |
}, str_split(ltrim($hex, '#'), 1)) ); | |
$rgb = array_map('hexdec', str_split(ltrim($hex, '#'), 2)); | |
} | |
// Convert RGB to HSL | |
$hsl = rgbToHsl($rgb[0], $rgb[1], $rgb[2]); | |
$hsl[0] = round($hsl[0], 1); | |
$hsl[1] = round($hsl[1]) * 100 . "%"; | |
$hsl[2] = round($hsl[2] * 100) . "%"; | |
$output = 'hsl(' . implode( ", ", $hsl ) . ')'; | |
return $output; | |
} | |
function rgbToHsl($r, $g, $b) | |
{ | |
// Normalize the RGB values | |
$r /= 255; | |
$g /= 255; | |
$b /= 255; | |
// Find the minimum and maximum RGB values | |
$min = min($r, $g, $b); | |
$max = max($r, $g, $b); | |
// Calculate the hue | |
if ($max == $min) { | |
$h = 0; | |
} else if ($max == $r) { | |
$h = 60 * ($g - $b) / ($max - $min); | |
} else if ($max == $g) { | |
$h = 60 * ($b - $r) / ($max - $min) + 120; | |
} else { | |
$h = 60 * ($r - $g) / ($max - $min) + 240; | |
} | |
// Normalize the hue to be between 0 and 360 | |
if ($h < 0) { | |
$h += 360; | |
} else if ($h > 360) { | |
$h -= 360; | |
} | |
// Calculate the saturation | |
if ($max == 0) { | |
$s = 0; | |
} else { | |
$s = ($max - $min) / $max; | |
} | |
// Calculate the lightness | |
$l = ($max + $min) / 2; | |
// Return the HSL values as an array | |
return array($h, $s, $l); | |
} | |
echo hexToHsl( "#fff" ); // hsl(0, 0%, 100%) | |
echo hexToHsl( "#ffffff" ); // hsl(0, 0%, 100%) | |
echo hexToHsl( "#999" ); // hsl(0, 0%, 60%) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment