Color: class refactor and add types#33996
Open
vorobey wants to merge 3 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors the internal Color implementation used across DevExtreme (via @ts/m_color and the deprecated @js/color re-export) into a TypeScript class with improved typing, and updates call sites to align with the refactor.
Changes:
- Switched palette color extrapolation logic to use
@ts/m_colorandColor.fromHSL(...)static API. - Updated ColorBox’s
ColorViewvalidation logic to useColor.isValid*static methods instead of instance methods. - Refactored
m_color.tsfrom a function/prototype pattern to a typedColorclass, adding type annotations to parsing/conversion helpers.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/devextreme/js/__internal/viz/palette.ts | Migrates palette extrapolation to @ts/m_color and the new fromHSL call style. |
| packages/devextreme/js/__internal/ui/color_box/color_view.ts | Updates alpha/hex/rgb validation to use static Color.isValid* methods. |
| packages/devextreme/js/__internal/m_color.ts | Reworks Color implementation into a typed class and updates parsing/conversion utilities. |
Comment on lines
+568
to
+592
| static isValidHex(hex: string): boolean { | ||
| return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(hex); | ||
| }, | ||
| } | ||
|
|
||
| isValidRGB(r, g, b) { | ||
| if (!isIntegerBetweenMinAndMax(r) || !isIntegerBetweenMinAndMax(g) || !isIntegerBetweenMinAndMax(b)) { | ||
| static isValidRGB( | ||
| r: number | undefined, | ||
| g: number | undefined, | ||
| b: number | undefined, | ||
| ): boolean { | ||
| if ( | ||
| !isIntegerBetweenMinAndMax(r) | ||
| || !isIntegerBetweenMinAndMax(g) | ||
| || !isIntegerBetweenMinAndMax(b) | ||
| ) { | ||
| return false; | ||
| } | ||
| return true; | ||
| }, | ||
| } | ||
|
|
||
| isValidAlpha(a) { | ||
| static isValidAlpha(a: number): boolean { | ||
| if (isNaN(a) || a < 0 || a > 1 || typeof a !== 'number') { | ||
| return false; | ||
| } | ||
| return true; | ||
| }, | ||
|
|
||
| colorIsInvalid: false, | ||
| } |
Comment on lines
594
to
+606
| @@ -598,12 +603,9 @@ Color.prototype = { | |||
| color.b = rgb[2]; | |||
|
|
|||
| return color; | |||
| }, | |||
| }; | |||
|
|
|||
| interface ColorConstructor { | |||
| prototype: ColorInstance; | |||
| new(value?: string): ColorInstance; | |||
| } | |||
| { | ||
| re: /^#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/, | ||
| process(colorString) { | ||
| process(colorString):[number, number, number, number] { |
bc312c2 to
8504c6a
Compare
8504c6a to
f607300
Compare
Comment on lines
+476
to
+480
| function isIntegerBetweenMinAndMax(number: number | undefined, min = 0, max = 255): boolean { | ||
| if (typeof number !== 'number' | ||
| || number % 1 !== 0 | ||
| || number < min | ||
| || number > max |
Comment on lines
+576
to
+580
| static isValidRGB( | ||
| r: number | undefined, | ||
| g: number | undefined, | ||
| b: number | undefined, | ||
| ): boolean { |
| { | ||
| re: /^#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/, | ||
| process(colorString) { | ||
| process(colorString):[number, number, number, number] { |
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.
No description provided.