From d16d1f0d23ffb8c4c9102d590a7776b5a9958b08 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Sat, 29 Jul 2023 18:15:23 +0200 Subject: [PATCH] Add the color changes in the annotation storage --- src/display/annotation_layer.js | 10 +++++++--- src/shared/scripting_utils.js | 32 +++++++++++++++++++++++++----- test/integration/scripting_spec.js | 26 ++++++++++++++++++++---- test/integration/test_utils.js | 1 + 4 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index eca676aa0..65d276263 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -331,9 +331,13 @@ class AnnotationElement { get _commonActions() { const setColor = (jsName, styleName, event) => { const color = event.detail[jsName]; - event.target.style[styleName] = ColorConverters[`${color[0]}_HTML`]( - color.slice(1) - ); + const colorType = color[0]; + const colorArray = color.slice(1); + event.target.style[styleName] = + ColorConverters[`${colorType}_HTML`](colorArray); + this.annotationStorage.setValue(this.data.id, { + [styleName]: ColorConverters[`${colorType}_rgb`](colorArray), + }); }; return shadow(this, "_commonActions", { diff --git a/src/shared/scripting_utils.js b/src/shared/scripting_utils.js index 643922051..dbf2fddc0 100644 --- a/src/shared/scripting_utils.js +++ b/src/shared/scripting_utils.js @@ -26,6 +26,10 @@ function makeColorComp(n) { .padStart(2, "0"); } +function scaleAndClamp(x) { + return Math.max(0, Math.min(255, 255 * x)); +} + // PDF specifications section 10.3 class ColorConverters { static CMYK_G([c, y, m, k]) { @@ -40,6 +44,11 @@ class ColorConverters { return ["RGB", g, g, g]; } + static G_rgb([g]) { + g = scaleAndClamp(g); + return [g, g, g]; + } + static G_HTML([g]) { const G = makeColorComp(g); return `#${G}${G}${G}`; @@ -49,17 +58,22 @@ class ColorConverters { return ["G", 0.3 * r + 0.59 * g + 0.11 * b]; } - static RGB_HTML([r, g, b]) { - const R = makeColorComp(r); - const G = makeColorComp(g); - const B = makeColorComp(b); - return `#${R}${G}${B}`; + static RGB_rgb(color) { + return color.map(scaleAndClamp); + } + + static RGB_HTML(color) { + return `#${color.map(makeColorComp).join("")}`; } static T_HTML() { return "#00000000"; } + static T_rgb() { + return [null]; + } + static CMYK_RGB([c, y, m, k]) { return [ "RGB", @@ -69,6 +83,14 @@ class ColorConverters { ]; } + static CMYK_rgb([c, y, m, k]) { + return [ + scaleAndClamp(1 - Math.min(1, c + k)), + scaleAndClamp(1 - Math.min(1, m + k)), + scaleAndClamp(1 - Math.min(1, y + k)), + ]; + } + static CMYK_HTML(components) { const rgb = this.CMYK_RGB(components).slice(1); return this.RGB_HTML(rgb); diff --git a/test/integration/scripting_spec.js b/test/integration/scripting_spec.js index f92ebe917..887d8b7e7 100644 --- a/test/integration/scripting_spec.js +++ b/test/integration/scripting_spec.js @@ -20,6 +20,7 @@ const { getQuerySelector, getComputedStyleSelector, loadAndWait, + getFirstSerialized, } = require("./test_utils.js"); describe("Interaction", () => { @@ -751,10 +752,22 @@ describe("Interaction", () => { delay: 10, }); - for (const [id, propName, expected] of [ - [41, "backgroundColor", "rgb(255, 0, 0)"], - [43, "color", "rgb(0, 255, 0)"], - [44, "border-top-color", "rgb(0, 0, 255)"], + for (const [id, propName, storedName, expected, storedExpected] of [ + [ + 41, + "backgroundColor", + "backgroundColor", + "rgb(255, 0, 0)", + [255, 0, 0], + ], + [43, "color", "color", "rgb(0, 255, 0)", [0, 255, 0]], + [ + 44, + "border-top-color", + "borderColor", + "rgb(0, 0, 255)", + [0, 0, 255], + ], ]) { const current = await page.$eval( getSelector(ref), @@ -775,6 +788,11 @@ describe("Interaction", () => { propName ); expect(color).withContext(`In ${browserName}`).toEqual(expected); + + const storedValue = (await getFirstSerialized(page))[storedName]; + expect(storedValue) + .withContext(`In ${browserName}`) + .toEqual(storedExpected); } } }) diff --git a/test/integration/test_utils.js b/test/integration/test_utils.js index 18dbfa4cb..1ab98f67b 100644 --- a/test/integration/test_utils.js +++ b/test/integration/test_utils.js @@ -63,6 +63,7 @@ exports.clearInput = async (page, selector) => { await page.keyboard.press("A"); await page.keyboard.up("Control"); await page.keyboard.press("Backspace"); + await page.waitForTimeout(10); }; function getSelector(id) {