Handlr

Customise

Making a card theme

How the share cards are painted, and how to code, test and ship a new card theme โ€” a palette, or a whole art style like Pixel Pup.

On this page

A card theme is the look a generated card is drawn in: its background, its ink, its accent and its separators. You pick one in the Theme row on Share. This page is for writing a new one. It is a code change: a theme is Python, not a setting.

It is not the app theme (how every page looks, on Settings โ†’ Look). That is a CSS file; see App themes. The two systems follow the same rules but share no code.

What a theme paints

Card Themed?
The share card, every kind, shape (story 1080ร—1920, square 1080ร—1080, wide 1200ร—628) and layout (poster, dense, checkin) Yes
The Kennel Card's toy box Yes
The session recap card No, always midnight

Some things on a card stay the same whatever the theme:

Where the code lives

File What it holds
app/handlr/core/theme.py The Theme dataclass, every shipped theme, THEMES, and the shared painters: ground() (background), rule() (separators), badge_mark()
app/handlr/core/share.py The card layouts. They read colours off the theme (th.ink, th.accentโ€ฆ) and never hard-code one
app/handlr/core/pixelart.py Pixel letters and sprites for the Pixel Pup style
tests/test_theme.py The guards. A new theme is checked by all of them automatically

Registering a theme in THEMES wires up everything else: the chip on Share (in dict order, showing your blurb), the ?theme= URL parameter, the saved-preset validator and the post-text preview. No template, route or JavaScript needs touching.

The fields

@dataclass(frozen=True)
class Theme:
    name: str        # the id in URLs and saved presets: lowercase, permanent
    label: str       # the chip text on Share
    blurb: str       # one line, shown under the chips
    dark: bool       # True for a dark ground; badge marks pick their plate from it
    top: tuple       # ground colour at y=0
    bottom: tuple    # ground colour at y=H (also the check-in panel and toy-box tiles)
    glows: tuple     # soft radial lights, see below
    ink: tuple       # main text
    muted: tuple     # secondary text (footer wordmark, "UNLOCKED")
    dim: tuple       # tertiary text (labels, dates, details)
    line: tuple      # hairline separators and tile outlines. Never text
    accent: tuple    # the headline word, "EARNED", highlights
    accent2: tuple   # the second accent
    metal: tuple     # (light, dark) pair for rules and rings
    rarity: str = "" # the badge metal it came from; non-empty gives rule() a lit left third

Colours are (r, g, b) tuples, 0โ€“255.

glows is a tuple of (cx, cy, radius, rgb, peak). cx and radius are fractions of the card's width, cy a fraction of its height, and peak (0โ€“255) is the strength at the centre. Because everything is a fraction, one theme looks the same on every shape. Centres may sit off the canvas (cy = -0.10 lights the top edge from above). Keep glow colours close to the ground: the legibility test measures text against top and bottom only, so a bright glow can make text hard to read where the test doesn't look.

Three ways to make one

A badge metal: one line

Every theme except midnight and plate comes from one helper, _from_metal(). It takes a rarity from BADGE_METAL and works out all twelve colours. All five rarities already have a theme, so this route only opens if the badge forge gains a rarity. That starts in tools/badges/badgeforge/chassis.py; see the warning below. Then:

_from_metal("obsidian", "Obsidian", "Black glass. The mythic badge.", "mythic"),

A hand-authored dark theme

Write every colour yourself and add the theme to the THEMES tuple in theme.py:

EMBER = Theme(
    name="ember", label="Ember", dark=True,
    blurb="Banked coals: rust ground, orange light.",
    top=(30, 14, 12), bottom=(16, 8, 8),
    glows=((0.20, -0.10, 0.65, (70, 24, 14), 140),),
    ink=(246, 236, 230), muted=(206, 170, 150), dim=(176, 140, 124), line=(70, 36, 30),
    accent=(255, 120, 60), accent2=(240, 170, 90), metal=((255, 170, 90), (200, 80, 40)),
)

THEMES: dict[str, Theme] = {t.name: t for t in (
    MIDNIGHT,
    # โ€ฆ the metal themes โ€ฆ
    PLATE,
    EMBER,
)}

This example passes every floor below with room to spare: ink 16.1:1, dim 6.1:1, accent 7.1:1.

A light theme

plate is the model. A light ground needs dark ink, muted, dim and accent. Copying a dark theme's inks onto a cream ground is the most common mistake, and it shows up as white text on a white card. The legibility test exists to catch it. Set dark=False so badge marks draw their pale plate, not the dark one.

The rules the tests enforce

Run tests/test_theme.py after any change. Each rule below is a test, and each one runs against every theme in THEMES.

Legibility. Each text colour must reach a minimum contrast ratio (WCAG) against both ends of the theme's own ground:

Field Floor
ink 7.0 : 1
muted 4.5 : 1
dim 3.5 : 1
accent, accent2 3.0 : 1

