-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
util: add hex colors support in styleText
#61556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #61556 +/- ##
==========================================
- Coverage 89.79% 89.78% -0.01%
==========================================
Files 672 672
Lines 203756 203814 +58
Branches 39169 39189 +20
==========================================
+ Hits 182956 183000 +44
+ Misses 13135 13129 -6
- Partials 7665 7685 +20
🚀 New features to boost your workflow:
|
ljharb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add test cases for invalid values.
| * @param {string} format | ||
| * @returns {[number, number, number] | undefined} | ||
| */ | ||
| function parseHexColor(format) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't see anything throwing when the 0th character isn't #
| const r = hexToNibble(format.charCodeAt(1)); | ||
| const g = hexToNibble(format.charCodeAt(2)); | ||
| const b = hexToNibble(format.charCodeAt(3)); | ||
|
|
||
| if ((r | g | b) < 0) return; | ||
|
|
||
| // Duplicate nibbles: #RGB -> #RRGGBB | ||
| return [(r << 4) | r, (g << 4) | g, (b << 4) | b]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const r = hexToNibble(format.charCodeAt(1)); | |
| const g = hexToNibble(format.charCodeAt(2)); | |
| const b = hexToNibble(format.charCodeAt(3)); | |
| if ((r | g | b) < 0) return; | |
| // Duplicate nibbles: #RGB -> #RRGGBB | |
| return [(r << 4) | r, (g << 4) | g, (b << 4) | b]; | |
| const x = ArrayPrototypeSlice(format, 1, 2); | |
| const y = ArrayPrototypeSlice(format, 2, 3); | |
| const z = ArrayPrototypeSlice(format, 3, 4); | |
| if (NumberIsFinite(x) && x >= 0 && NumberIsFinite(y) && y >= 0 && NumberIsFinite(z) && z >= 0) { | |
| const r = ParseInt(x + x, 16); | |
| const g = ParseInt(y + y, 16) | |
| const b = ParseInt(z + z, 16); | |
| return [r, g, b]; | |
| } | |
| throw new RangeError('Invalid three digit hex value'); |
| const r1 = hexToNibble(format.charCodeAt(1)); | ||
| const r2 = hexToNibble(format.charCodeAt(2)); | ||
| const g1 = hexToNibble(format.charCodeAt(3)); | ||
| const g2 = hexToNibble(format.charCodeAt(4)); | ||
| const b1 = hexToNibble(format.charCodeAt(5)); | ||
| const b2 = hexToNibble(format.charCodeAt(6)); | ||
|
|
||
| if ((r1 | r2 | g1 | g2 | b1 | b2) < 0) return; | ||
|
|
||
| return [(r1 << 4) | r2, (g1 << 4) | g2, (b1 << 4) | b2]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const r1 = hexToNibble(format.charCodeAt(1)); | |
| const r2 = hexToNibble(format.charCodeAt(2)); | |
| const g1 = hexToNibble(format.charCodeAt(3)); | |
| const g2 = hexToNibble(format.charCodeAt(4)); | |
| const b1 = hexToNibble(format.charCodeAt(5)); | |
| const b2 = hexToNibble(format.charCodeAt(6)); | |
| if ((r1 | r2 | g1 | g2 | b1 | b2) < 0) return; | |
| return [(r1 << 4) | r2, (g1 << 4) | g2, (b1 << 4) | b2]; | |
| const x = ArrayPrototypeSlice(format, 1, 3); | |
| const y = ArrayPrototypeSlice(format, 3, 5); | |
| const z = ArrayPrototypeSlice(format, 5); | |
| if (NumberIsFinite(x) && x >= 0 && NumberIsFinite(y) && y >= 0 && NumberIsFinite(z) && z >= 0) { | |
| const r = ParseInt(x, 16); | |
| const g = ParseInt(y, 16) | |
| const b = ParseInt(z, 16); | |
| return [r, g, b]; | |
| } | |
| throw new RangeError('Invalid six digit hex value'); |
| const hashtagCode = 35 | ||
|
|
||
| /** | ||
| * Parses a hex color in #RGB or #RRGGBB form. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only a personal opinion, but I feel like there's no significant advantage over just accepting #RRGGBB here. It would also mean that all this machinery could just be replaced with a single hex-to-Buffer encode step, eg.
const RGB_RE = /^#[0-9A-F]{6}$/i;
// ...
if (RegExpPrototypeTest(RGB_RE, key)) {
const rgb = Buffer.from(StringPrototypeSlice(key, 1), 'hex');
ArrayPrototypePush(codes, [`38;2;${rgb[0]};${rgb[1]};${rgb[2]}`, 39]);
continue;
}
Fixes #61543