Skip to content

Commit

Permalink
Replace css color rgb(...) by #...
Browse files Browse the repository at this point in the history
* it's faster to generate the color code in using a table components
* it's very likely a way faster to parse (when setting the color in the canvas)
  • Loading branch information
calixteman committed Oct 30, 2020
1 parent bf870bd commit 00838a2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/core/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@ Shadings.RadialAxial = (function RadialAxialClosure() {
ratio[0] = t0 + i * step;
fn(ratio, 0, color, 0);
rgbColor = cs.getRgb(color, 0);
var cssColor = Util.makeCssRgb(rgbColor[0], rgbColor[1], rgbColor[2]);
var cssColor = Util.makeHexColor(rgbColor[0], rgbColor[1], rgbColor[2]);
colorStops.push([i / NUMBER_OF_SAMPLES, cssColor]);
}

var background = "transparent";
if (dict.has("Background")) {
rgbColor = cs.getRgb(dict.get("Background"), 0);
background = Util.makeCssRgb(rgbColor[0], rgbColor[1], rgbColor[2]);
background = Util.makeHexColor(rgbColor[0], rgbColor[1], rgbColor[2]);
}

if (!extendStart) {
Expand Down
4 changes: 2 additions & 2 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class AnnotationElement {
}

if (data.color) {
container.style.borderColor = Util.makeCssRgb(
container.style.borderColor = Util.makeHexColor(
data.color[0] | 0,
data.color[1] | 0,
data.color[2] | 0
Expand Down Expand Up @@ -860,7 +860,7 @@ class PopupElement {
const r = BACKGROUND_ENLIGHT * (255 - color[0]) + color[0];
const g = BACKGROUND_ENLIGHT * (255 - color[1]) + color[1];
const b = BACKGROUND_ENLIGHT * (255 - color[2]) + color[2];
popup.style.backgroundColor = Util.makeCssRgb(r | 0, g | 0, b | 0);
popup.style.backgroundColor = Util.makeHexColor(r | 0, g | 0, b | 0);
}

const title = document.createElement("h1");
Expand Down
4 changes: 2 additions & 2 deletions src/display/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -1951,12 +1951,12 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
this.current.patternFill = true;
},
setStrokeRGBColor: function CanvasGraphics_setStrokeRGBColor(r, g, b) {
const color = Util.makeCssRgb(r, g, b);
const color = Util.makeHexColor(r, g, b);
this.ctx.strokeStyle = color;
this.current.strokeColor = color;
},
setFillRGBColor: function CanvasGraphics_setFillRGBColor(r, g, b) {
const color = Util.makeCssRgb(r, g, b);
const color = Util.makeHexColor(r, g, b);
this.ctx.fillStyle = color;
this.current.fillColor = color;
this.current.patternFill = false;
Expand Down
2 changes: 1 addition & 1 deletion src/display/pattern_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ const TilingPattern = (function TilingPatternClosure() {
current.strokeColor = ctx.strokeStyle;
break;
case PaintType.UNCOLORED:
const cssColor = Util.makeCssRgb(color[0], color[1], color[2]);
const cssColor = Util.makeHexColor(color[0], color[1], color[2]);
context.fillStyle = cssColor;
context.strokeStyle = cssColor;
// Set color needed by image masks (fixes issues 3226 and 8741).
Expand Down
7 changes: 7 additions & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,9 @@ const IsEvalSupportedCached = {
};

const rgbBuf = ["rgb(", 0, ",", 0, ",", 0, ")"];
const hexNumbers = [...Array(256).keys()].map(n =>
n.toString(16).padStart(2, "0")
);

class Util {
// makeCssRgb() can be called thousands of times. Using ´rgbBuf` avoids
Expand All @@ -630,6 +633,10 @@ class Util {
return rgbBuf.join("");
}

static makeHexColor(r, g, b) {
return "#" + hexNumbers[r] + hexNumbers[g] + hexNumbers[b];
}

// Concatenates two transformation matrices together and returns the result.
static transform(m1, m2) {
return [
Expand Down

0 comments on commit 00838a2

Please sign in to comment.