Skip to content

Commit 34dc816

Browse files
Fix heatmap text color logic (again)
1 parent 8f99477 commit 34dc816

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

plotly/Test_plotlyfig.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,6 +2613,38 @@ function testHeatmapTextColorDarkCells(tc)
26132613
end
26142614
end
26152615

2616+
function testHeatmapMixedColorCells(tc)
2617+
fig = figure("visible","off");
2618+
data = [ ...
2619+
4.316 3.895 3.176 2.850 2.667 2.429 2.173 1.920;
2620+
4.226 3.847 3.179 2.863 2.688 2.455 2.203 1.954;
2621+
3.951 3.650 3.113 2.824 2.666 2.458 2.224 1.998;
2622+
3.820 3.556 3.091 2.828 2.684 2.495 2.271 2.060;
2623+
3.701 3.459 3.036 2.787 2.650 2.475 2.260 2.062;
2624+
3.490 3.289 2.937 2.706 2.580 2.421 2.227 2.048;
2625+
3.292 3.128 2.820 2.608 2.493 2.353 2.186 2.031;
2626+
3.073 2.939 2.675 2.481 2.377 2.259 2.121 1.992;
2627+
];
2628+
h = heatmap(data);
2629+
nColors = 256;
2630+
zeroPos = round(0.5 * (nColors-1)) + 1;
2631+
redToWhite = [linspace(1,1,zeroPos)', linspace(0,1,zeroPos)', linspace(0,1,zeroPos)'];
2632+
whiteToGreen = [linspace(1,0,nColors-zeroPos+1)', ...
2633+
linspace(1,1,nColors-zeroPos+1)', ...
2634+
linspace(1,0,nColors-zeroPos+1)'];
2635+
customMap = [redToWhite; whiteToGreen(2:end,:)];
2636+
colormap(h, customMap);
2637+
caxis([min(data(:)) max(data(:))]);
2638+
2639+
p = plotlyfig(fig,"visible","off");
2640+
2641+
isWhite = cellfun(@(ann) ann.font.color == "rgb(255,255,255)", p.layout.annotations);
2642+
positions = cellfun(@(ann) ann.x + "," + ann.y, p.layout.annotations);
2643+
actual = positions(isWhite);
2644+
expected = ["5,0" "6,0" "6,1" "6,2" "6,3" "6,4" "6,5" "6,6" "6,7" "7,0" "7,1" "7,2" "7,3" "7,4" "7,5" "7,6" "7,7"];
2645+
tc.verifyEqual(actual, expected);
2646+
end
2647+
26162648
function testHeatmapCellTextAnnotations(tc)
26172649
fig = figure("Visible","off");
26182650
data = [1 2; 3 4; 5 6];

plotly/plotlyfig_aux/handlegraphics/updateHeatmap.m

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
data.opacity = 0.95;
5555

5656
%-setting annotation text-%
57-
maxcol = max(cdata(:));
5857
m = size(cdata, 2);
5958
n = size(cdata, 1);
6059
annotations = cell(1,m*n);
@@ -65,17 +64,17 @@
6564
family = matlab2plotlyfont(heat_data.FontName) ...
6665
);
6766

68-
values = heat_data.ColorDisplayData';
6967
for i = 1:m
7068
for j = 1:n
7169
ann.text = string(num2str(round(cdata(j,i), 2)));
7270
ann.x = i-1;
7371
ann.y = j-1;
7472
ann.showarrow = false;
75-
indices = find(values(:) == cdata(j,i));
76-
colorIndex = indices(1);
73+
ratio = (cdata(j,i) - data.zmin) / (data.zmax - data.zmin);
74+
colorIndex = 1 + clip(round(ratio*len), 0, len);
7775
c = 255*cmap(colorIndex,:);
78-
if 442*c(1) + 867*c(2) + 170*c(3) - 189006 > 0
76+
luminance = [0.299 0.587 0.114] * c'; % ITU-R BT.601 luminance standard
77+
if luminance > 128
7978
col = [0,0,0];
8079
else
8180
col = [255,255,255];

0 commit comments

Comments
 (0)