Title: Conversions/RgbHslConversion: rgbToHsl mutates (and returns) its input array instead of a copy · Issue #1907 · TheAlgorithms/JavaScript · GitHub
Open Graph Title: Conversions/RgbHslConversion: rgbToHsl mutates (and returns) its input array instead of a copy · Issue #1907 · TheAlgorithms/JavaScript
X Title: Conversions/RgbHslConversion: rgbToHsl mutates (and returns) its input array instead of a copy · Issue #1907 · TheAlgorithms/JavaScript
Description: Description Conversions/RgbHslConversion.js::rgbToHsl mutates its input array. It aliases the argument with let colorHsl = colorRgb (no copy) and then overwrites colorHsl[0..2] in place, so the caller's RGB array is destroyed and replace...
Open Graph Description: Description Conversions/RgbHslConversion.js::rgbToHsl mutates its input array. It aliases the argument with let colorHsl = colorRgb (no copy) and then overwrites colorHsl[0..2] in place, so the cal...
X Description: Description Conversions/RgbHslConversion.js::rgbToHsl mutates its input array. It aliases the argument with let colorHsl = colorRgb (no copy) and then overwrites colorHsl[0..2] in place, so the cal...
Opengraph URL: https://github.com/TheAlgorithms/JavaScript/issues/1907
X: @github
Domain: Github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Conversions/RgbHslConversion: rgbToHsl mutates (and returns) its input array instead of a copy","articleBody":"**Description**\n\n`Conversions/RgbHslConversion.js::rgbToHsl` mutates its input array. It aliases the argument with `let colorHsl = colorRgb` (no copy) and then overwrites `colorHsl[0..2]` in place, so the caller's RGB array is destroyed and replaced with the HSL result.\n\n```js\nconst rgbToHsl = (colorRgb) =\u003e {\n ...\n let colorHsl = colorRgb // \u003c- alias, NOT a copy\n ...\n colorHsl[0] = red / limit // mutates caller's array\n ...\n colorHsl[0] = Math.round(hue) // final HSL written back into the input array\n colorHsl[1] = Math.round(saturation * 100)\n colorHsl[2] = Math.round(luminance * 100)\n return colorHsl\n}\n```\n\nThe returned value is the *same array object* as the input, so a caller that still needs its RGB values loses them.\n\n**To Reproduce** (verified with Node):\n\n```js\nconst input = [24, 98, 118]\nconst result = rgbToHsl(input)\n// result -\u003e [193, 66, 28] (correct HSL)\n// input -\u003e [193, 66, 28] \u003c- the caller's RGB array was overwritten\n// input === result -\u003e true \u003c- same object, not a copy\n```\n\n**Expected behavior**\n\n`rgbToHsl` returns a new HSL array and leaves the input RGB array unchanged (a conversion function should not have side effects on its argument).\n\n**Actual behavior**\n\nThe input array is mutated in place and is returned as the result (`input === result`), so the original RGB data is destroyed.\n\n**Suggested fix**\n\nWork on a copy instead of aliasing the input:\n\n```js\nlet colorHsl = [...colorRgb]\n```\n\nThe HSL math itself is correct; only the in-place mutation of the argument needs fixing.\n","author":{"url":"https://github.com/aimasteracc","@type":"Person","name":"aimasteracc"},"datePublished":"2026-06-18T02:21:21.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1907/JavaScript/issues/1907"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0eceb651-6d1e-041c-a0ef-5ef1b57f4a21 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BBDC:32514C:462581:5EF1AB:6A627642 |
| html-safe-nonce | 3968bccebb202622079b38cfafd7ccf4c62e9e0ec27853e539bc06d7e54959bf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQkRDOjMyNTE0Qzo0NjI1ODE6NUVGMUFCOjZBNjI3NjQyIiwidmlzaXRvcl9pZCI6Ijc5NTc5NTg2MTAzMzY3NzM2OTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7674b902c39b615588eb346316ddf3749473d47603a2e012abb4388aaa35db7d |
| hovercard-subject-tag | issue:4688584356 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/TheAlgorithms/JavaScript/1907/issue_layout |
| twitter:image | https://opengraph.githubassets.com/066f1780e1285a400634a5204dd3e2820e5894f63e97f0e2c190cd56d7872c37/TheAlgorithms/JavaScript/issues/1907 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/066f1780e1285a400634a5204dd3e2820e5894f63e97f0e2c190cd56d7872c37/TheAlgorithms/JavaScript/issues/1907 |
| og:image:alt | Description Conversions/RgbHslConversion.js::rgbToHsl mutates its input array. It aliases the argument with let colorHsl = colorRgb (no copy) and then overwrites colorHsl[0..2] in place, so the cal... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aimasteracc |
| hostname | github.com |
| expected-hostname | github.com |
| None | 838e87bd0e4f5415707a930bc14e89e382dc2ea046e50ff575252eb2f2cb06a2 |
| turbo-cache-control | no-preview |
| go-import | github.com/TheAlgorithms/JavaScript git https://github.com/TheAlgorithms/JavaScript.git |
| octolytics-dimension-user_id | 20487725 |
| octolytics-dimension-user_login | TheAlgorithms |
| octolytics-dimension-repository_id | 97086543 |
| octolytics-dimension-repository_nwo | TheAlgorithms/JavaScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 97086543 |
| octolytics-dimension-repository_network_root_nwo | TheAlgorithms/JavaScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f0081192f88523d2bf4cfdf22bb3e138affc4cd1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width