spatie / color

A little library to deal with color conversions
https://spatie.be/opensource/php
MIT License
350 stars 36 forks source link

Converting Hex to Hsl for #000 & #fff gets division by zero #41

Closed hodashirzad closed 3 years ago

hodashirzad commented 3 years ago

Hi there

First of all thank you very much for this package as it is very useful, unfortunately when I try to convert the pure black or white colours it fails with division by zero error:

Hex::fromString('#000000')->toHsl()
Hex::fromString('#ffffff')->toHsl()

I think the following fix will sort this issue out:

In src/Convert.php line 77:

Replace this line:

            $saturation = $delta / (1 - abs((2 * $lightness) - 1));

with the following:

        if( $lightness > 0 && $lightness < 1) {
            $saturation = $delta / (1 - abs((2 * $lightness) - 1));
        }else {
            $saturation = 0;
        }
Nielsvanpach commented 3 years ago

Released in 1.2.4