React library
Utilities
The color, contrast, size validation and feature detection helpers qrdx exports, with their signatures, return values and an example of each.
These helpers are the small functions the renderer and the scan score use, exported for your own UI: picking a representative color, checking contrast, validating a download size and detecting canvas features. They run anywhere except where noted.
Colors
import { getSolidColor, hexToRgb } from "qrdx";
getSolidColor({
type: "linear",
angle: 45,
stops: [
{ color: "#ff6b6b", offset: 0 },
{ color: "#4ecdc4", offset: 100 },
],
});
// "#ff6b6b": the first stop
getSolidColor({ type: "image", src: photo, underlay: "#111111" }); // "#111111"
getSolidColor(undefined, "#ffffff"); // "#ffffff"
hexToRgb("#ff6b6b"); // { r: 255, g: 107, b: 107 }
hexToRgb("#f00"); // { r: 255, g: 0, b: 0 }
hexToRgb("red"); // nullUse getSolidColor where you need one color for a swatch, a CSS background or a fallback. The renderer uses it where a format cannot draw a gradient, such as QRCodeCanvas and the Canvas frame drawing.
Contrast
import { getContrastLevel, getContrastRatio, getLuminance } from "qrdx";
getContrastRatio("#000000", "#ffffff"); // 21
getContrastRatio("#1d4ed8", "#f8fafc"); // 6.4
getContrastRatio("rgba(29, 78, 216, 0.5)", "#ffffff"); // the blue over white, so lower
getContrastLevel(4.5);
// { level: "AA", warning: false, message: "Good contrast" }
getContrastLevel(2);
// { level: "Fail", warning: true, message: "Poor contrast - QR code may not scan properly" }
getLuminance(255, 255, 255); // 1The scan score passes a code at a ratio of 4.5 or more (SCAN_CONTRAST_MIN), takes points off below it and fails it below 1.5. Pictures need more: SCAN_IMAGE_CONTRAST_MIN is 7 for an image fill in the modules and 12 for one behind them. getScanContrast(props) runs the score's own check on a whole design, every gradient stop and eye color included; see Frames, scan score and print files.
A live contrast checker takes a dozen lines:
"use client";
import { getContrastLevel, getContrastRatio } from "qrdx";
import { useState } from "react";
export function ContrastChecker() {
const [fgColor, setFgColor] = useState("#000000");
const [bgColor, setBgColor] = useState("#ffffff");
const ratio = getContrastRatio(fgColor, bgColor);
const level = getContrastLevel(ratio);
return (
<div>
<input
aria-label="Module color"
type="color"
value={fgColor}
onChange={(event) => setFgColor(event.target.value)}
/>
<input
aria-label="Background color"
type="color"
value={bgColor}
onChange={(event) => setBgColor(event.target.value)}
/>
<p>
{ratio.toFixed(2)}:1, {level.level}. {level.message}
</p>
</div>
);
}Validation
import { validateSize } from "qrdx";
validateSize(1024, 1024); // { isValid: true }
validateSize(40, 40); // { isValid: false, error: "Size must be at least 50x50 pixels" }
validateSize(6000, 6000); // { isValid: false, error: "Size must not exceed 5000x5000 pixels" }
validateSize(400, 300); // { isValid: false, error: "Width and height must be equal for QR codes" }downloadQRCode, getQRCodeDataUrl and copyQRCodeToClipboard throw the same error text when a size fails, so a form can check it first.
Feature detection
import { SUPPORTS_PATH2D } from "qrdx";
if (SUPPORTS_PATH2D) {
// draw module paths with Path2D
}SUPPORTS_PATH2D is computed once when the module loads, so it is false on a server.
Other helpers
getColorString(color)andnormalizeColorConfig(color)turn the forms ofColorConfiginto one another;isGradient(color)andisImagePaint(color)tell them apart.sanitizeFrameText(text)andisLatin1Text(text)apply the frame label rules (24 characters; Latin-1 for PDF and EPS).decodePNGPixels(bytes)reads an 8-bit PNG into RGBA pixels with no DOM, forcreateQRArtImage,createQRSilhouetteorscanQRPixelsin Node.rgbToCmyk(color)is the conversion the CMYK print files use.
Related
Was this page helpful?
Last updated on
Library API reference
Every entry point, component, function, constant and type the qrdx package exports, with types and defaults. For HTTP endpoints, see the REST API.
QR detection
Read and decode QR codes from images in the browser with handleImageLoad, get the code's position, and score an image with getImageScanScore.