Repository: mozilla/pdf.js
File: src/core/pattern.js (JavaScript)

Code before:
    let gPrev = rgbBuffer[1];
    let bPrev = rgbBuffer[2];

    // Slopes are rise / run.
    // A max slope is from the least value the base component could have been
    // to the greatest value the current component could have been.
    // A min slope is from the greatest value the base component could have been
    // to the least value the current component could have been.
    // Each component could have been rounded up to .5 from its original value
    // so the conservative deltas are +-1 (+-.5 for base and -+.5 for current).

    // The run is iPrev - iBase = 1, so omitted.
    let maxSlopeR = rPrev - rBase + 1;
    let maxSlopeG = gPrev - gBase + 1;
    let maxSlopeB = bPrev - bBase + 1;
    let minSlopeR = rPrev - rBase - 1;
    let minSlopeG = gPrev - gBase - 1;
    let minSlopeB = bPrev - bBase - 1;

    for (let i = 2; i < NUMBER_OF_SAMPLES; i++) {
      ratio[0] = t0 + i * step;
      fn(ratio, 0, color, 0);
      cs.getRgb(color, 0, rgbBuffer);
      const r = rgbBuffer[0];
      const g = rgbBuffer[1];
      const b = rgbBuffer[2];

      // Keep going if the maximum minimum slope <= the minimum maximum slope.
      // Otherwise add a rgbPrev color stop and make it the new base.

      const run = i - iBase;
      maxSlopeR = Math.min(maxSlopeR, (r - rBase + 1) / run);
      maxSlopeG = Math.min(maxSlopeG, (g - gBase + 1) / run);
      maxSlopeB = Math.min(maxSlopeB, (b - bBase + 1) / run);
      minSlopeR = Math.max(minSlopeR, (r - rBase - 1) / run);
      minSlopeG = Math.max(minSlopeG, (g - gBase - 1) / run);
      minSlopeB = Math.max(minSlopeB, (b - bBase - 1) / run);

      const slopesExist =
        minSlopeR <= maxSlopeR &&
        minSlopeG <= maxSlopeG &&
        minSlopeB <= maxSlopeB;

      if (!slopesExist) {
        const cssColor = Util.makeHexColor(rPrev, gPrev, bPrev);
        colorStops.push([iPrev / NUMBER_OF_SAMPLES, cssColor]);

        // TODO: When fn frequency is high (iPrev - iBase === 1 twice in a row),
        // send the color space and function to do the sampling display side.

        // The run is i - iPrev = 1, so omitted.

Review comment (suggestion):
Nit: Could we shorten this with destructuring?
```suggestion
      const [r, g, b] = rgbBuffer;
```

Code after:
    // A max slope is from the least value the base component could have been
    // to the greatest value the current component could have been.
    // A min slope is from the greatest value the base component could have been
    // to the least value the current component could have been.
    // Each component could have been rounded up to .5 from its original value
    // so the conservative deltas are +-1 (+-.5 for base and -+.5 for current).

    // The run is iPrev - iBase = 1, so omitted.
    let maxSlopeR = rPrev - rBase + 1;
    let maxSlopeG = gPrev - gBase + 1;
    let maxSlopeB = bPrev - bBase + 1;
    let minSlopeR = rPrev - rBase - 1;
    let minSlopeG = gPrev - gBase - 1;
    let minSlopeB = bPrev - bBase - 1;

    for (let i = 2; i < NUMBER_OF_SAMPLES; i++) {
      ratio[0] = t0 + i * step;
      fn(ratio, 0, color, 0);
      cs.getRgb(color, 0, rgbBuffer);
      const [r, g, b] = rgbBuffer;

      // Keep going if the maximum minimum slope <= the minimum maximum slope.
      // Otherwise add a rgbPrev color stop and make it the new base.

      const run = i - iBase;
      maxSlopeR = Math.min(maxSlopeR, (r - rBase + 1) / run);
      maxSlopeG = Math.min(maxSlopeG, (g - gBase + 1) / run);
      maxSlopeB = Math.min(maxSlopeB, (b - bBase + 1) / run);
      minSlopeR = Math.max(minSlopeR, (r - rBase - 1) / run);
      minSlopeG = Math.max(minSlopeG, (g - gBase - 1) / run);
      minSlopeB = Math.max(minSlopeB, (b - bBase - 1) / run);

      const slopesExist =
        minSlopeR <= maxSlopeR &&
        minSlopeG <= maxSlopeG &&
        minSlopeB <= maxSlopeB;

      if (!slopesExist) {
        const cssColor = Util.makeHexColor(rPrev, gPrev, bPrev);
        colorStops.push([iPrev / NUMBER_OF_SAMPLES, cssColor]);

        // TODO: When fn frequency is high (iPrev - iBase === 1 twice in a row),
        // send the color space and function to do the sampling display side.

        // The run is i - iPrev = 1, so omitted.
        maxSlopeR = r - rPrev + 1;
        maxSlopeG = g - gPrev + 1;
        maxSlopeB = b - bPrev + 1;
        minSlopeR = r - rPrev - 1;
        minSlopeG = g - gPrev - 1;
        minSlopeB = b - bPrev - 1;