Skip to content

SVG: read rgb() with channels that are not integers - #2207

Merged
andersonhc merged 5 commits into
py-pdf:masterfrom
RavSinghChandan:svg-rgb-float-channels
Nov 18, 2026
Merged

andersonhc merged 5 commits into
py-pdf:masterfrom
RavSinghChandan:svg-rgb-float-channels

Conversation

@RavSinghChandan

Copy link
Copy Markdown

Mermaid computes its theme colors and writes them as fill:rgb(45,179.9999999999,184.5) in its stylesheets, as in the LegalQA diagrams. An rgb() with commas and a channel that isn't an integer fails with ValueError: invalid literal for int() with base 10. These rules aren't applied today, but they would be once descendant selectors are (#2201).

Such a color is now read as in CSS Color 4, like the space-separated syntax already is. Integers and percentages still go through fpdf2's own parser, and an integer 4th value still sets an alpha from 0 to 255. The comma handling moved into _comma_rgb_color() to keep svgcolor() readable.

Two new cases in parameters.py cover rgb(0, 127.5, 1e2) and rgb(255, 127.5, 0, 51); both fail without the change.

@andersonhc andersonhc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small thing: rebuilding the color from channels[:3] also lets malformed inputs through. For example, rgb(0,127.5,0,51,99) now succeeds and silently drops the extra values, while a missing closing parenthesis is accepted too.

Could you validate the parentheses and require three or four channels before rebuilding the string?

@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, fixed. Before rebuilding the color, the value now has to be rgb(...) or rgba(...) with one pair of parentheses and 3 or 4 values, otherwise it raises ValueError. So rgb(0,127.5,0,51,99), a missing ")" and an extra ")" are rejected now. Integer values still go through color_from_rgb_string() as before. Added the 3 cases to the tests.

@andersonhc
andersonhc merged commit 8f790c0 into py-pdf:master Nov 18, 2026
23 checks passed
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.

2 participants