Skip to content

Clamp negative alpha values in AlphaTrait - #53

Open
gaoflow wants to merge 1 commit into
ozdemirburak:masterfrom
gaoflow:fix-alpha-lower-bound-clamp
Open

Clamp negative alpha values in AlphaTrait#53
gaoflow wants to merge 1 commit into
ozdemirburak:masterfrom
gaoflow:fix-alpha-lower-bound-clamp

Conversation

@gaoflow

@gaoflow gaoflow commented Jul 28, 2026

Copy link
Copy Markdown

AlphaTrait::alpha() clamps only the upper bound (min((float) $alpha, 1.0)), so a negative value passes through and then serializes to a string the class's own constructor rejects. Round-trip breaks for all three alpha types:

(new Rgba('10,20,30,0.5'))->alpha(-0.3);    // rgba(10,20,30,-0.3)          -> InvalidColorException
(new Hsla('120,50%,50%,0.5'))->alpha(-0.3); // hsla(120,50%,50%,-0.3)       -> InvalidColorException
(new Hexa('112233ff'))->alpha(-0.3);        // #112233ffffffffffffffb3      -> InvalidColorException

The sibling setters (red/green/blue, hue, saturation, lightness) already guard both bounds; alpha() only guarded the upper one. This clamps the lower bound as well, keeping the existing silent upper clamp. Added boundary tests across Rgba/Hsla/Hexa: negative clamps to 0 and round-trips, >1 still clamps to 1, in-range values pass through unchanged.

The alpha() setter clamped only the upper bound, so a negative value
passed through and serialized to a string the constructor rejects
(Rgba/Hsla, and garbage hex for Hexa). Clamp the lower bound too, as
the sibling red/green/hue/saturation/lightness setters already do.
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

Successfully merging this pull request may close these issues.

1 participant