Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect conversion from HEX to HSL* with this color. #56

Closed
overclokk opened this issue Nov 8, 2021 · 4 comments
Closed

Incorrect conversion from HEX to HSL* with this color. #56

overclokk opened this issue Nov 8, 2021 · 4 comments

Comments

@overclokk
Copy link
Contributor

Hi, I just found this issue trying to convert #DC3545 to an hsl value, the hue returned is -5.748502994012 instead of 354.

You can try this method inside FactoryTest::class:

    public function test_it_should_return_correct_hsa_value_from_hex() {

        $sut = Factory::fromString('#dc3545' );

	$this->assertStringMatchesFormat( '#dc3545', (string) $sut->toHex(), '' );
	$this->assertStringMatchesFormat( 'rgb(220,53,69)', (string) $sut->toRgb(), '' );
        $this->assertStringMatchesFormat( 'hsl(354,70%,54%)', (string) $sut->toHsl(), '' );
        $this->assertStringMatchesFormat( 'hsla(354,70%,54%,1)', (string) $sut->toHsla(), '' );
    }

This is part of the tests output:

There was 1 failure:

1) Spatie\Color\Test\FactoryTest::test_it_should_return_correct_hsa_value_from_hex
Failed asserting that string matches format description.
--- Expected
+++ Actual
@@ @@
-hsl(354,70%,54%)
+hsl(-6,70%,54%)

Maybe the issue is here

$hue = 60 * fmod(($g - $b) / $delta, 6);

The hex to rgb conversion is fine, the rgb value returned is 220, 53, 69, so it should be only the rgb to hsl in the line above.

@overclokk
Copy link
Contributor Author

I just found another conversion issue from hsl to hex or rgb but I think it's better to open another issue for that.

@horuskol
Copy link
Contributor

horuskol commented Nov 9, 2021

The maths used for HEX/RGB to HSL conversion does sometimes output a negative value for hue, but hsl(354, 70%, 54%) and hsl(-6, 70%, 54%) will produce the same shade of red on display. Because of the cyclic nature of hue, values like 714 and -366 would also be equivalent.

However, the hue could be normalised before being so that it is always between 0 and 360.

while ($hue < 0) {
    // can sometimes be negative
    $hue += 360;
}

while ($hue > 360) {
  // I don't think this is likely, but for balance
  $hue -= 360;
}

edit: I just saw that the markdown preview for hsl(-6, 70%, 54%) didn't work. Interesting. So far I've never had a problem with negative hue whenever I've used it.

@overclokk
Copy link
Contributor Author

I just created a pull-request with a fix for this issue, it simply normalizes the negative value for the hue.

freekmurze added a commit that referenced this issue Nov 9, 2021
@freekmurze
Copy link
Member

Thanks for the PR! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants