Skip to content

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

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

bardrop wants to merge 1 commit into
statamic:6.xfrom
bardrop-oss: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.

@github-actions

Copy link
Copy Markdown

Thanks for submitting a PR!

In order to review and merge PRs most efficiently, we require that all PRs grant maintainer edit access before we review them. GitHub doesn't allow maintainer edits on PRs from forks owned by organizations, even when "Allow edits by maintainers" is checked. Please resubmit this PR from a fork on your personal GitHub account with maintainer edits enabled. For more information, see the relevant GitHub documentation.

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