Skip to content

Commit

Permalink
Enhance precision in HSL to HEX conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Oct 8, 2021
1 parent 6d07aa3 commit 9125524
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/Mexitek/PHPColors/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ public static function hslToHex(array $hsl = array()): string

$var_1 = 2 * $L - $var_2;

$r = round(255 * self::hueToRgb($var_1, $var_2, $H + (1 / 3)));
$g = round(255 * self::hueToRgb($var_1, $var_2, $H));
$b = round(255 * self::hueToRgb($var_1, $var_2, $H - (1 / 3)));
$r = 255 * self::hueToRgb($var_1, $var_2, $H + (1 / 3));
$g = 255 * self::hueToRgb($var_1, $var_2, $H);
$b = 255 * self::hueToRgb($var_1, $var_2, $H - (1 / 3));
}

// Convert to hex
$r = dechex((int)$r);
$g = dechex((int)$g);
$b = dechex((int)$b);
$r = dechex(round($r));
$g = dechex(round($g));
$b = dechex(round($b));

// Make sure we get 2 digits for decimals
$r = (strlen("" . $r) === 1) ? "0" . $r : $r;
Expand Down
2 changes: 1 addition & 1 deletion tests/colorConvertHslToHex.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $colorsToConvert = array(
'hsl' => $black,
],
'grey' => [
'hex' => 'a5a5a5',
'hex' => 'a6a6a6',
'hsl' => $grey,
],
'white' => [
Expand Down

0 comments on commit 9125524

Please sign in to comment.