# Gradient with alpha renders weird

**URL:** <https://forum.juce.com/t/gradient-with-alpha-renders-weird/41069>\
**Category:** General JUCE discussion\
**Created:** [August 4, 2020, 2:19am UTC](https://forum.juce.com/t/gradient-with-alpha-renders-weird/41069 "2020-08-04T02:19:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lcouka](https://avatars.discourse-cdn.com/v4/letter/l/eada6e/32.png) [@lcouka](https://forum.juce.com/u/lcouka)\
**Post date:** [August 4, 2020, 2:19am UTC](https://forum.juce.com/t/gradient-with-alpha-renders-weird/41069/1 "2020-08-04T02:19:54Z")

</div>

Hello,  
When applying a gradient fill with alpha on its colours, I noticed some unattractive artefacts, like heavy banding effects, that are not present or less if you use solid colours with no alpha.

With opaque colors

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/0/06e7865605027f0411ff20695afdd1b27eb402a0.png)  
With transparent colors  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/7/7c0e8cfc47e4927456da14c966af92f1032cd284.png)

I wonder if it’s a bug or not, because in my use case the last band created by the gradient from the magnitude response on the bottom seems to be darker than the bottom background, which seems weird since the gradient is bright at the top, and fully transparent at the bottom.

Thanks in advance for your help,  
Louis

(Juce 6, OS:windows 10)

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [August 4, 2020, 6:43am UTC](https://forum.juce.com/t/gradient-with-alpha-renders-weird/41069/2 "2020-08-04T06:43:18Z")

</div>

Unfortunately there is no dithering on gradients which is normally what causes this banding effect.

---

<div class="post-metadata">

**Author:** ![yairadix](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yairadix/32/1114_2.png) [@yairadix](https://forum.juce.com/u/yairadix)\
**Post date:** [August 4, 2020, 12:51pm UTC](https://forum.juce.com/t/gradient-with-alpha-renders-weird/41069/3 "2020-08-04T12:51:22Z")

</div>

Do you mean dithering?

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [August 4, 2020, 12:55pm UTC](https://forum.juce.com/t/gradient-with-alpha-renders-weird/41069/4 "2020-08-04T12:55:02Z")

</div>

Ahem… I don’t know what you’re talking about that’s what I wrote in the first place. Nothing to see here 😁

---

<div class="post-metadata">

**Author:** ![yairadix](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yairadix/32/1114_2.png) [@yairadix](https://forum.juce.com/u/yairadix)\
**Post date:** [August 4, 2020, 1:00pm UTC](https://forum.juce.com/t/gradient-with-alpha-renders-weird/41069/5 "2020-08-04T13:00:36Z")

</div>

> [@anthony-nicholls](#):
>
> Unfortunately there is no dithering on gradients which is normally what causes this banding effect.

Afaik CoreGraphics does dither gradients! And in SR’s branch I’ve also added [a very basic gradient dither for the OpenGL backend](https://github.com/soundradix/JUCE/commit/245dab79fda98ee5fcd717a394bd10440453b1da) because I noticed that our gradients became ugly when using it. YMMV but it worked nicely for our UIs.

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [August 4, 2020, 2:58pm UTC](https://forum.juce.com/t/gradient-with-alpha-renders-weird/41069/6 "2020-08-04T14:58:53Z")

</div>

> [@yairadix](#):
>
> Afaik CoreGraphics does dither gradients!

I didn’t know that! I guess it’s not that surprising. I notice that the OP mentions they are using Windows hence they are seeing this.

---

<div class="post-metadata">

**Author:** ![lcouka](https://avatars.discourse-cdn.com/v4/letter/l/eada6e/32.png) [@lcouka](https://forum.juce.com/u/lcouka)\
**Post date:** [August 4, 2020, 4:04pm UTC](https://forum.juce.com/t/gradient-with-alpha-renders-weird/41069/7 "2020-08-04T16:04:38Z")

</div>

Yes I confirme about CoreGraphics, when my collegue sent me a screenshot on his mac it was day and night. I ended up with opaque colours in my gradient for now, where the dither is still present but far more acceptable than when the gradient is made of transparent colours.

@Yairadix, thanks a lot for sharing your code, I’ll definitivly take a look if I move on openGL one time.

Thanks again guys. 🙂

---

<div class="post-metadata">

**Author:** ![kamedin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kamedin/32/11711_2.png) [@kamedin](https://forum.juce.com/u/kamedin)\
**Post date:** [August 4, 2020, 4:16pm UTC](https://forum.juce.com/t/gradient-with-alpha-renders-weird/41069/8 "2020-08-04T16:16:43Z")

</div>

![detail](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/8/8033308bd1fef0ecfb9fe9816b3388c2b9769bf0.png)  
I think this is something else. The value goes up and down, and it falls below minimum on alpha 0. This looks like some kind of rounding error.

---

<div class="post-metadata">

**Author:** ![lcouka](https://avatars.discourse-cdn.com/v4/letter/l/eada6e/32.png) [@lcouka](https://forum.juce.com/u/lcouka)\
**Post date:** [August 4, 2020, 4:32pm UTC](https://forum.juce.com/t/gradient-with-alpha-renders-weird/41069/9 "2020-08-04T16:32:04Z")

</div>

That’s what I was thinking at first.

---

<div class="post-metadata">

**Author:** ![yairadix](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yairadix/32/1114_2.png) [@yairadix](https://forum.juce.com/u/yairadix)\
**Post date:** [August 5, 2020, 8:36am UTC](https://forum.juce.com/t/gradient-with-alpha-renders-weird/41069/10 "2020-08-05T08:36:36Z")

</div>

This can easily happen without dithering:

```auto
>>> background = 5
>>> color = [round(x*0.3) for x in range(10)]
>>> alpha = [0.1*round(x*0.2) for x in range(10)]
>>> [background+(c-background)*a for (c, a) in zip(color, alpha)]
[5.0, 5.0, 5.0, 4.6, 4.6, 4.7, 4.7, 4.7, 4.4, 4.6]

```
