Skip to content

[6.x] Support shorthand hex values in hex_to_rgb modifier - #16063

Open
bardrop wants to merge 1 commit into
statamic:6.xfrom
bardrop:fix-hex-to-rgb-shorthand
Open

bardrop wants to merge 1 commit into
statamic:6.xfrom
bardrop:fix-hex-to-rgb-shorthand

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

The hex_to_rgb modifier breaks on shorthand hex colors like #fff.

It reads the value two characters at a time, so fff is read as ff, f, and nothing.

{{ '#fff' | hex_to_rgb }}

Actual: 255, 15,
Expected: 255, 255, 255

This can happen with the Color fieldtype too. The custom color input expands #fff to #ffffff, but a swatch is saved exactly as it is written in the config. So a swatch like #fff gives a broken value.

The fix

If the value has 3 or 4 characters (after the # is removed), each character is doubled first. So fff becomes ffffff and abcd becomes aabbccdd. The alpha part is ignored, the same as for 8 character values today.

The test

Added it_converts_shorthand_hex_values_to_rgb to HexToRgbTest. It fails before the fix ('255, 15, ') and passes after. All 591 tests in tests/Modifiers pass, and Pint is clean.

Resubmitted from a personal fork (replaces #16062) so maintainer edits are allowed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant