[cairo] cairo_add_color_stop_rgba_premultiplied or equivalent

Nigel Tao nigel.tao.gnome at gmail.com
Sun May 16 01:06:30 UTC 2021


Summary: Was cairo_add_color_stop_rgba_premultiplied (or equivalent) ever
implemented? How else can I make Cairo gradients interpolate in
premultiplied alpha space?


In 2006 (https://lists.freedesktop.org/archives/cairo/2006-June/007203.html),
Carl Worth proposed adding cairo_set_source_rgba_premultiplied and
cairo_add_color_stop_rgba_premultiplied.

cairo_set_source_rgba_premultiplied is easy to fake on the caller-side
(albeit unable to distinguish e.g. transparent red from transparent green).
I can un-premultiply the red, green and blue arguments and pass to
cairo_set_source_rgba, watching out for division by zero alpha.

cairo_add_color_stop_rgba_premultiplied is trickier. I can un-premultiply
each stop's RGBA values in the same way. But I would also like the
interpolation to happen in premultiplied space.

Suppose that two adjacent stops' RGBA values are opaque red = RGBA(1, 0, 0,
1) and transparent black = RGBA (0, 0, 0, 0). Note that the stop color
semantics are equivalent in both premul and non-premul space.

The halfway point, mathematically, is RGBA(0.5, 0, 0, 0.5). This is 50%
opaque, but the question is whether the red has 50% or 100% Value (in the
HSV sense). Using the existing cairo_pattern_add_color_stop_rgba produces
50% Value.

I have a vector graphics file format that works entirely with premultiplied
colors (unlike SVG). I'd like to rasterize it with Cairo, so I'd like to
make Cairo gradients interpolate in premultiplied alpha space, yielding
100% Value instead in the example above.

The cairo_add_color_stop_rgba_premultiplied idea sounds very close to what
I want, but it doesn't look like it was ever implemented. That 2006 post to
the cairo@ list, linked above, is also literally the only search hit for
[cairo_add_color_stop_rgba_premultiplied]. I guess nobody replied
specificaly to the proposal.

I suppose that, on the caller-of-Cairo side, I could approximate two
adjacent stops in the file format by N explicit stops in Cairo, doing the
blend-in-premul-space myself, somewhat analogous to approximating a smooth
Bézier segment by N line segments.

I skimmed the thread (split into three on the lists.freedesktop.org
archives for some reason) that it was posted to:
https://lists.freedesktop.org/archives/cairo/2006-June/007201.html
https://lists.freedesktop.org/archives/cairo/2006-June/007206.html
https://lists.freedesktop.org/archives/cairo/2006-June/007216.html
but didn't find an alternative.

Any other suggestions for how I can do this?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.cairographics.org/archives/cairo/attachments/20210516/556dd656/attachment.htm>


More information about the cairo mailing list