Skip to content

Commit 9d11b51

Browse files
committed
Replace css color rgb(...) by #...
* it's faster to generate the color code in using a table for components * it's very likely a way faster to parse (when setting the color in the canvas)
1 parent bf870bd commit 9d11b51

File tree

6 files changed

+15
-18
lines changed

6 files changed

+15
-18
lines changed

src/core/pattern.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,14 @@ Shadings.RadialAxial = (function RadialAxialClosure() {
205205
ratio[0] = t0 + i * step;
206206
fn(ratio, 0, color, 0);
207207
rgbColor = cs.getRgb(color, 0);
208-
var cssColor = Util.makeCssRgb(rgbColor[0], rgbColor[1], rgbColor[2]);
208+
var cssColor = Util.makeHexColor(rgbColor[0], rgbColor[1], rgbColor[2]);
209209
colorStops.push([i / NUMBER_OF_SAMPLES, cssColor]);
210210
}
211211

212212
var background = "transparent";
213213
if (dict.has("Background")) {
214214
rgbColor = cs.getRgb(dict.get("Background"), 0);
215-
background = Util.makeCssRgb(rgbColor[0], rgbColor[1], rgbColor[2]);
215+
background = Util.makeHexColor(rgbColor[0], rgbColor[1], rgbColor[2]);
216216
}
217217

218218
if (!extendStart) {

src/display/annotation_layer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class AnnotationElement {
221221
}
222222

223223
if (data.color) {
224-
container.style.borderColor = Util.makeCssRgb(
224+
container.style.borderColor = Util.makeHexColor(
225225
data.color[0] | 0,
226226
data.color[1] | 0,
227227
data.color[2] | 0
@@ -860,7 +860,7 @@ class PopupElement {
860860
const r = BACKGROUND_ENLIGHT * (255 - color[0]) + color[0];
861861
const g = BACKGROUND_ENLIGHT * (255 - color[1]) + color[1];
862862
const b = BACKGROUND_ENLIGHT * (255 - color[2]) + color[2];
863-
popup.style.backgroundColor = Util.makeCssRgb(r | 0, g | 0, b | 0);
863+
popup.style.backgroundColor = Util.makeHexColor(r | 0, g | 0, b | 0);
864864
}
865865

866866
const title = document.createElement("h1");

src/display/canvas.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1951,12 +1951,12 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
19511951
this.current.patternFill = true;
19521952
},
19531953
setStrokeRGBColor: function CanvasGraphics_setStrokeRGBColor(r, g, b) {
1954-
const color = Util.makeCssRgb(r, g, b);
1954+
const color = Util.makeHexColor(r, g, b);
19551955
this.ctx.strokeStyle = color;
19561956
this.current.strokeColor = color;
19571957
},
19581958
setFillRGBColor: function CanvasGraphics_setFillRGBColor(r, g, b) {
1959-
const color = Util.makeCssRgb(r, g, b);
1959+
const color = Util.makeHexColor(r, g, b);
19601960
this.ctx.fillStyle = color;
19611961
this.current.fillColor = color;
19621962
this.current.patternFill = false;

src/display/pattern_helper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ const TilingPattern = (function TilingPatternClosure() {
571571
current.strokeColor = ctx.strokeStyle;
572572
break;
573573
case PaintType.UNCOLORED:
574-
const cssColor = Util.makeCssRgb(color[0], color[1], color[2]);
574+
const cssColor = Util.makeHexColor(color[0], color[1], color[2]);
575575
context.fillStyle = cssColor;
576576
context.strokeStyle = cssColor;
577577
// Set color needed by image masks (fixes issues 3226 and 8741).

src/display/svg.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1049,15 +1049,15 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
10491049
}
10501050

10511051
setStrokeRGBColor(r, g, b) {
1052-
this.current.strokeColor = Util.makeCssRgb(r, g, b);
1052+
this.current.strokeColor = Util.makeHexColor(r, g, b);
10531053
}
10541054

10551055
setFillAlpha(fillAlpha) {
10561056
this.current.fillAlpha = fillAlpha;
10571057
}
10581058

10591059
setFillRGBColor(r, g, b) {
1060-
this.current.fillColor = Util.makeCssRgb(r, g, b);
1060+
this.current.fillColor = Util.makeHexColor(r, g, b);
10611061
this.current.tspan = this.svgFactory.createElement("svg:tspan");
10621062
this.current.xcoords = [];
10631063
this.current.ycoords = [];
@@ -1143,7 +1143,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
11431143
this.svg = bbox;
11441144
this.transformMatrix = matrix;
11451145
if (paintType === 2) {
1146-
const cssColor = Util.makeCssRgb(...color);
1146+
const cssColor = Util.makeHexColor(...color);
11471147
this.current.fillColor = cssColor;
11481148
this.current.strokeColor = cssColor;
11491149
}

src/shared/util.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -618,16 +618,13 @@ const IsEvalSupportedCached = {
618618
},
619619
};
620620

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

623625
class Util {
624-
// makeCssRgb() can be called thousands of times. Using ´rgbBuf` avoids
625-
// creating many intermediate strings.
626-
static makeCssRgb(r, g, b) {
627-
rgbBuf[1] = r;
628-
rgbBuf[3] = g;
629-
rgbBuf[5] = b;
630-
return rgbBuf.join("");
626+
static makeHexColor(r, g, b) {
627+
return `#${hexNumbers[r]}${hexNumbers[g]}${hexNumbers[b]}`;
631628
}
632629

633630
// Concatenates two transformation matrices together and returns the result.

0 commit comments

Comments
 (0)