fix: rgbToHsl no longer mutates its input array by InukaWijerathna · Pull Request #1908 · TheAlgorithms/JavaScript · GitHub
Skip to content

fix: rgbToHsl no longer mutates its input array#1908

Open
InukaWijerathna wants to merge 1 commit into
TheAlgorithms:masterfrom
InukaWijerathna:fix/1907-rgb-to-hsl-mutation
Open

fix: rgbToHsl no longer mutates its input array#1908
InukaWijerathna wants to merge 1 commit into
TheAlgorithms:masterfrom
InukaWijerathna:fix/1907-rgb-to-hsl-mutation

Conversation

@InukaWijerathna

Copy link
Copy Markdown

Closes #1907.

rgbToHsl aliased its argument (let colorHsl = colorRgb) instead of copying it, then wrote the HSL result into that same array and returned it. Any caller that kept a reference to its RGB input had that array silently overwritten with the HSL output (input === result was true).

Fixes the alias to a copy ([...colorRgb]), per the fix suggested in the issue, and adds a regression test asserting the input array is left unchanged after conversion.

The existing HSL conversion math is untouched — only the in-place mutation is fixed.

rgbToHsl aliased its argument with colorHsl = colorRgb instead of
copying it, then overwrote colorHsl in place and returned that same
array, destroying the caller's original RGB values. Copy the input
with [...colorRgb] instead, and add a regression test asserting the
input is left unchanged.
@codecov-commenter

Copy link
Copy Markdown

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.

Conversions/RgbHslConversion: rgbToHsl mutates (and returns) its input array instead of a copy

2 participants