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.

FunctionReturnsNotes
getSolidColor(color, fallback?)stringOne color that stands for a ColorConfig: the string, a solid's color, a gradient's first stop, an image fill's underlay, else fallback ("#000000")
getContrastRatio(a, b)numberThe WCAG contrast ratio between two CSS colors, 1 to 21. See-through colors are composited over white first. Returns 1 when a color cannot be parsed
getContrastLevel(ratio){ level, warning, message }AAA from 7, AA from 4.5, AA Large from 3 (with warning: true), Fail below 3
getLuminance(r, g, b)numberRelative luminance of an sRGB color, 0 to 1
hexToRgb(hex){ r, g, b } | nullReads 3, 4, 6 and 8 digit hex, dropping alpha. null for anything else
validateSize(width, height){ isValid, error? }The rule every download applies: 50 to 5000 px and square
SUPPORTS_PATH2DbooleanWhether this environment's canvas supports Path2D with addPath

Colors

lib/colors.ts
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"); // null

Use 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

lib/contrast.ts
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); // 1

The 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:

components/contrast-checker.tsx
"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

lib/sizes.ts
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

lib/features.ts
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) and normalizeColorConfig(color) turn the forms of ColorConfig into one another; isGradient(color) and isImagePaint(color) tell them apart.
  • sanitizeFrameText(text) and isLatin1Text(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, for createQRArtImage, createQRSilhouette or scanQRPixels in Node.
  • rgbToCmyk(color) is the conversion the CMYK print files use.

Was this page helpful?

Last updated on

On this page