Skip to content

SVG: read oklch() and oklab() colors - #2199

Merged
andersonhc merged 4 commits into
py-pdf:masterfrom
RavSinghChandan:svg-oklch-colors
Nov 14, 2026
Merged

andersonhc merged 4 commits into
py-pdf:masterfrom
RavSinghChandan:svg-oklch-colors

Conversation

@RavSinghChandan

@RavSinghChandan RavSinghChandan commented Nov 13, 2026 •

Copy link
Copy Markdown

oklch() colors fail with ValueError: unsupported color specification, for example in the social preview images of react-window, react-virtualized and react-resizable-panels, and in the favicon of styled-components (oklch(0.6979 0.2752 25.7477 / 0.6)).

_oklab_color() reads oklab(L a b) and oklch(L C H) of CSS Color 4 (https://www.w3.org/TR/css-color-4/#specifying-oklab-oklch), with percentages, hue units, none and an optional / A. It converts them to sRGB with Björn Ottosson's matrices and clips colors outside sRGB. The hue parsing of hsl() moves to _hue_degrees() so both share it. lab() is still unsupported, so the unsupported-color.svg test is unchanged.

test_svgcolor_oklab_and_oklch checks red, blue and lime, white and black with alpha, none, and a clipped color, and fails without the change.

They failed with ValueError: unsupported color specification, as in the
social preview images of react-window and the favicon of
styled-components. They are converted to sRGB and clipped to it.

@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.

I noticed a few CSS Color 4 edge cases worth addressing before merging:

  • oklch(50% 0.1 30 / none) raises ValueError. Missing alpha should render as zero.
  • Negative chroma should clamp to zero before conversion. Currently, oklch(50% -0.2 30) produces cyan rather than gray.
  • Lightness needs clamping and black/white endpoint handling. For example, oklab(100% 0.2 0.1) currently renders pink instead of white.

@andersonhc
andersonhc merged commit 928e9b1 into py-pdf:master Nov 14, 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