From 7acb112ca9f3ab3143a1a8ae82d824b0c59bc8d1 Mon Sep 17 00:00:00 2001 From: Takashi Tamura Date: Tue, 2 Jun 2020 22:26:39 +0900 Subject: [PATCH] Optimization: Avoid calling Math.pow if possible when calculating the transfer function of the CalRGB color space since calling Math.pow is expensive. If the value of color is larger than the threshold, 0.99554525, the final result of the transform is larger that 254.5 since ((1 + 0.055) * 0.99554525 ** (1 / 2.4) - 0.055) * 255 === 254.50000003134699 --- src/core/colorspace.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/core/colorspace.js b/src/core/colorspace.js index b4f326d57..dbd63a26c 100644 --- a/src/core/colorspace.js +++ b/src/core/colorspace.js @@ -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) { + return 1; + } return adjustToRange(0, 1, (1 + 0.055) * color ** (1 / 2.4) - 0.055); } @@ -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.