Themes Internals¶
How a theme file is read, how its four colors become PadForge's whole look, where the picture is drawn, and how a Steam profile's game look follows the active profile.
The user side is on Themes. The code is in PadForge.App/Common/Themes/ unless a path says otherwise.
The parts¶
| Type | Job |
|---|---|
ThemeDefinition, ThemeFile |
One theme, and the .pftheme reader and writer |
ThemeManager |
The starters, the listed files, and the one place a look is put on |
ThemeEngine |
Works out every brush from the four colors |
ThemeColor, Lch |
OKLCH, contrast, and the two steps that move a color until it fits |
ThemeTokens, ThemeLook |
The look for the places a by-name reference does not follow |
Controls/ThemeBackdrop.cs |
The picture behind the pages |
Views/Controls/ThemeGallery |
The gallery in Settings |
Views/ThemeBuilderDialog |
New Theme and Edit Theme |
ThemePicture, ThemePalette |
The builder's picture maker and its color suggestion |
SteamGameArt |
A Steam game's library picture and logo |
GameLooks, GameLookStore |
A Steam profile's game look, and where its picture is kept |
WebThemeSheet |
The web controller's colors |
Putting a look on¶
MainWindow.OnThemeChanged calls ThemeManager.Apply(stockIndex, themeId) whenever SettingsViewModel changes the stock look (0 System, 1 Light, 2 Dark) or the picked theme's id. Ids are Starter:<slug>, File:<stored path> and Game:<app id>. The theme in force is, in this order, the one the builder is working on (SetPreview), the active profile's game look (SetGameLook), and the pick.
Reapply runs on the UI thread between Application.Resources.BeginInit() and EndInit(), so a change redraws once:
- The previous theme's values come off, through the undo the engine handed back.
- WPF UI's
ApplicationThemeManagerapplies the theme's base, or the stock look. A picked file that is missing, unreadable or no theme puts on Dark. Under Windows high contrast (HighContrastOn) no theme runs: WPF UI'sHighContrasttheme goes on over every look, Light, Dark and that Dark included.FollowHighContrastputs the look on again whenSystemParameters.HighContrastchanges. EmberTheme.ApplyAccentsets the stock tables, since a WPF UI apply derives the accent from the system color again, and ends withHoldButtonInkover them. Under high contrast it lays the contrast theme's own colors over PadForge's keys instead (ApplyContrast).new ThemeEngine(seeds).Apply(app)sets the derived values over the application's resources and returns their undo.- Home's engine card key,
HomeEngineCardBrush, takes the card fill in force (SetEngineCard). With a picture the fill is translucent: 118 of 255 where every text on the card holds its floor over every pixel of the picture laid on the page, else the least alpha that does (EngineCardAlpha).
Then ThemeTokens.Instance.Refresh() and the Applied event reach everything that redraws by hand: the steel layer, the backdrop, the gallery, the web sheet.
The picked theme file is watched by a FileSystemWatcher on its folder, whatever stands over it, and read again 300 ms after its last change. While the folder is gone, the nearest folder above it that is there is watched with its subfolders, and only a change on the way to the file counts. A watcher that raises Error, as when its drive is taken out, is let go and the file read again (WatcherFailed), and the next look put on watches it anew. A file found back when its watcher is made is read again at once.
The engine¶
ThemeEngine reads every key of the application's own dictionary and of PadForge's merged dictionaries after the two WPF UI ones. For each solid brush, color and gradient stop it finds a family:
| Family | By key, starting with | Otherwise, by OKLCH |
|---|---|---|
| Accent | Ember, Danger, LinkHover, Accent, SystemAccent, SliderThumb, SystemFillColorAttention, TitleWarmth |
Hue 15 to 68, chroma 0.03 and up |
| Secondary | Cold, InputChip |
Hue 215 to 250, chroma 0.055 and up |
| Attention | Wait, SystemFillColorCaution |
Hue 68 to 115, chroma 0.03 and up |
| Steel, the ground's steps | Chroma under 0.012, or hue 215 to 300 |
A color of no family stays as it is: player colors, axis colors, the light and key colors a user picks, and the R, G and B letters of a lighting card's rows (Channel), which are held as text all the same.
A family color turns by the seed's hue less the stock seed's, and its chroma scales with the seed's. A bright tone, of lightness 0.45 and up on a dark base or 0.80 and down on a light one, moves with the seed's lightness. A tinted ground moves with the ground's. Steel surfaces move with the ground's lightness, and steel text keeps its own. On a light base the engine sets the three page keys to the background, and the stock light look's translucent cards tint themselves over it.
The floors, each held on a card and on the page:
| Keys | Contrast |
|---|---|
The accent seed (MarkContrast) |
3.0 |
Colored text: EmberTextBrush, ColdBrush, WaitBrush, SystemFillColorCautionBrush, the Channel letters and the rest of TextKeys |
4.5 |
The gold words (WaitBrush, WaitTextBrush, WaitMutedBrush), also in a wait box and on the wait tint over a card |
4.5 |
The words on the filled buttons (EmberInkBrush, AccentButtonForeground and its pointer-over and pressed keys), on each fill they stand on |
4.5 |
TextFillColorPrimaryBrush, SubRowTextBrush, SelectedTextBrush |
7.0 |
TextFillColorSecondaryBrush |
5.5 |
TextFillColorTertiaryBrush |
4.5 |
QuietGlyphBrush |
3.0 |
TextFillColorDisabledBrush |
2.6 |
HoldButtonInk gives each filled button's words the dark ink (DarkInk) where it reads on every fill the words stand on, else white where white does, else the ink that reads better on the fill at rest, and moves a fill they do not read on until they do. An accent on which neither ink reads is first moved until one does: lighter, for the dark ink, on a dark base, and darker, for white, on a light one.
Before any of that, the ground is held to its base (GroundTextContrast, 7.0). A dark base's background is darkened in OKLCH lightness steps of 0.0025, at its own hue and chroma, until white reads 7 to 1 on the page and on the card it makes. A light base's is lightened until black does.
System, Light and Dark never run the engine, so their tables stay the stock ones. ThemeTests.PadForgesOwnLooksKeepTheirTablesAfterATheme checks them after a theme comes off.
Colors a by-name reference does not reach¶
Markup reads nearly every color by DynamicResource, which follows a theme by itself. Three kinds of place do not:
- A literal color in markup. The ember, gold and cold glows that were literals are tokens in
App.xamlnow, such asEmber29Color,ColdInputBrushandEmberTrailColor, each with its old value. - A brush kept in a style's own resources, which takes a
DynamicResourcecolor once. The two button styles that keep one bind it toThemeTokens.Instance[key], whose indexer reports a change. - Code that draws with a color it looked up. It reads
ThemeLook, whose frozen brushes and glows are made again whenThemeLook.Versionrises, and redraws throughThemeTokens.Follow(element, redraw).
ThemeTests.NoPageWritesAThemeColorAsALiteral and NoCodeKeepsAThemeColorAsAConstant keep new code to that.
The file¶
ThemeFile.Parse reads a zip when the first four bytes are PK 03 04, and JSON otherwise. In a zip, the shallowest theme.json holds the settings, and the picture is the entry they name, matched by path and then by name, else the first .png, .jpg or .jpeg. The JSON is read with comments skipped, trailing commas allowed, a byte order mark skipped and field names in any case. Text fields turn control characters, line and paragraph separators and direction overrides, embeddings and isolates into spaces, and are cut to their lengths, never through a surrogate pair: name 80, look 60, author 80, credits 400. A file is read up to 24 MB, theme.json up to 64 KB and a picture up to 16 MB, each counted as its bytes are read. The picture is decoded once, no larger than 2352 by 480, and frozen. Read never throws: a file that is gone, cannot be read or holds no theme comes back with its fault set (Missing, Unreadable, NotATheme) and the name it was listed under.
ThemeFile.Write writes the keys in the order name, look, author, base, colors, picture, tile, credits, stores the picture uncompressed, and writes a file beside the target that then takes its place.
The ten starters are such files, embedded as PadForge.Themes.<slug>.pftheme from PadForge.App/Resources/Themes/. tools/gen_starter_themes.py writes them from its table and the pictures, with a fixed zip date, so a run that changes nothing writes the same bytes. Save a Copy writes a starter's embedded bytes.
PadForge.xml keeps the list as ThemeFiles (<Theme Name="…" Path="…"/>), with a path under PadForge's folder stored relative, as a sound package's is, and the pick as ThemeId beside ThemeIndex.
The picture¶
ThemeBackdrop lies under the pages in MainWindow.xaml. Its box is the page area's upper right, as wide as the area up to 1176 units, and 240/1176 of that tall. A picture is drawn as tall as the box at its right, and one wider than the box is clipped to it. NavigateToTag sets IsWhole on Home. On every other page an opacity mask keeps the top 66 units and is clear by 86, above the first card. The element is pinned left to right, and on a right-to-left page it draws at 0.6 opacity.
The builder¶
The dialog puts its theme on through ThemeManager.SetPreview 120 ms after the last change, and takes it off with SetPreview(null) when it closes.
SteamGameArt goes through the Community tab's clients: SteamStoreClient.SearchAsync for the search, cached in the Workshop cache's Games category, and SteamArtworkClient for library_hero.jpg, then header.jpg, and logo.png, kept in its Art category. Every call stands behind the Community Configs gate. With the gate off, only the art cache is read. FetchAsync also says when there is no wide picture because Steam could not be reached, so the builder and a game look tell that (GameLookFault.Unreached) apart from a game with no picture (NoArt) and a picture that does not decode (Unreadable).
ThemePicture.Compose makes a 2352 by 480 picture with a clear ground. The source covers the right 72 percent, fades in over the first 28 percent of that box and out over the bottom 20 percent, and takes a fill of the background at 40 of 255 on a dark base or 150 on a light one. The logo fits in 300 by 116 pixels centered at (940, 84), over a soft dark shadow. Position is where the window sits down a taller source.
ThemePalette.Suggest samples the picture 96 across, up to 512 rows, and skips pixels under half alpha. The ground takes the circular mean hue of the quiet tones (lightness under 0.5 on a dark base, over it on a light one) at lightness 0.16 or 0.93. Vivid samples (chroma 0.05 and up, lightness 0.22 to 0.95) weigh into 24 hue bins by chroma squared. The accent is the heaviest bin at lightness 0.76 (0.50 on a light base), chroma 0.13 to 0.26. The secondary is the next bin at least 50 degrees away, and attention the next at least 40 degrees from both, with a warm hue when the picture has none.
A Steam profile's game look¶
ProfileData.GameLook (<GameLook AppId="…" Game="…" Background="…" Accent="…" Secondary="…" Attention="…"/>) is authored: no snapshot of the live setup captures it, and Save Current Setup Here keeps it. GameLooks.MakeAsync gets the art, suggests the colors on the dark base and composes the picture. GameLooks.Keep writes the picture into GameLookStore when the profile is saved, as <PadForge's folder>\GameLooks\<app id>.png, and Release deletes it when no profile's look names that app id. SettingsService.ResetToDefaults releases every game look's picture with the profiles.
SettingsManager.ActiveProfileId raises ActiveProfileChanged when it changes, on the thread that changed it. GameLooks.Start listens, and RefreshSoon posts Refresh to the UI thread, which builds the look's theme, kept while the look's values and the picture's write time stay the same, and calls SetGameLook. SettingsService calls RefreshSoon after the profiles load.
The web controller¶
WebControllerServer hands every served file to WebThemeSheet.Dress. Under a theme, /css/padforge.css gets a second :root rule after it, with color-scheme and the colors the sheet names after the app's brushes in its comments, 25 of them, each read from the look in force. A solid color stays solid, laid over the ground. --ember-glow, --ember-dot, --cold-dot and --veil are made from the ember, the cold and the ground. Each page's theme-color (#0a0e15) becomes the ground. Under System, Light and Dark the files go out as they are. The server sends every file with no-cache, so a page takes a change at its next load.
Tests¶
PadForge.Tests/ThemeTests.cs and ThemeTests.Follow.cs cover the color math, both file forms and their limits, the starters, the gallery, the floors over the starters and fourteen hostile themes, the stock tables, the list and its Dark fallback, the watched file, a page already built following a theme, the picture's shape, the picture maker and the palette, the builder, the look store, a game look through profile switches, the web sheet, the views that redraw in a new look, high contrast turned on and off, the filled buttons' words, the gold words and the engine card's text. ThemeInterfaceTests covers the gallery's keys, the builder's search box, the copy of PadForge's own look, the profile note, the faults and the resets, and ThemeFileReturnTests a picked file whose folder comes back. PageRenderDumpPages.DrawThemes draws the gallery, the builder and the profile dialog when PADFORGE_PAGE_RENDER_DIR names a folder.
Related pages¶
- Themes: the user's side.
- Settings and Serialization:
PadForge.xml. - XAML Views: the view layer the tokens reach.
Last updated for PadForge 6.1.0.