From 201993945d632ab87363790d19594fe022f6b79f Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Wed, 28 Jul 2021 18:01:41 +0000 Subject: [PATCH] In Java and TypeScript versions, added parameters for custom module colors when rendering to SVG. --- .../nayuki/qrcodegen/QrCodeGeneratorDemo.java | 18 +++++++++++------- typescript-javascript/qrcodegen-input-demo.ts | 8 ++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/java/src/main/java/io/nayuki/qrcodegen/QrCodeGeneratorDemo.java b/java/src/main/java/io/nayuki/qrcodegen/QrCodeGeneratorDemo.java index 44f85fd..eb7c96e 100644 --- a/java/src/main/java/io/nayuki/qrcodegen/QrCodeGeneratorDemo.java +++ b/java/src/main/java/io/nayuki/qrcodegen/QrCodeGeneratorDemo.java @@ -62,9 +62,9 @@ public final class QrCodeGeneratorDemo { File imgFile = new File("hello-world-QR.png"); // File path for output ImageIO.write(img, "png", imgFile); // Write image to file - String svg = toSvgString(qr, 4); // Convert to SVG XML code - File svgFile = new File("hello-world-QR.svg"); // File path for output - Files.write(svgFile.toPath(), // Write image to file + String svg = toSvgString(qr, 4, "#FFFFFF", "#000000"); // Convert to SVG XML code + File svgFile = new File("hello-world-QR.svg"); // File path for output + Files.write(svgFile.toPath(), // Write image to file svg.getBytes(StandardCharsets.UTF_8)); } @@ -217,12 +217,16 @@ public final class QrCodeGeneratorDemo { * number of border modules. The string always uses Unix newlines (\n), regardless of the platform. * @param qr the QR Code to render (not {@code null}) * @param border the number of border modules to add, which must be non-negative + * @param lightColor the color to use for light modules, in any format supported by CSS, not {@code null} + * @param darkColor the color to use for dark modules, in any format supported by CSS, not {@code null} * @return a string representing the QR Code as an SVG XML document - * @throws NullPointerException if the QR Code is {@code null} + * @throws NullPointerException if any object is {@code null} * @throws IllegalArgumentException if the border is negative */ - private static String toSvgString(QrCode qr, int border) { + private static String toSvgString(QrCode qr, int border, String lightColor, String darkColor) { Objects.requireNonNull(qr); + Objects.requireNonNull(lightColor); + Objects.requireNonNull(darkColor); if (border < 0) throw new IllegalArgumentException("Border must be non-negative"); long brd = border; @@ -231,7 +235,7 @@ public final class QrCodeGeneratorDemo { .append("\n") .append(String.format("\n", qr.size + brd * 2)) - .append("\t\n") + .append("\t\n") .append("\t\n") + .append("\" fill=\"" + darkColor + "\"/>\n") .append("\n") .toString(); } diff --git a/typescript-javascript/qrcodegen-input-demo.ts b/typescript-javascript/qrcodegen-input-demo.ts index d2d6a81..f9a027a 100644 --- a/typescript-javascript/qrcodegen-input-demo.ts +++ b/typescript-javascript/qrcodegen-input-demo.ts @@ -95,7 +95,7 @@ namespace app { drawCanvas(qr, scale, border, "#FFFFFF", "#000000", canvas); canvas.style.removeProperty("display"); } else { - const code: string = toSvgString(qr, border); + const code: string = toSvgString(qr, border, "#FFFFFF", "#000000"); const viewBox: string = (/ viewBox="([^"]*)"/.exec(code) as RegExpExecArray)[1]; const pathD: string = (/ d="([^"]*)"/.exec(code) as RegExpExecArray)[1]; svg.setAttribute("viewBox", viewBox); @@ -170,7 +170,7 @@ namespace app { // Returns a string of SVG code for an image depicting the given QR Code, with the given number // of border modules. The string always uses Unix newlines (\n), regardless of the platform. - function toSvgString(qr: qrcodegen.QrCode, border: number): string { + function toSvgString(qr: qrcodegen.QrCode, border: number, lightColor: string, darkColor: string): string { if (border < 0) throw "Border must be non-negative"; let parts: Array = []; @@ -183,8 +183,8 @@ namespace app { return ` - - + + ` }