Skip to content

Read the alpha of #rgba and #rrggbbaa, and clamp colour channels instead of wrapping them - #570

Merged
jakejackson1 merged 1 commit into
gravitypdffrom
fix/552-colour-values
Sep 30, 2026
Merged

jakejackson1 merged 1 commit into
gravitypdffrom
fix/552-colour-values

Conversation

@jakejackson1

@jakejackson1 jakejackson1 commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Problem

Some colour values were read as a different colour:

Input Drawn Expected
background: #f008 rgb(240, 8, 0), opaque red at 53% opacity
background: #ff000080 opaque red red at 50% opacity
color: rgb(255.5, 0, 0) black red
color: rgb(300, 0, 0) dark red, rgb(44, 0, 0) red
rgba(0, 0, 255, 1.5) /ca 1.5, outside the range PDF allows opaque blue
cmyk(150, 0, 0, 0) a component of 1.5 100% cyan
color: rebeccapurple black #663399

These are the causes:

  • processHashColor() handled only 3 and 6 digits. The background shorthand also only recognised a hex colour of up to 6 digits.
  • convert() packed each channel with round(...) & 0xFF, which wraps a value outside 0–255 instead of clamping it. For example, 255.5 rounds to 256 and wraps to 0.
  • rebeccapurple was not in NamedColors.

Change

  • #rgba and #rrggbbaa are read with their alpha, as rgba() is.
  • rgb() channels are clamped to 0–255, cmyk() components to 0–100, and alphas to 0–1. hsl() saturation and lightness are clamped to 0–100%, and a bare grey number is clamped to 255. This happens in processModeColor(), before the PDF/A and PDF/X colour space restrictor sees the colour.
  • The background shorthand recognises a hex colour of up to 8 digits.
  • rebeccapurple is added. violetred is not a CSS colour, but mPDF has always accepted it, so it stays. Removing it would turn existing documents that use it black.

Colours inside their range draw the same as before. No existing snapshot fixture changed.

The percentage alpha from #552 (rgba(255, 0, 0, 50%) writing /ca 1.36) is fixed in #563, because the slash syntax there needs it.

Tests

  • ColorConverterTest::testConvertColorValues checks the binary colour for each of these:

    • #rgba and #rrggbbaa, including uppercase, fully opaque and fully transparent
    • a fractional channel, channels past either end, and a percentage past 100%
    • alphas above 1, below 0 and above 100%
    • hsl() saturation and lightness above 100%
    • cmyk() and cmyka() values out of range
    • a bare grey above 255
    • rebeccapurple, and violetred kept
  • ColorValueTest renders each case in a paragraph. It asserts the colour operator and opacity drawn, that no /ca above 1 is written, and that nothing warns. It covers #rgba and #rrggbbaa in the background shorthand and in background-color, which exercises the shorthand regex. With the NormalizeProperties change alone reverted, the shorthand #rrggbbaa case fails. With all source changes reverted, all seven cases fail.

  • ColorValueSnapshotTest (color-value) puts each case under a caption saying what should be seen:

    • red and blue bands at 53% and 50% opacity over black and white halves
    • two red lines from out-of-range channels
    • an opaque blue band from an alpha above 1
    • rebeccapurple and violetred text

    Rendered before this change:

    • the #f008 band is an opaque orange-red and the #0000ff80 band is opaque blue
    • the first line is black and the second dark red
    • the rebeccapurple line is black

Checks

  • composer test: OK. 4522 tests, 31 skipped for missing optional packages or ungenerated data. The snapshot suite ran with Imagick and Ghostscript.
  • composer cs: clean.
  • PHPStan 2.2.16: no errors (run with --memory-limit=2G).

Part of #552. Part of #520.

🤖 Generated with Claude Code

@jakejackson1 jakejackson1 added bug Something isn't working create-upstream-pr labels Sep 29, 2026
@jakejackson1
jakejackson1 force-pushed the fix/552-space-syntax-colours branch from f92feed to a56cfee Compare September 29, 2026 22:26
@jakejackson1
jakejackson1 force-pushed the fix/552-colour-values branch 2 times, most recently from a033a72 to b4398f2 Compare September 29, 2026 23:03
@jakejackson1
jakejackson1 changed the base branch from fix/552-space-syntax-colours to gravitypdf September 29, 2026 23:03
…ead of wrapping them

`#f008` was read from the wrong digits and `#rrggbbaa` lost its alpha.
A channel outside its range was masked into a byte, so rgb(255.5, 0, 0)
drew black and rgb(300, 0, 0) dark red. Read both hex forms with their
alpha, let the background shorthand take an eight-digit colour, clamp
channels, alphas and hsl() saturation and lightness to their range, and
add rebeccapurple.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jakejackson1
jakejackson1 merged commit 2ec1731 into gravitypdf Sep 30, 2026
11 checks passed
jakejackson1 added a commit that referenced this pull request Sep 30, 2026
The three sub-bullets under #563's rgb()/hsl() space-syntax entry and the
three under #570's colour-values entry lost their two-space indent in
later merges, so each read as a bugfix of its own. They are indented
under their entries again, as #563 and #570 wrote them.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@jakejackson1
jakejackson1 deleted the fix/552-colour-values branch September 30, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working create-upstream-pr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant