<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>150940</bug_id>
          
          <creation_ts>2015-11-05 13:06:04 -0800</creation_ts>
          <short_desc>Gradient color interpolation incorrect for colors with alpha (need to interpolate premultiplied colors)</short_desc>
          <delta_ts>2023-03-23 08:38:48 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>CSS</component>
          <version>Safari 9</version>
          <rep_platform>All</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=212442</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          <dependson>234492</dependson>
    
    <dependson>234653</dependson>
          
          <everconfirmed>0</everconfirmed>
          <reporter>tmjenox</reporter>
          <assigned_to name="Sam Weinig">sam</assigned_to>
          <cc>7raivis</cc>
    
    <cc>alexwriter2003</cc>
    
    <cc>bramus</cc>
    
    <cc>calum.p.m.smith</cc>
    
    <cc>darin</cc>
    
    <cc>dino</cc>
    
    <cc>fujii</cc>
    
    <cc>gilmoreorless</cc>
    
    <cc>jonlee</cc>
    
    <cc>philip</cc>
    
    <cc>rik</cc>
    
    <cc>sam</cc>
    
    <cc>simon.fraser</cc>
    
    <cc>tym46383</cc>
    
    <cc>webkit-bug-importer</cc>
    
    <cc>zyuzin.vitaly</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1139717</commentid>
    <comment_count>0</comment_count>
    <who name="">tmjenox</who>
    <bug_when>2015-11-05 13:06:04 -0800</bug_when>
    <thetext>Currently the following gradients produce different results despite they should render the same:
 - opaque black to transparent black
 - opaque black to transparent white
 - opaque black to transparent anything

All transparent anythings are in fact the same color, a clear color, no matter what the underlying RGB components say. It seems all four components are currently linearly interpolated, resulting in a very odd gradient -- the second one has a semitransparent gray in the middle. For semitransparent endpoint colors we have the same issue: their RBG components affect the interpolated colors too much, in an unnatural way.

No matter in which color space colors are interpolated, their components should be weighted according to their alpha component.

Firefox seems to use the following weighted approach, producing a much more visually appealing gradient:

float r3 = (r1 * a1 * (1.0 - fraction) + r2 * a2 * fraction) / ((1.0 - fraction) * a1 + fraction * a2);
float g3 = (g1 * a1 * (1.0 - fraction) + g2 * a2 * fraction) / ((1.0 - fraction) * a1 + fraction * a2);
float b3 = (b1 * a1 * (1.0 - fraction) + b2 * a2 * fraction) / ((1.0 - fraction) * a1 + fraction * a2);
float a3 = a1 * (1.0 - fraction) + a2 * fraction;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1139878</commentid>
    <comment_count>1</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2015-11-05 21:23:22 -0800</bug_when>
    <thetext>This is because we don&apos;t interpolate gradients in non-premultiplied colors, because CG can&apos;t do this yet.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1139930</commentid>
    <comment_count>2</comment_count>
    <who name="">tmjenox</who>
    <bug_when>2015-11-06 03:17:46 -0800</bug_when>
    <thetext>What do you mean by &apos;CG can&apos;t do this yet&apos;?
It is obviously possible...

