[reportlab-users] Semi-transparent gradient

54 views
Skip to first unread message

Lele Gaifax

unread,
Apr 4, 2022, 7:48:21 AM4/4/22
to reportl...@lists2.reportlab.com
Hi all,

I need to put some text on top of an image, and to make sure it's readable I'm
drawing a rectangle filled with a semi-transparent color (say,
rgba(250,250,250,0.6)) and then writing the text over that.

Now I'm asked if it would be possible to use a gradient instead: since the
text is right-aligned, they thinks it would be nicer to have a "progressive
shading" instead of a that equally opaque rectangle under the text.

I tried to use the Canvas.linearGradient() functionality to obtain that,
something like

path = canvas.beginPath()
path.rect(113.2*mm, 0, 200*mm, 176.8*mm)
canvas.clipPath(path, fill=False, stroke=False)
canvas.linearGradient(113.2*mm, 0, 200*mm, 0,
(Color(0, 0, 0, 0.1), Color(0, 0, 0, 0.5)),
extend=False)

but, AFAICT, it does not respect the color's alpha, and it basically
completely wipes the underlying image.

Am I missing something, or is there another way to achieve the result I'm
seeking, other than overlying another semi-transparent image over the first?

Thanks in advance for any hint,
ciao, lele.
--
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
le...@metapensiero.it | -- Fortunato Depero, 1929.

_______________________________________________
reportlab-users mailing list
reportl...@lists2.reportlab.com
https://pairlist2.pair.net/mailman/listinfo/reportlab-users

Robin Becker

unread,
Apr 4, 2022, 9:11:53 AM4/4/22
to reportlab-users, Lele Gaifax
Hi Lele,

I think your logic would be correct, but I believe that the linearGradient functionality is ignoring the alpha as you
suspect.

I did not have any great part in the pdf canvas gradient code. However, I think the problem is in the color definitions
we are using that are derived from the input colours. I think that is done in canvas._normalizeColor which seems only to
care about the rgb values.

I seem to recall tha pdf transparency is supposed to apply to objects rather than colours.

I can certainly change the whole gradient using eg


c._setFillAlpha(0.7)

prior to calling c.linerGradient.

On 04/04/2022 12:48, Lele Gaifax wrote:
> Hi all,
>
> I need to put some text on top of an image, and to make sure it's readable I'm
> drawing a rectangle filled with a semi-transparent color (say,
> rgba(250,250,250,0.6)) and then writing the text over that.
>
...........
--
Robin Becker

Lele Gaifax

unread,
Apr 4, 2022, 1:57:50 PM4/4/22
to reportl...@lists2.reportlab.com
Hi Robin,

Robin Becker <ro...@reportlab.com> writes:

> I think your logic would be correct, but I believe that the linearGradient
> functionality is ignoring the alpha as you suspect.
>

> I think the problem is in the color definitions we are using that are
> derived from the input colours. I think that is done in
> canvas._normalizeColor which seems only to care about the rgb values.
>
> I seem to recall tha pdf transparency is supposed to apply to objects rather
> than colours.

If I understand correctly, I then get that it's not possible to have a
gradient that works on the alpha level. Not a great problem, I will try using
a semi-transparent image over the real one.

> I can certainly change the whole gradient using eg
>
> c._setFillAlpha(0.7)
>
> prior to calling c.linerGradient.

I tried that, and it somewhat work, but is not appropriate for my case,
because I'd like to blur only a part of the image. Anyway, will try to find an
alternative, although less elegant, approach.

Thanks a lot,
bye, lele.


--
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
le...@metapensiero.it | -- Fortunato Depero, 1929.

_______________________________________________

Reply all
Reply to author
Forward
0 new messages