Skip to content

Commit c91c373

Browse files
gh-140677 Improve heatmap colors (#142241)
Co-authored-by: Pablo Galindo Salgado <pablogsal@gmail.com>
1 parent 14715e3 commit c91c373

File tree

6 files changed

+117
-157
lines changed

6 files changed

+117
-157
lines changed

Lib/profiling/sampling/_heatmap_assets/heatmap.css

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,18 +1094,34 @@
10941094
}
10951095

10961096
#scroll_marker .marker.cold {
1097+
background: var(--heat-1);
1098+
}
1099+
1100+
#scroll_marker .marker.cool {
10971101
background: var(--heat-2);
10981102
}
10991103

1104+
#scroll_marker .marker.mild {
1105+
background: var(--heat-3);
1106+
}
1107+
11001108
#scroll_marker .marker.warm {
1101-
background: var(--heat-5);
1109+
background: var(--heat-4);
11021110
}
11031111

11041112
#scroll_marker .marker.hot {
1113+
background: var(--heat-5);
1114+
}
1115+
1116+
#scroll_marker .marker.very-hot {
1117+
background: var(--heat-6);
1118+
}
1119+
1120+
#scroll_marker .marker.intense {
11051121
background: var(--heat-7);
11061122
}
11071123

1108-
#scroll_marker .marker.vhot {
1124+
#scroll_marker .marker.extreme {
11091125
background: var(--heat-8);
11101126
}
11111127

