spatie / color

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

HSLA -> RGBA looses transparency value #69

Closed jcogs-design closed 2 years ago

jcogs-design commented 2 years ago

I did this:

$colour = "hsla(331,96%,90%,0.76)"
$the_colour = @Factory::fromString($colour);

This creates a Spatie\Color\Hsla object with this value:

$the_colour: Spatie\Color\Hsla
 hue: 331
 saturation: 96
 lightness: 90
 alpha: 0.76

If I now convert this to RGBA thusly:

$the_rgba_colour = $the_colour->toRgba();

I get a Spatie\Color\Rgba object with this value:

$the_rgba_colour: Spatie\Color\Rgba
 red: 254
 blue: 205
 green: 229
 alpha: 1

The correct value of alpha in this case should be 0.76.

Does anyone know why I get this result? I don't think I'm doing anything wrong vis the library, but would value thoughts.

Use of Factory::fromString() rather than a specific colour format object is simply due to use case being considered one where it is uncertain what colour format string might be supplied.

Thanks in advance for anyone who can offer any insight etc.

jcogs-design commented 2 years ago

Answered my own question... you need to explicitly specify alpha even if it is already set... which seems both odd and unnecessary. Vis... to get my conversion to work I need to enter

$the_rgba_colour = $the_colour->toRgba($the_colour->alpha());

This works, but maybe the conversions for color formats that support transparency should check to see if an alpha value has already been set before using a default (if no value passed as a parameter)?

spatie-bot commented 2 years ago

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.