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

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.91%. Comparing base (5c39e87) to head (e1dc94f).

Additional details and impacted files
@@ Coverage Diff @@## master #1908 +/- ##
=======================================
Coverage 85.91% 85.91% =======================================
Files 379 379 Lines 19778 19778 Branches 3016 3015 -1 =======================================
Hits 16993 16993 Misses 2785 2785 

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for freeto 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

3 participants

@InukaWijerathna@codecov-commenter@anandfresh