Lib/profiling/sampling/_heatmap_assets/heatmap.js

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function toggleTheme() {
2626
if (btn) {
2727
btn.innerHTML = next === 'dark' ? '&#9788;' : '&#9790;'; // sun or moon
2828
}
29+
applyLineColors();
2930

3031
// Rebuild scroll marker with new theme colors
3132
buildScrollMarker();
@@ -160,13 +161,6 @@ function getSampleCount(line) {
160161
return parseInt(text) || 0;
161162
}
162163

163-
function getIntensityClass(ratio) {
164-
if (ratio > 0.75) return 'vhot';
165-
if (ratio > 0.5) return 'hot';
166-
if (ratio > 0.25) return 'warm';
167-
return 'cold';
168-
}
169-
170164
// ============================================================================
171165
// Scroll Minimap
172166
// ============================================================================
@@ -194,7 +188,7 @@ function buildScrollMarker() {
194188

195189
const lineTop = Math.floor(line.offsetTop * markerScale);
196190
const lineNumber = index + 1;
197-
const intensityClass = maxSamples > 0 ? getIntensityClass(samples / maxSamples) : 'cold';
191+
const intensityClass = maxSamples > 0 ? (intensityToClass(samples / maxSamples) || 'cold') : 'cold';
198192

199193
if (lineNumber === prevLine + 1 && lastMark?.classList.contains(intensityClass)) {
200194
lastMark.style.height = `${lineTop + lineHeight - lastTop}px`;
@@ -212,6 +206,21 @@ function buildScrollMarker() {
212206
document.body.appendChild(scrollMarker);
213207
}
214208

209+
function applyLineColors() {
210+
const lines = document.querySelectorAll('.code-line');
211+
lines.forEach(line => {
212+
let intensity;
213+
if (colorMode === 'self') {
214+
intensity = parseFloat(line.getAttribute('data-self-intensity')) || 0;
215+
} else {
216+
intensity = parseFloat(line.getAttribute('data-cumulative-intensity')) || 0;
217+
}
218+
219+
const color = intensityToColor(intensity);
220+
line.style.background = color;
221+
});
222+
}
223+
215224
// ============================================================================
216225
// Toggle Controls
217226
// ============================================================================
@@ -264,20 +273,7 @@ function applyHotFilter() {
264273

265274
function toggleColorMode() {
266275
colorMode = colorMode === 'self' ? 'cumulative' : 'self';
267-
const lines = document.querySelectorAll('.code-line');
268-
269-
lines.forEach(line => {
270-
let bgColor;
271-
if (colorMode === 'self') {
272-
bgColor = line.getAttribute('data-self-color');
273-
} else {
274-
bgColor = line.getAttribute('data-cumulative-color');
275-
}
276-
277-
if (bgColor) {
278-
line.style.background = bgColor;
279-
}
280-
});
276+
applyLineColors();
281277

282278
updateToggleUI('toggle-color-mode', colorMode === 'cumulative');
283279

@@ -295,14 +291,7 @@ function toggleColorMode() {
295291
document.addEventListener('DOMContentLoaded', function() {
296292
// Restore UI state (theme, etc.)
297293
restoreUIState();
298-
299-
// Apply background colors
300-
document.querySelectorAll('.code-line[data-bg-color]').forEach(line => {
301-
const bgColor = line.getAttribute('data-bg-color');
302-
if (bgColor) {
303-
line.style.background = bgColor;
304-
}
305-
});
294+
applyLineColors();
306295

307296
// Initialize navigation buttons
308297
document.querySelectorAll('.nav-btn').forEach(button => {

Lib/profiling/sampling/_heatmap_assets/heatmap_index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
// Tachyon Profiler - Heatmap Index JavaScript
22
// Index page specific functionality
33

4+
// ============================================================================
5+
// Heatmap Bar Coloring
6+
// ============================================================================
7+
8+
function applyHeatmapBarColors() {
9+
const bars = document.querySelectorAll('.heatmap-bar[data-intensity]');
10+
bars.forEach(bar => {
11+
const intensity = parseFloat(bar.getAttribute('data-intensity')) || 0;
12+
const color = intensityToColor(intensity);
13+
bar.style.backgroundColor = color;
14+
});
15+
}
16+
417
// ============================================================================
518
// Theme Support
619
// ============================================================================
@@ -17,6 +30,8 @@ function toggleTheme() {
1730
if (btn) {
1831
btn.innerHTML = next === 'dark' ? '&#9788;' : '&#9790;'; // sun or moon
1932
}
33+
34+
applyHeatmapBarColors();
2035
}
2136

2237
function restoreUIState() {
@@ -108,4 +123,5 @@ function collapseAll() {
108123

109124
document.addEventListener('DOMContentLoaded', function() {
110125
restoreUIState();
126+
applyHeatmapBarColors();
111127
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Tachyon Profiler - Shared Heatmap JavaScript
2+
// Common utilities shared between index and file views
3+
4+
// ============================================================================
5+
// Heat Level Mapping (Single source of truth for intensity thresholds)
6+
// ============================================================================
7+
8+
// Maps intensity (0-1) to heat level (0-8). Level 0 = no heat, 1-8 = heat levels.
9+
function intensityToHeatLevel(intensity) {
10+
if (intensity <= 0) return 0;
11+
if (intensity <= 0.125) return 1;
12+
if (intensity <= 0.25) return 2;
13+
if (intensity <= 0.375) return 3;
14+
if (intensity <= 0.5) return 4;
15+
if (intensity <= 0.625) return 5;
16+
if (intensity <= 0.75) return 6;
17+
if (intensity <= 0.875) return 7;
18+
return 8;
19+
}
20+
21+
// Class names corresponding to heat levels 1-8 (used by scroll marker)
22+
const HEAT_CLASS_NAMES = ['cold', 'cool', 'mild', 'warm', 'hot', 'very-hot', 'intense', 'extreme'];
23+
24+
function intensityToClass(intensity) {
25+
const level = intensityToHeatLevel(intensity);
26+
return level === 0 ? null : HEAT_CLASS_NAMES[level - 1];
27+
}
28+
29+
// ============================================================================
30+
// Color Mapping (Intensity to Heat Color)
31+
// ============================================================================
32+
33+
function intensityToColor(intensity) {
34+
const level = intensityToHeatLevel(intensity);
35+
if (level === 0) {
36+
return 'transparent';
37+
}
38+
const rootStyle = getComputedStyle(document.documentElement);
39+
return rootStyle.getPropertyValue(`--heat-${level}`).trim();
40+
}

0 commit comments

Comments
 (0)