Added custom colors support to the TypeScript input demo web page and program.

pull/118/head
Project Nayuki 3 years ago
parent 201993945d
commit f29c84a2e7

@ -97,6 +97,13 @@
<td>Scale:</td> <td>Scale:</td>
<td><input type="number" value="8" min="1" max="30" step="1" id="scale-input" style="width:4em"> pixels per module</td> <td><input type="number" value="8" min="1" max="30" step="1" id="scale-input" style="width:4em"> pixels per module</td>
</tr> </tr>
<tr>
<td>Colors:</td>
<td>
Light = <input type="text" value="#FFFFFF" id="light-color-input" style="width:6em">,
dark = <input type="text" value="#000000" id="dark-color-input" style="width:6em">
</td>
</tr>
<tr> <tr>
<td>Version range:</td> <td>Version range:</td>
<td> <td>

@ -29,7 +29,7 @@ namespace app {
function initialize(): void { function initialize(): void {
getElem("loading").style.display = "none"; getElem("loading").style.display = "none";
getElem("loaded").style.removeProperty("display"); getElem("loaded").style.removeProperty("display");
let elems = document.querySelectorAll("input[type=number], textarea"); let elems = document.querySelectorAll("input[type=number], input[type=text], textarea");
for (let el of elems) { for (let el of elems) {
if (el.id.indexOf("version-") != 0) if (el.id.indexOf("version-") != 0)
(el as any).oninput = redrawQrCode; (el as any).oninput = redrawQrCode;
@ -86,20 +86,24 @@ namespace app {
// Draw image output // Draw image output
const border: number = parseInt(getInput("border-input").value, 10); const border: number = parseInt(getInput("border-input").value, 10);
const lightColor: string = getInput("light-color-input").value;
const darkColor : string = getInput("dark-color-input" ).value;
if (border < 0 || border > 100) if (border < 0 || border > 100)
return; return;
if (bitmapOutput) { if (bitmapOutput) {
const scale: number = parseInt(getInput("scale-input").value, 10); const scale: number = parseInt(getInput("scale-input").value, 10);
if (scale <= 0 || scale > 30) if (scale <= 0 || scale > 30)
return; return;
drawCanvas(qr, scale, border, "#FFFFFF", "#000000", canvas); drawCanvas(qr, scale, border, lightColor, darkColor, canvas);
canvas.style.removeProperty("display"); canvas.style.removeProperty("display");
} else { } else {
const code: string = toSvgString(qr, border, "#FFFFFF", "#000000"); const code: string = toSvgString(qr, border, lightColor, darkColor);
const viewBox: string = (/ viewBox="([^"]*)"/.exec(code) as RegExpExecArray)[1]; const viewBox: string = (/ viewBox="([^"]*)"/.exec(code) as RegExpExecArray)[1];
const pathD: string = (/ d="([^"]*)"/.exec(code) as RegExpExecArray)[1]; const pathD: string = (/ d="([^"]*)"/.exec(code) as RegExpExecArray)[1];
svg.setAttribute("viewBox", viewBox); svg.setAttribute("viewBox", viewBox);
(svg.querySelector("path") as Element).setAttribute("d", pathD); (svg.querySelector("path") as Element).setAttribute("d", pathD);
(svg.querySelector("rect") as Element).setAttribute("fill", lightColor);
(svg.querySelector("path") as Element).setAttribute("fill", darkColor);
svg.style.removeProperty("display"); svg.style.removeProperty("display");
svgXml.value = code; svgXml.value = code;
} }

Loading…
Cancel
Save