Refactor (packages/ui/src/theme/color.ts): Extract RGB gamut check - #56
Open
magdang wants to merge 1 commit into
Open
Refactor (packages/ui/src/theme/color.ts): Extract RGB gamut check#56magdang wants to merge 1 commit into
magdang wants to merge 1 commit into
Conversation
Extract the RGB gamut check used in the chroma-reduction loop and add tests for in-gamut and out-of-gamut fitOklch behavior.
magdang
marked this pull request as draft
September 3, 2026 20:18
magdang
marked this pull request as ready for review
September 3, 2026 20:39
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #52
Link to the associated GitHub issue:
#52
Full path to the refactored file:
packages/ui/src/theme/color.tsWhat do you think this file does?
This file provides color utilities for the UI theme system, including color conversion and adjustment while ensuring generated colors remain within the valid RGB gamut.
What is the scope of your refactoring within that file?
I refactored the RGB gamut check inside the chroma-reduction loop in
fitOklchand extracted it intoisRgbInGamut.Which Qlty-reported issue did you address?
I addressed the Complex binary expression reported in
fitOklch. Before the refactor, Qlty reported two Complex binary expression issues incolor.ts, and this refactor removed the targeted occurrence.Type of change
What does this PR do?
This refactors the RGB gamut check in
fitOklch's chroma-reduction loop intoisRgbInGamut.The helper uses the minimum and maximum RGB channel values to determine whether the output is within the valid
0–1range. I added tests covering an in-gamut colour and an out-of-gamut colour whose chroma must be reduced.How did the specific issue you chose impact the codebase’s maintainability?
The inline RGB gamut condition mixed validation logic with the chroma-reduction algorithm, making
fitOklchharder to read and reason about.What changes did you make to resolve the issue?
I extracted the RGB gamut condition into the
isRgbInGamuthelper, which uses the minimum and maximum RGB channel values to check whether all channels are within the valid0–1range.How do your changes improve maintainability? Did you consider alternatives?
The helper gives the condition a clear name and separates RGB validation from the chroma-reduction logic, making
fitOklcheasier to understand and test. I considered leaving the condition inline, but extracting it reduces the complex expression without changing the existing behavior.How did you verify your code works?
How did you validate that the change is correct?
I added tests for both an in-gamut colour and an out-of-gamut colour whose chroma must be reduced. I also ran the UI tests, coverage, CI, and Qlty before and after the refactor to confirm the behavior remained correct and the targeted smell was removed.
Focused coverage test:
The coverage report includes
src/theme/color.tsand executes the changed gamut-check path.Assignment validation evidence
Q4.1 — Qlty smell reduction
Before and after screenshots of:
The screenshots show the selected smell before the refactor and the reduced/removed result afterward.
Before:
After:
Q4.2 — Local lint and tests
Commands run:
bun lint returns errors unrelated to my changes
Q4.3 — Coverage
The added
packages/ui/src/theme/color.test.tscallsfitOklchfor both in-gamut and out-of-gamut inputs. The latter executes the chroma-reduction loop and verifies the resulting RGB values are in gamut.Q4.4 — CI
The CI unit-test workflow runs
bun turbo test, which includes the UI package test command and discoverscolor.test.ts.Screenshots / recordings
No visible UI behaviour changed. The validation screenshots are included above.
Checklist