(In reply to comment #1)
&gt; This is because we don&apos;t interpolate gradients in non-premultiplied colors,
&gt; because CG can&apos;t do this yet.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1139963</commentid>
    <comment_count>3</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2015-11-06 08:22:12 -0800</bug_when>
    <thetext>The OS X and iOS graphics framework, Core Graphics, that WebKit uses to draw on those operating systems has a gradient feature that does not have a mode where it properly handles this. If we wanted to fix this problem in WebKit without waiting for the Core Graphics we would have to write a new gradient implementation that bypasses the Core Graphics Framework. Our plan is to instead ask Apple to add this feature to the Core Graphics framework.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1139969</commentid>
    <comment_count>4</comment_count>
    <who name="">tmjenox</who>
    <bug_when>2015-11-06 08:37:58 -0800</bug_when>
    <thetext>(In reply to comment #3)
&gt; The OS X and iOS graphics framework, Core Graphics, that WebKit uses to draw
&gt; on those operating systems has a gradient feature that does not have a mode
&gt; where it properly handles this. If we wanted to fix this problem in WebKit
&gt; without waiting for the Core Graphics we would have to write a new gradient
&gt; implementation that bypasses the Core Graphics Framework. Our plan is to
&gt; instead ask Apple to add this feature to the Core Graphics framework.

Ah, I see. So is this a known/confirmed issue? One may argue for both approaches of drawing such a gradient.

How does the collaboration with Apple work in such scenarios? Who even decides who&apos;s right and who&apos;s wrong? Is there a specification on how these gradients should be drawn?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1139975</commentid>
    <comment_count>5</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2015-11-06 08:49:10 -0800</bug_when>
    <thetext>(In reply to comment #4)
&gt; So is this a known/confirmed issue?

Yes. Simon personally contacted the team at Apple responsible for Core Graphics, a while back, to let them know about this issue.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1139976</commentid>
    <comment_count>6</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2015-11-06 08:52:44 -0800</bug_when>
    <thetext>(In reply to comment #4)
&gt; How does the collaboration with Apple work in such scenarios? Who even
&gt; decides who&apos;s right and who&apos;s wrong? Is there a specification on how these
&gt; gradients should be drawn?

This is specified by CSS: &lt;https://drafts.csswg.org/css-images/#gradients&gt;

&quot;with the interpolation taking place in premultiplied RGBA space&quot;

but that text is expected to change once Mac and iOS can support interpolation in non-premultiplied.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1139986</commentid>
    <comment_count>7</comment_count>
    <who name="">tmjenox</who>
    <bug_when>2015-11-06 09:49:27 -0800</bug_when>
    <thetext>&gt; &quot;with the interpolation taking place in premultiplied RGBA space&quot;

&gt; but that text is expected to change once Mac and iOS can support
&gt; interpolation in non-premultiplied.

Mh, I&apos;m a little confused now as I&apos;m not quite familiar with this terminology. Is interpolation in premultiplied RGBA what we are seeing now or what would be the correct thing to do? Why would that text change once Core Graphics decides to ship a fix for this bug?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1140001</commentid>
    <comment_count>8</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2015-11-06 10:38:33 -0800</bug_when>
    <thetext>(In reply to comment #7)
&gt; &gt; &quot;with the interpolation taking place in premultiplied RGBA space&quot;
&gt; 
&gt; &gt; but that text is expected to change once Mac and iOS can support
&gt; &gt; interpolation in non-premultiplied.
&gt; 
&gt; Mh, I&apos;m a little confused now as I&apos;m not quite familiar with this
&gt; terminology. Is interpolation in premultiplied RGBA what we are seeing now
&gt; or what would be the correct thing to do?

That&apos;s what you&apos;re seeing now. Transparent red in premultiplied color just becomes transparent, so any time you interpolate from it you end up with grayish intermediate values.

&gt; Why would that text change once Core Graphics decides to ship a fix for this bug?

Because all browsers would then be able to interpolate in non-premultiplied color, and the CSS working group would change the text (at least for the next level of the spec).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1140004</commentid>
    <comment_count>9</comment_count>
    <who name="">tmjenox</who>
    <bug_when>2015-11-06 10:58:58 -0800</bug_when>
    <thetext>&gt; &gt; Mh, I&apos;m a little confused now as I&apos;m not quite familiar with this
&gt; &gt; terminology. Is interpolation in premultiplied RGBA what we are seeing now
&gt; &gt; or what would be the correct thing to do?
&gt; 
&gt; That&apos;s what you&apos;re seeing now. Transparent red in premultiplied color just
&gt; becomes transparent, so any time you interpolate from it you end up with
&gt; grayish intermediate values.

Sorry if this is really obvious and I am missing something, but that makes only little sense to me.

I am currently seeing this behavior in Safari:
[opaque black (0,0,0,1)] =&gt; [semitransparent dark red (128,0,0,0.5)] =&gt; [transparent red (255,0,0,0)] (all components interpolated linearly)

However, I think it should look like this: (and it does in Firefox)
[opaque black (0,0,0,1)] =&gt; [semitransparent black (0,0,0,0.5)] =&gt; [transparent black (0,0,0,0)] (color components weighted with alpha)

Your description (&quot;so any time you interpolate from it you end up with grayish intermediate values&quot;) sounds like the latter is what you call interpolation in premultiplied RGBA, however you are also stating that what I am currently seeing is interpolation in premultiplied RGBA...

Could you please clear this up? Which of the two examples is interpolated in premultiplied RGBA? And how is the other method called?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1179922</commentid>
    <comment_count>10</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2016-04-01 13:13:13 -0700</bug_when>
    <thetext>*** Bug 156105 has been marked as a duplicate of this bug. ***</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1179927</commentid>
    <comment_count>11</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2016-04-01 13:59:08 -0700</bug_when>
    <thetext>&lt;rdar://problem/25499232&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1265846</commentid>
    <comment_count>12</comment_count>
    <who name="Binyamin">7raivis</who>
    <bug_when>2017-01-11 11:30:54 -0800</bug_when>
    <thetext>Any progress with this?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1640564</commentid>
    <comment_count>13</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2020-04-11 10:38:52 -0700</bug_when>
    <thetext>*** Bug 210365 has been marked as a duplicate of this bug. ***</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1640565</commentid>
    <comment_count>14</comment_count>
      <attachid>396173</attachid>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2020-04-11 10:40:22 -0700</bug_when>
    <thetext>Created attachment 396173
example</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1642179</commentid>
    <comment_count>15</comment_count>
    <who name="Philip Jägenstedt">philip</who>
    <bug_when>2020-04-16 01:48:25 -0700</bug_when>
    <thetext>This issue was mentioned to me while interviewing web developers about browser compat issues. I found this test in web-platform-tests which I think covers the behavior and is failing only in Safari:
https://wpt.fyi/results/css/css-images/gradients-with-transparent.html?run_id=488890002&amp;run_id=483120004&amp;run_id=505970001

Is this still blocked on Core Graphics not supporting it?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1642181</commentid>
    <comment_count>16</comment_count>
    <who name="Philip Jägenstedt">philip</who>
    <bug_when>2020-04-16 01:58:57 -0700</bug_when>
    <thetext>I also found bug 200208, which is about the specific failure I found.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1642290</commentid>
    <comment_count>17</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2020-04-16 09:46:14 -0700</bug_when>
    <thetext>(In reply to Philip Jägenstedt from comment #15)
&gt; Is this still blocked on Core Graphics not supporting it?

Yes.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1826099</commentid>
    <comment_count>18</comment_count>
    <who name="Fujii Hironori">fujii</who>
    <bug_when>2021-12-21 13:43:40 -0800</bug_when>
    <thetext>

*** This bug has been marked as a duplicate of bug 234492 ***</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1826174</commentid>
    <comment_count>19</comment_count>
    <who name="Sam Weinig">sam</who>
    <bug_when>2021-12-21 17:43:57 -0800</bug_when>
    <thetext>Keeping this open for a moment, since we still have not enabled it yet.

We need to decide what perf cost is acceptable for older platforms, and if we should add any additional optimizations like all opaque checking or the extension of that Dan suggested in https://bugs.webkit.org/show_bug.cgi?id=234492#c19.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1826664</commentid>
    <comment_count>20</comment_count>
      <attachid>447917</attachid>
    <who name="Sam Weinig">sam</who>
    <bug_when>2021-12-23 16:37:11 -0800</bug_when>
    <thetext>Created attachment 447917
Enable premultiplied interpolation for CSS gradients by default</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1826684</commentid>
    <comment_count>21</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2021-12-23 20:24:49 -0800</bug_when>
    <thetext>Committed r287419 (245554@main): &lt;https://commits.webkit.org/245554@main&gt;

All reviewed patches have been landed. Closing bug and clearing flags on attachment 447917.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>396173</attachid>
            <date>2020-04-11 10:40:22 -0700</date>
            <delta_ts>2020-04-11 10:40:22 -0700</delta_ts>
            <desc>example</desc>
            <filename>210365.html</filename>
            <type>text/html</type>
            <size>249</size>
            <attacher name="Alexey Proskuryakov">ap</attacher>
            
              <data encoding="base64">PHN0eWxlPgpwIHsKICBiYWNrZ3JvdW5kOgogICAgIGxpbmVhci1ncmFkaWVudCgKICAgICAgIHRv
IHJpZ2h0LAogICAgICAgI2ZmOTgwMCwKICAgICAgIHRyYW5zcGFyZW50CiAgICAgKTsKfQo8L3N0
eWxlPgo8cD5Mb3JlbSBpcHN1bSBkb2xvciBzaXQgYW1ldCwgY29uc2VjdGV0dXIgYWRpcGlzY2lu
ZyBlbGl0LCBzZWQgZG8gZWl1c21vZCB0ZW1wb3IgaW5jaWRpZHVudCB1dCBsYWJvcmUgZXQgZG9s
b3JlIG1hZ25hIGFsaXF1YS48L3A+
</data>

          </attachment>
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>447917</attachid>
            <date>2021-12-23 16:37:11 -0800</date>
            <delta_ts>2021-12-23 20:24:51 -0800</delta_ts>
            <desc>Enable premultiplied interpolation for CSS gradients by default</desc>
            <filename>bug-150940-20211223163710.patch</filename>
            <type>text/plain</type>
            <size>1591</size>
            <attacher name="Sam Weinig">sam</attacher>
            
              <data encoding="base64">SW5kZXg6IFNvdXJjZS9XVEYvQ2hhbmdlTG9nCj09PT09PT09PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT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