Skip to content

Colours: one shared rule reads and writes #rrggbb text, so a stray + or ## is refused (#5184) - #16716

Merged
bherbruck merged 1 commit into
tangency-labs:mainfrom
CamWight:coedit-ce15-hex-colour
Nov 21, 2026
Merged

bherbruck merged 1 commit into
tangency-labs:mainfrom
CamWight:coedit-ce15-hex-colour

Conversation

@CamWight

@CamWight CamWight commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Cam

Change

SolidCraft reads and writes colours as #rrggbb text in many places: theme files, the colour field's hex box, 3MF files and the appearance dialog. This PR gives that text one rule in one place, so every reader accepts the same colours.

It is one of the cleanups in the live co-editing plan (#5184). The plan found the rule written four times for reading and seven times for writing, with different results.

Before this change:

  • Two readers (the 3MF reader and the appearance dialog's) did not check the digits first. Rust's u8::from_str_radix accepts a leading +, so "#+f0000" read as a colour.
  • The appearance dialog's reader also accepted any number of #.
  • The colour field and theme files already used the strict rule.

Now:

  • solidcraft_color::hex holds the rule, beside srgb in the shared colour crate. A colour is # and exactly 6 or 8 ASCII hex digits, in either case. The alpha is straight (not premultiplied), as CSS Color 4 §5.2 and the 3MF ST_ColorValue type write it.
  • parse_digits is for readers that let the # be left out. format and format_alpha write lower case.
  • Nothing is trimmed. A text field keeps a space as typed, and a file reader trims first, as it did before.
  • These call it: the colour field's hex box and recent swatches, theme file colours and icon palettes (ui-kit), the 3MF reader's display colours (io), Look::hex (doc), and the appearance dialog's reader and hex (ui-egui).
  • What changes: the 3MF reader and the appearance dialog's reader refuse a + where a digit belongs, and the dialog's reader refuses ##. Every well-formed colour reads and writes as before, and no saved or exported text changes.

Changes a default: no

Follow-ups, not in this PR

  • The command colour parameter (inspect::color_param). Its looser text form is a published schema pattern, so a change there changes what agents may send, with its own pattern.
  • Two literal format!("#{:02x}...") lines in crates/engine/src/cmd/inspect.rs, after Appearance dialog: "In This Design" lists imported and material colours, not only assigned looks (#14856) #14858 lands (it edits around them).
  • The 3MF writer writes upper case. Moving it would change exported bytes, so it stays.
  • The CLI preview's own check, which would need a solidcraft-color dependency.
  • The KiCad import's colours from KiCad PCB import (#5091) #5092, which landed after this PR's research: its reader (parse_kicad_color, crates/engine/src/cmd/file.rs:871) and its writer (crates/ui-egui/src/kicad_import_dialog.rs:38) don't use the shared rule yet. A follow-up PR on main moves them onto it and decides the 3-digit #fff form, which KiCad's reader accepts.

Validation

Tested on Windows, --profile ci, on main 22aa00d (14 Nov):

  • color: 6 passed (3 new hex::tests, 3 srgb).
  • ui-kit color_tests and theme_files: 28 passed.
  • io threemf: 27 passed, including the new display_colours_read_only_hex_digits.
  • doc appearance: 18 passed.
  • ui-egui dialogs_appearance and theme_files_tests: 47 passed, including the new refusals in colours_parse_both_ways.
  • engine schema::appearance::tests and cmd::inspect::pbr_tests: 34 passed (the replies' colours, and the command pattern this PR leaves alone).
  • 3 planted faults in crates/color/src/hex.rs, each caught, then a clean run:
    • no hex-digit check: caught by only_a_hash_and_six_or_eight_hex_digits_read and digits_without_the_hash;
    • parse trims: caught by only_a_hash_and_six_or_eight_hex_digits_read;
    • format writes upper case: caught by every_byte_writes_and_reads_back and doc's appearance::tests::library_and_materials.
  • cargo clippy -D warnings, all targets, on color, ui-kit, io, doc and ui-egui: clean.
  • cargo check --target wasm32-unknown-unknown on color, doc and io: clean. cargo xtask lists: passes. rustfmt --check on the 7 changed Rust files: clean.

What the tests don't cover: the preference fields that read through the dialog's parse_color are not exercised on their own. colours_parse_both_ways covers the text forms they send (with and without the #).

Screenshots of the behavior (when observable)

Nothing changes on screen. Well-formed colours read and write exactly as before. The only difference is that malformed text ("#+f0000", "##0a1b2c") is refused, and the tests above show it.

Integration status

Checked on 14 Nov 2026: one commit on main f63f3b8, no conflicts. The two main commits since the tested base change only solidcore's AGENTS.md. No open PR touches crates/color, crates/ui-kit/src/color.rs or crates/io/src/threemf.rs. #14858 (appearance dialog), #2717, #10968 and #14259 (theme files) change the same files in other places, at least 4 lines away from these hunks.

Summary by CodeRabbit

  • New Features
    • Added support for parsing hexadecimal colors in #rrggbb and #rrggbbaa formats, accepting uppercase or lowercase digits.
    • Added lowercase hexadecimal formatting for RGB colors, with optional alpha output.
  • Improvements
    • Color appearance settings, editing, theme files, and 3MF imports now use consistent hexadecimal color handling.
    • Color entry in appearance settings accepts surrounding whitespace and an optional # prefix; alpha-bearing values are accepted only where supported.

… and appearances (CE15, tangency-labs#5184)

Colours as `#rrggbb` text were read in four places and written in seven,
with different rules. Two copies took a leading `+` as a digit
(`u8::from_str_radix("+f", 16)` is 15), and one took any number of `#`.

`solidcraft_color::hex` now holds the rule once, beside `srgb`: `#` and
exactly 6 or 8 ASCII hex digits, either case, alpha straight;
`parse_digits` for readers that let the `#` be left out; `format` and
`format_alpha` write lower case. Nothing trims; callers that read files
trim first, as they did.

Switched to it: the colour field's hex text and recent swatches and theme
files' colours and icon palettes (ui-kit), the 3MF reader's display
colours (io), `Look::hex` (doc), and the appearance dialog's reader and
`hex` (ui-egui). The ui-kit field and theme files accept exactly what they
did. The 3MF reader and the dialog's reader now refuse a `+` where a
digit belongs, and the dialog's reader refuses `##`. Well-formed colours
read and write as before.

Left as they are, each for its own change: the command colour parameter
(`inspect::color_param`, whose looser text form is a published schema
pattern), the 3MF writer (upper case in exported files), and the CLI's
preview check (no colour dependency).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The color crate adds public hexadecimal parsing and formatting helpers. Code in 3MF I/O, appearance dialogs, and UI components now uses these helpers for color text.

Changes: Hexadecimal color handling

Layer / File(s) Summary
Shared parser and formatter
crates/color/src/hex.rs, crates/color/src/lib.rs, crates/color/README.md, crates/color/Cargo.toml
The color crate exposes helpers that parse six- or eight-digit ASCII hex values and format lowercase RGB or RGBA text. Tests and documentation cover the accepted formats and rejection rules.
I/O and appearance dialogs
crates/io/src/threemf.rs, crates/ui-egui/src/dialogs_appearance.rs, crates/doc/src/appearance.rs
3MF parsing and appearance dialog parsing and formatting use the shared helpers. The appearance dialog accepts trimmed bare hex or a single leading #, and rejects alpha-bearing values.
UI color and theme handling
crates/ui-kit/src/color.rs, crates/ui-kit/src/theme_files.rs
Color editing, recent-color labels, theme parsing, and palette formatting use the shared helpers. Theme parsing defaults missing alpha to 255.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: Minimal · up to c1b52

This change centralises hex colour parsing and formatting. Well-formed colours read and write as before, and the stricter rejection of signs and repeated '#' is intended. No merge-blocking risk remains.

Security architecture risk: Minimal · up to c1b52

The shared helpers tighten malformed color input while preserving existing formatting and caller-owned opacity controls. The inspected changes do not introduce a material security, failure-containment, rollback, or ownership risk.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The inspected changed input paths affect imported model display colors and caller-owned UI or theme colors. Their outputs are bounded color values; these paths do not establish a new privileged operation or broader identity boundary.

Trust Boundaries and Controls

  • observed — The UI color picker still refuses nonopaque alpha when opacity is not editable, and applies parsed values only after that check. The preferences hex field likewise updates the saved RGB only after successful parsing. Malformed drafts therefore do not bypass these validation gates.
Pre-merge checks: Passed 5

Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title names the main change: one shared rule for reading and writing #rrggbb colour text, with the example of refusing a stray + or ##. It matches the changeset, which adds solidcraft_color::hex a…
Description check Passed The description follows the template closely. It has a plain-language Change section that explains the problem and the new shared rule, a 'Changes a default: no' line, a Validation section with comman…
Existing Defaults Unchanged Passed The check passes. The PR changes no UX default that a user relies on, and its description correctly states Changes a default: no. Evidence from the diff (base..head, 9 files): - Output is unchanged.…
Full details: Title check

Explanation

The title names the main change: one shared rule for reading and writing #rrggbb colour text, with the example of refusing a stray + or ##. It matches the changeset, which adds solidcraft_color::hex and switches the readers and formatters to it. The title is long at 93 characters, and the issue number adds noise, but the primary change is clear.

Full details: Description check

Explanation

The description follows the template closely. It has a plain-language Change section that explains the problem and the new shared rule, a 'Changes a default: no' line, a Validation section with commands and results, and a Screenshots section that explains why none are needed because nothing changes on screen. The Integration status section is present. The description also lists the follow-ups and what the tests do not cover. It refers to issue #5184 and other PR numbers without fully explaining them, but the core content is complete.

Full details: Existing Defaults Unchanged

Explanation

The check passes. The PR changes no UX default that a user relies on, and its description correctly states Changes a default: no. Evidence from the diff (base..head, 9 files): - Output is unchanged. Look::hex (doc), hex() (ui-egui), HexEdit::set and recent swatches (ui-kit), export_icon_palette and format_color (ui-kit theme_files) all now call solidcraft_color::hex::format or format_alpha. Both emit lowercase #rrggbb / #rrggbbaa, the same bytes as the previous inline format!("#{:02x}...") code. - Parsing changes only refuse malformed text: - The 3MF color() reader (io/threemf.rs) now refuses a + in a digit position. Previously from_str_radix accepted it. - The appearance dialog parse_color (ui-egui) now strips only one #, so ##... is refused. It also checks digits first, so + is refused. - Every well-formed 6-digit input, with or without # and with surrounding whitespace, is still accepted. The dialog still rejects 8-digit input as before. - The ui-kit parse_hex and theme_files parse_color already used the strict rule. Their behaviour is unchanged apart from moving to the shared function. - The changes affect no preference default, layout, shortcut, or command's normal usage. The only behaviour change is refusing malformed colour text such as #+f0000 or ##0a1b2c. That is input validation for invalid input, not a changed default used the usual way. - The change does not touch solidcore or any kernel or geometry result. The description's Changes a default: no is consistent with the code.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between f63f3b8 and c1b5283.

Files selected for processing: 9

  • crates/color/Cargo.toml
  • crates/color/README.md
  • crates/color/src/hex.rs
  • crates/color/src/lib.rs
  • crates/doc/src/appearance.rs
  • crates/io/src/threemf.rs
  • crates/ui-egui/src/dialogs_appearance.rs
  • crates/ui-kit/src/color.rs
  • crates/ui-kit/src/theme_files.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@github-actions github-actions Bot added area:doc timeline, components, parameters, naming area:io import/export and file formats (STEP, IGES, 3MF, glTF, designs) area:ui desktop/web front end, dialogs, viewport labels Nov 14, 2026
@CamWight
CamWight marked this pull request as ready for review November 14, 2026 20:07
@bherbruck bherbruck added ready to merge reviewed, every ask addressed; merge once CI is green approved-for-ci Reviewed: run the full PR CI (build, tests, goldens, Clippy) labels Nov 14, 2026
@CamWight CamWight changed the title Colours: one shared rule reads and writes #rrggbb text, so a stray + or ## is refused everywhere (#5184) Colours: one shared rule reads and writes #rrggbb text, so a stray + or ## is refused (#5184) Nov 17, 2026
@bherbruck
bherbruck added this pull request to the merge queue Nov 21, 2026
@bherbruck bherbruck added the owner-approved Owner reviewed a sensitive-path change (kernel, CI, build scripts, deps); only the owner adds this label Nov 21, 2026
Merged via the queue into tangency-labs:main with commit 3b481b9 Nov 21, 2026
167 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved-for-ci Reviewed: run the full PR CI (build, tests, goldens, Clippy) area:doc timeline, components, parameters, naming area:io import/export and file formats (STEP, IGES, 3MF, glTF, designs) area:ui desktop/web front end, dialogs, viewport owner-approved Owner reviewed a sensitive-path change (kernel, CI, build scripts, deps); only the owner adds this ready to merge reviewed, every ask addressed; merge once CI is green

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants