Skip to content
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

Avoid calling Math.pow if possible. #11963

Merged
merged 1 commit into from
Jun 7, 2020
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
15 changes: 12 additions & 3 deletions src/core/colorspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,15 @@ const CalRGBCS = (function CalRGBCSClosure() {
if (color <= 0.0031308) {
return adjustToRange(0, 1, 12.92 * color);
}
// Optimization:
// If color is close enough to 1, skip calling the following transform
// since calling Math.pow is expensive. If color is larger than
// the threshold, the final result is larger than 254.5 since
// ((1 + 0.055) * 0.99554525 ** (1 / 2.4) - 0.055) * 255 ===
// 254.50000003134699
if (color >= 0.99554525) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add a comment above this line, explaining exactly how this value was determined!
As-is, this line looks quite wrong until you've actually read #11963 (comment)

return 1;
}
return adjustToRange(0, 1, (1 + 0.055) * color ** (1 / 2.4) - 0.055);
}

Expand Down Expand Up @@ -1156,9 +1165,9 @@ const CalRGBCS = (function CalRGBCSClosure() {
// A <---> AGR in the spec
// B <---> BGG in the spec
// C <---> CGB in the spec
const AGR = A ** cs.GR;
const BGG = B ** cs.GG;
const CGB = C ** cs.GB;
const AGR = A === 1 ? 1 : A ** cs.GR;
const BGG = B === 1 ? 1 : B ** cs.GG;
const CGB = C === 1 ? 1 : C ** cs.GB;

// Computes intermediate variables L, M, N as per spec.
// To decode X, Y, Z values map L, M, N directly to them.
Expand Down