Skip to content

refactor (parseCssColor): use parseInt, avoid unnecessary coercion #20856

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

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions ext/console/01_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -2753,34 +2753,34 @@ const HSL_PATTERN = new SafeRegExp(
);

function parseCssColor(colorString) {
if (MapPrototypeHas(colorKeywords, colorString)) {
colorString = MapPrototypeGet(colorKeywords, colorString);
if (colorKeywords.has(colorString)) {
Copy link
Member

@lucacasonato lucacasonato Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI to other reviewers: safe because SafeMap, but may result in prefer-primordials linter errors. Let's see :)

EDIT: linter is happy :)

colorString = colorKeywords.get(colorString);
}
// deno-fmt-ignore
const hashMatch = StringPrototypeMatch(colorString, HASH_PATTERN);
if (hashMatch != null) {
return [
Number(`0x${hashMatch[1]}`),
Number(`0x${hashMatch[2]}`),
Number(`0x${hashMatch[3]}`),
NumberParseInt(hashMatch[1], 16),
NumberParseInt(hashMatch[2], 16),
NumberParseInt(hashMatch[3], 16),
];
}
// deno-fmt-ignore
const smallHashMatch = StringPrototypeMatch(colorString, SMALL_HASH_PATTERN);
if (smallHashMatch != null) {
return [
Number(`0x${smallHashMatch[1]}${smallHashMatch[1]}`),
Number(`0x${smallHashMatch[2]}${smallHashMatch[2]}`),
Number(`0x${smallHashMatch[3]}${smallHashMatch[3]}`),
NumberParseInt(`${smallHashMatch[1]}${smallHashMatch[1]}`, 16),
NumberParseInt(`${smallHashMatch[2]}${smallHashMatch[2]}`, 16),
NumberParseInt(`${smallHashMatch[3]}${smallHashMatch[3]}`, 16),
];
}
// deno-fmt-ignore
const rgbMatch = StringPrototypeMatch(colorString, RGB_PATTERN);
if (rgbMatch != null) {
return [
MathRound(MathMax(0, MathMin(255, Number(rgbMatch[1])))),
MathRound(MathMax(0, MathMin(255, Number(rgbMatch[2])))),
MathRound(MathMax(0, MathMin(255, Number(rgbMatch[3])))),
MathRound(MathMax(0, MathMin(255, rgbMatch[1]))),
MathRound(MathMax(0, MathMin(255, rgbMatch[2]))),
MathRound(MathMax(0, MathMin(255, rgbMatch[3]))),
];
}
// deno-fmt-ignore
Expand All @@ -2791,8 +2791,8 @@ function parseCssColor(colorString) {
if (h < 0) {
h += 360;
}
const s = MathMax(0, MathMin(100, Number(hslMatch[2]))) / 100;
const l = MathMax(0, MathMin(100, Number(hslMatch[3]))) / 100;
const s = MathMax(0, MathMin(100, hslMatch[2])) / 100;
const l = MathMax(0, MathMin(100, hslMatch[3])) / 100;
const c = (1 - MathAbs(2 * l - 1)) * s;
const x = c * (1 - MathAbs((h / 60) % 2 - 1));
const m = l - c / 2;
Expand Down