line is not checked, because it is never used for text. To check a colour while you work, use theme.contrast(fg, bg).

Semantic colours are off-limits. theme.tones() swaps in the theme's decorative colours (ink โ€ฆ accent2) and keeps ok, bad, warn, ice and cyan exactly as stamp.py sets them. Those colours carry meaning. Added time is red and removed time is green, which is the opposite of what most people expect, and a theme that recoloured them would make the card say the wrong thing. There is no field for them on purpose. Don't add one.

midnight must not change. It is the default, and it draws the card that existed before themes did, pixel for pixel. One test checks its ground against the original code. Another checks that no theme, an empty theme and an unknown theme all render the same bytes as midnight. If you change ground() or rule(), any new branch must leave a plain theme alone. The if th.rarity: and if th.pixel: branches are written that way.

A theme's name is permanent. It goes into share URLs and into saved presets. If a name is renamed or removed, theme.get() quietly falls back to midnight, and every preset and link that used it changes look without any error. You can change label and blurb freely.

Every theme renders everywhere. One test draws every theme ร— every shape ร— every layout with a real photo, and checks that a check-in card really drew full-bleed, not the Dense fallback.

The badge metals are copied by hand on purpose

BADGE_METAL repeats tools/badges/badgeforge/chassis.py::RARITY because tools/ is not in the Docker image, so an import would work on a dev machine and fail in production. Change the forge first, then copy the values exactly. test_badge_metals_match_the_forge compares them field by field.

Beyond colour: an art style

A palette changes colours. An art style changes how things are drawn. The Pixel Pup theme (pixel) is the example: scanlines and a double pixel frame on the ground, dashed rules, the headline in the dashboard's 5ร—7 pixel letters, and the pup sprite in the footer.

The pattern:

  1. Add one flag to Theme with a default that turns it off (pixel: bool = False). Every existing theme keeps its exact pixels without being edited.
  2. Branch in the shared painters, not in each layout. ground() paints the ramp and glows as normal and then calls _pixel_frame() when the flag is set. rule() returns early with its dashed version. Each layout calls these painters, so every card picks up the style at once. Drawing that belongs to one spot on one layout gets its own function instead. share._pixel_pup() places the pup to the right of the headline clock, or else centred in the footer, or else not at all, and records the box it used in the probe as info["pup"]. An ornament with no room is left out, never squeezed in. ๐Ÿ”ด Check the check-in card by eye. Its ground is the photograph and it has no rule, so it calls neither shared painter: a style that lives only in ground() and rule() is a colour swap there. _draw_checkin branches on the flag itself (the frame without scanlines, a square panel, the pup standing on the panel).
  3. Stay inside the margin. Each shape has a margin M (56โ€“72 px). Anything drawn at the edge, such as the frame, must fit inside it. frame_px() sizes the frame from the shorter side of the card, so it stays thin on wide.
  4. Text drawn as shapes still counts as text. The tests read a card's text back to prove that, for example, a hidden lock's remaining time never appears on it. They do this by recording every string passed to the pen. If you draw letters as rectangles, pass the string to the pen's sink first (pixelart.text() shows how), or the check can't see that text.
  5. Keep the palette rules. A style flag doesn't exempt a theme from the legibility floors or the semantic rule. Pixel Pup is measured like any other theme.

Cosmo Pup (cosmo) is the second art style, and it reuses Pixel Pup's rather than adding a third: pixel=True, plus two fields that default to Pixel Pup's behaviour. sprite names the mascot ("cosmo/cosmo" is Cosmo in his helmet, from the vendored package in app/handlr/web/static/cosmo/), and stars=True swaps the scanlines for a sparse sky of faint square stars. The stars stay faint on purpose: they go down before the layout, so one can land beside a digit, where a bright dot reads as a decimal point and a plus shape reads as "+" (added time). Cosmo's colours are read from the package's tokens.json, not copied, so they cannot drift.

Performance rules for any new drawing code:

Preview and test

Run the dev server (./run-dev.sh, port 8770), open Share, and click your chip. To look at one card on its own, request the image directly. preview=1 shows it in the browser instead of downloading it:

http://localhost:8770/share/card.jpg?theme=ember&shape=story&layout=poster&preview=1
http://localhost:8770/share/card.jpg?theme=ember&shape=wide&layout=dense&preview=1
http://localhost:8770/share/card.jpg?theme=ember&shape=square&layout=checkin&photo=1&preview=1

Check all three shapes. wide is the tightest, and that is where clipped or crowded elements show up first. Check-in needs a photo on the box (photo= is a media id), and wide has no check-in layout: it draws Dense.

Then run the tests:

.venv/bin/pytest tests/test_theme.py tests/test_share.py tests/test_share_layout.py tests/test_share_page.py tests/test_share_presets.py

Checklist


This page also ships inside the app, at /guide โ€” so your own instance always serves the guide for the version you are running, with the internet unplugged. Get Handlr ยท Something wrong here? Tell me.