Skip to content

Commit 35bab1f

Browse files
committed
(3.48) Fixed -transparency color artifacts, set default bgcolor tolerance of +-0.1 (issue #400)
1 parent 21beeea commit 35bab1f

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

export_fig.m

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,12 @@
117117
% -transparent=<color> convert all pixels in the specified color (RGB tripplet
118118
% or a Matlab predefined color string e.g. 'r'). Usage examples:
119119
% -transparent=g or -transparent=[1,.5,.2] or -transparent=80,65,235
120-
% Optional suffix +-<value> adds tolerance to pixels color matching
121-
% for example: -transparent=g+-30 or -transparent=[1,.5,.2]+-0.1
122-
% Note: only supported by PNG,GIF,TIF formats.
120+
% -transparent=<color>+-<value> adds optional tolerance to bgcolor matching.
121+
% For example: -transparent=g+-30 or -transparent=[1,.5,.2]+-0.1
122+
% <color> may be omitted, e.g. -transparent+-8 or -transparent=+-.03
123+
% Default tolerance is 0.1 (or 25 in [0,255] units), to reduce color
124+
% artifacts around letters and line edges. Note: transparent color
125+
% customization is only supported by PNG,GIF,TIF formats.
123126
% -nocrop - option indicating that empty margins should not be cropped.
124127
% -c[<val>,<val>,<val>,<val>] - option indicating crop amounts. Must be
125128
% a 4-element vector of numeric values: [top,right,bottom,left]
@@ -406,6 +409,7 @@
406409
% 02/05/24: (3.45) Display the builtin error message when uifigure cannot be exported (issue #387); fixed contour labels with non-default FontName incorrectly exported as Courier (issue #388)
407410
% 09/05/24: (3.46) Added -xkcd option (thanks @slayton); added .fig input and output format (previously undocumented & buggy); redirect .tex output to matlab2tikz utility
408411
% 05/11/24: (3.47) Fixed -transparency in case the default bgcolor is already used in the figure (issue #398); enabled specifying non-default transparency color via -transparency parameter; suppress warnings about setting figure position; multiple -xkcd fixes
412+
% 06/03/25: (3.48) Fixed -transparency color artifacts, set default bgcolor tolerance of +-0.1 (issue #400)
409413
%}
410414

411415
if nargout
@@ -443,7 +447,7 @@
443447
[fig, options] = parse_args(nargout, fig, argNames, varargin{:});
444448

445449
% Check for newer version and exportgraphics/copygraphics compatibility
446-
currentVersion = 3.47;
450+
currentVersion = 3.48;
447451
if options.version % export_fig's version requested - return it and bail out
448452
imageData = currentVersion;
449453
return
@@ -1709,7 +1713,7 @@ function notify(filename)
17091713
'crop_amounts', nan(1,4), ... % auto-crop all 4 image sides
17101714
'transparent', false, ...
17111715
'tcol', [], ...
1712-
'ttol', 0, ...
1716+
'ttol', uint8(25), ... % in [0,255] RGB color units
17131717
'renderer', 0, ... % 0: default, 1: OpenGL, 2: ZBuffer, 3: Painters
17141718
'pdf', false, ...
17151719
'eps', false, ...
@@ -1791,7 +1795,7 @@ function notify(filename)
17911795
else %if ischar(thisArg) && ~isempty(thisArg)
17921796
if thisArg(1) == '-'
17931797
addToOptionsStr = true;
1794-
[thisArg,extra] = strtok(thisArg,'=');
1798+
[thisArg,extra] = strtok(thisArg,{'=','+'});
17951799
switch lower(thisArg(2:end))
17961800
case 'nocrop'
17971801
options.crop = false;
@@ -1816,11 +1820,18 @@ function notify(filename)
18161820
options.tcol = defaultColors(colIdx,:);
18171821
else
18181822
tcol = str2num(extra); %#ok<ST2NM>
1819-
assert(isequal(size(tcol),[1,3]),'export_fig:invalid_tcol','Invalid transparent color %s specified',extra);
1820-
if all(tcol <= 1) %fractional values
1821-
tcol = 255 * tcol; %[0-1] => [0-255]
1823+
if numel(tcol)==1 && isempty(tol)
1824+
if tcol <= 1 %fractional value
1825+
tcol = 255 * tcol; %[0-1] => [0-255]
1826+
end
1827+
options.ttol = uint8(tcol);
1828+
else
1829+
assert(isequal(size(tcol),[1,3]),'export_fig:invalid_tcol','Invalid transparent color %s specified',extra);
1830+
if all(tcol <= 1) %fractional values
1831+
tcol = 255 * tcol; %[0-1] => [0-255]
1832+
end
1833+
options.tcol = uint8(tcol);
18221834
end
1823-
options.tcol = uint8(tcol);
18241835
end
18251836
if length(tol) > 2
18261837
tol = tol(3:end); % discard '+-'
@@ -2400,12 +2411,14 @@ function eps_remove_background(fname, count)
24002411
if options.transparent
24012412
% Modify the figure's bgcolor to off-white
24022413
if isempty(options.tcol)
2403-
bgcol = uint8(254*[1,1,1]); %default bgcolor =off-white
2414+
bgcol = uint8([255,255,254]); %default bgcolor =off-white
2415+
white = uint8([255,255,255]);
24042416

24052417
% Determine off-white shade not already used in the fig - issue #398
24062418
A = getFigImage2(fig, magnify, renderer, options); % get RGB tripplets
2407-
A = unique(reshape(A,[],3),'rows'); % reshape [m,n,3] => [m*n,3]
2408-
A = sum(double(A).*(256.^[2,1,0]),2); % convert RGB tripplets => int
2419+
A = reshape(A,[],3); % reshape [m,n,3] => [m*n,3]
2420+
A = unique([A; white], 'rows'); % add pure white %issue #400
2421+
A = sort(sum(double(A).*(256.^[2,1,0]),2)); % convert RGB tripplets => int
24092422
d = A(find(diff(A)>1,1,'last')+1) - 1; %=largest RGB value -1 (off-white)
24102423
if ~isempty(d)
24112424
n=2; while n>=0, p=256^n; bgcol(3-n)=floor(d/p); d=rem(d,p); n=n-1; end %int => RGB tripplet

0 commit comments

Comments
 (0)