From 13e0cb64f47f54c54651a239c8633f6d836ea9eb Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 23 Dec 2022 00:54:05 -0500 Subject: [PATCH] hb-view: Interpolate gradients premultiplied This is what the specs demand. --- util/hb-cairo-utils.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/util/hb-cairo-utils.c b/util/hb-cairo-utils.c index 614d96eda..316f5480b 100644 --- a/util/hb-cairo-utils.c +++ b/util/hb-cairo-utils.c @@ -382,13 +382,37 @@ interpolate (float f0, float f1, float f) return f0 + f * (f1 - f0); } +static inline void +premultiply (color_t *c) +{ + c->r *= c->a; + c->g *= c->a; + c->b *= c->a; +} + +static inline void +unpremultiply (color_t *c) +{ + if (c->a != 0.) + { + c->r /= c->a; + c->g /= c->a; + c->b /= c->a; + } +} + static void interpolate_colors (color_t *c0, color_t *c1, float k, color_t *c) { + // According to the COLR specification, gradients + // should be interpolated in premultiplied form + premultiply (c0); + premultiply (c1); c->r = c0->r + k * (c1->r - c0->r); c->g = c0->g + k * (c1->g - c0->g); c->b = c0->b + k * (c1->b - c0->b); c->a = c0->a + k * (c1->a - c0->a); + unpremultiply (c); } static inline float