Move the hexNumbers Array into Util, to enable using it in the viewer

This reduces some code duplication, and the new `Util.hexNums` property is now computed lazily.
This commit is contained in:
Jonas Jenwald 2026-04-26 12:05:12 +02:00
parent 1591ddfa8d
commit 9b238b9719
5 changed files with 18 additions and 25 deletions

View File

@ -17,7 +17,6 @@ import {
AnnotationEditorPrefix, AnnotationEditorPrefix,
assert, assert,
BaseException, BaseException,
hexNumbers,
makeArr, makeArr,
objectSize, objectSize,
stringToPDFString, stringToPDFString,
@ -697,7 +696,7 @@ function stringToUTF16HexString(str) {
const buf = []; const buf = [];
for (let i = 0, ii = str.length; i < ii; i++) { for (let i = 0, ii = str.length; i < ii; i++) {
const char = str.charCodeAt(i); const char = str.charCodeAt(i);
buf.push(hexNumbers[(char >> 8) & 0xff], hexNumbers[char & 0xff]); buf.push(Util.hexNums[(char >> 8) & 0xff], Util.hexNums[char & 0xff]);
} }
return buf.join(""); return buf.join("");
} }

View File

@ -157,7 +157,7 @@ class IccColorSpace extends ColorSpace {
}); });
isUsable = !!this._module; isUsable = !!this._module;
QCMS._memory = this._module.memory; QCMS._memory = this._module.memory;
QCMS._makeHexColor = Util.makeHexColor; QCMS._makeHexColor = Util.makeHexColor.bind(Util);
} catch (e) { } catch (e) {
warn(`ICCBased color space: "${e}".`); warn(`ICCBased color space: "${e}".`);
} }

View File

@ -355,9 +355,7 @@ class BasicColorPicker {
input.type = "color"; input.type = "color";
if (hasAlpha) { if (hasAlpha) {
input.setAttribute("alpha", ""); input.setAttribute("alpha", "");
const alphaHex = Math.round((opacity ?? 1) * 255) const alphaHex = Util.hexNums[Math.round((opacity ?? 1) * 255)];
.toString(16)
.padStart(2, "0");
input.value = (color || "#000000") + alphaHex; input.value = (color || "#000000") + alphaHex;
} else { } else {
input.value = color || "#000000"; input.value = color || "#000000";
@ -399,9 +397,7 @@ class BasicColorPicker {
} }
if (this.#hasAlpha) { if (this.#hasAlpha) {
// Reconstruct #RRGGBBAA using the editor's current opacity. // Reconstruct #RRGGBBAA using the editor's current opacity.
const alphaHex = Math.round(this.#editor.opacity * 255) const alphaHex = Util.hexNums[Math.round(this.#editor.opacity * 255)];
.toString(16)
.padStart(2, "0");
this.#input.value = value + alphaHex; this.#input.value = value + alphaHex;
} else { } else {
this.#input.value = value; this.#input.value = value;
@ -413,9 +409,7 @@ class BasicColorPicker {
return; return;
} }
// Reconstruct #RRGGBBAA using the editor's current color. // Reconstruct #RRGGBBAA using the editor's current color.
const alphaHex = Math.round(value * 255) const alphaHex = Util.hexNums[Math.round(value * 255)];
.toString(16)
.padStart(2, "0");
this.#input.value = this.#editor.color + alphaHex; this.#input.value = this.#editor.color + alphaHex;
} }

View File

@ -688,13 +688,17 @@ class FeatureTest {
} }
} }
const hexNumbers = Array.from(Array(256).keys(), n =>
n.toString(16).padStart(2, "0")
);
class Util { class Util {
static get hexNums() {
return shadow(
this,
"hexNums",
Array.from(Array(256).keys(), n => n.toString(16).padStart(2, "0"))
);
}
static makeHexColor(r, g, b) { static makeHexColor(r, g, b) {
return `#${hexNumbers[r]}${hexNumbers[g]}${hexNumbers[b]}`; return `#${this.hexNums[r]}${this.hexNums[g]}${this.hexNums[b]}`;
} }
static domMatrixToTransform(dm) { static domMatrixToTransform(dm) {
@ -1316,7 +1320,6 @@ export {
getModificationDate, getModificationDate,
getUuid, getUuid,
getVerbosityLevel, getVerbosityLevel,
hexNumbers,
ImageKind, ImageKind,
info, info,
InvalidPDFException, InvalidPDFException,

View File

@ -91,11 +91,6 @@ class AnnotationEditorParams {
let currentInkColor = "#000000"; let currentInkColor = "#000000";
let currentInkOpacity = 1; let currentInkOpacity = 1;
const toAlphaHex = opacity =>
Math.round(opacity * 255)
.toString(16)
.padStart(2, "0");
editorInkColor.addEventListener("input", function () { editorInkColor.addEventListener("input", function () {
// The returned value format varies by browser; normalize it. // The returned value format varies by browser; normalize it.
const rgba = getRGBA(this.value); const rgba = getRGBA(this.value);
@ -111,11 +106,13 @@ class AnnotationEditorParams {
updateInkColor = value => { updateInkColor = value => {
currentInkColor = value; currentInkColor = value;
editorInkColor.value = currentInkColor + toAlphaHex(currentInkOpacity); const alphaHex = Util.hexNums[Math.round(currentInkOpacity * 255)];
editorInkColor.value = currentInkColor + alphaHex;
}; };
updateInkOpacity = value => { updateInkOpacity = value => {
currentInkOpacity = value; currentInkOpacity = value;
editorInkColor.value = currentInkColor + toAlphaHex(currentInkOpacity); const alphaHex = Util.hexNums[Math.round(currentInkOpacity * 255)];
editorInkColor.value = currentInkColor + alphaHex;
}; };
} else { } else {
editorInkColor.addEventListener("input", function () { editorInkColor.addEventListener("input", function () {