Azzera filtri
Azzera filtri

No transparency in Livescript

6 visualizzazioni (ultimi 30 giorni)
Duijnhouwer il 21 Feb 2021
Modificato: Duijnhouwer il 11 Apr 2023
I use RBGA values to get transparency in plots using Matlab 2020a.
This works fine in regular code but I noticed that in Livescript, the color renders as completely opaque no matter what I set the opacity value A too. This example code,
plot([0 1],[0 1],'-','Color',[1 0 0 .25],'LineWidth',20);
hold on
plot([0 1],[1 0],'-','Color',[1 0 0 .25],'LineWidth',20);
title(sprintf("renderer = %s",get(gcf,'renderer')))
when run as regular code from the draws two red bars with transparency visible where they cross (Figure 1).
However, when run it as a Livescript it produces two 100%-opaque bars, (Figure 2). Is this a know shortcoming? Is there a workaround?
  6 Commenti
Adam Danz
Adam Danz il 23 Feb 2021
Modificato: Adam Danz il 4 Apr 2023
-------------------------- old response as a user------------------------
I have the same results (windows 10, r2020b update 4) using opengl hardward and software. I see expected behavior outside of mlx but no transparency within mlx.
Keep in mind this style of line transparency is undocumented so that challenges the notion of expected behavior.
The three (undocumented) properties that control transparency of line objects appear to be set correctly when produced in an mlx file. Even when I set those values after the lines are rendered, there is no effect.
h(1) = plot([0 1],[0 1],'-','Color',[1 0 0 .25],'LineWidth',20);
% ans =
% 4×1 uint8 column vector
% 255
% 0
% 0
% 64
% ans =
% 'truecoloralpha'
% ans =
% 'object'
Noam A
Noam A il 4 Apr 2023
still having this problem in Windows 2022b, very annoying

Accedi per commentare.

Risposte (1)

Adam Danz
Adam Danz il 4 Apr 2023
Modificato: Adam Danz il 7 Apr 2023
The RGBA color definition for lines is undocumented and does not work with MLX files. The line transparency also will not appear in regular figures if you save and load the figure.
A workaround is to use patch. Here's an anonymous function you can adapt to work like plot() or line().
lineAlphaFcn = @(x,y,style,width,color,alpha) patch('XData',[x(:);nan],'YData',[y(:);nan],'EdgeColor',color,'EdgeAlpha',alpha,'LineStyle',style,'LineWidth',width);
% x: vector of x data
% y: vector of y data
% style: linestyle (e.g. '-')
% width: linewidth (e.g. 1)
% color: color (e.g. 'r' | [1 0 0])
% alpha: transparency level 0:1
% Example
% h = lineAlphaFcn(x, y, '-', 2, 'r', 0.3);
Applied to OP's demo,
lineAlphaFcn([0 1],[0 1],'-',20,[1 0 0], 0.25);
hold on
lineAlphaFcn([0 1],[1 0],'-',20,[1 0 0], 0.25);
box on
Another example,
th = linspace(0,2*pi,150)';
r = linspace(0,3,10);
n = numel(r);
color = cool(n);
hold on
for i = 1:n
h = lineAlphaFcn(th, sin(th+r(i)), '-', 12, color(i,:), 0.25);
This is the same approach I used to generate the 10,000 partially transparent line segments using a single patch object in this polar plot of pi
  5 Commenti
Noam A
Noam A il 5 Apr 2023
Ok the following code seems to do what I want and work in livescript, thank you!
color = [0 0 1 0.5];
t = linspace(0, 2*pi);
r = 10;
x = r*cos(t) + 5;
y = r*sin(t) + 5;
color = [1 0 0 0.5];
x = r*cos(t) + 3;
y = r*sin(t) + 3;
Duijnhouwer il 11 Apr 2023
Modificato: Duijnhouwer il 11 Apr 2023
Nice, @Adam Danz! I did not know use of RGBA is undocumented. So it's fair enough it doesn't work in Live Editor. I'll use patch objects in future. Thanks!

Accedi per commentare.


Scopri di più su Specifying Target for Graphics Output in Help Center e File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by