cpython/Lib/profiling/sampling/_heatmap_assets/heatmap_shared.js
ivonastojanovic c91c373ef6
gh-140677 Improve heatmap colors (#142241)
Co-authored-by: Pablo Galindo Salgado <pablogsal@gmail.com>
2025-12-06 20:27:16 +00:00

40 lines
1.6 KiB
JavaScript

// Tachyon Profiler - Shared Heatmap JavaScript
// Common utilities shared between index and file views
// ============================================================================
// Heat Level Mapping (Single source of truth for intensity thresholds)
// ============================================================================
// Maps intensity (0-1) to heat level (0-8). Level 0 = no heat, 1-8 = heat levels.
function intensityToHeatLevel(intensity) {
if (intensity <= 0) return 0;
if (intensity <= 0.125) return 1;
if (intensity <= 0.25) return 2;
if (intensity <= 0.375) return 3;
if (intensity <= 0.5) return 4;
if (intensity <= 0.625) return 5;
if (intensity <= 0.75) return 6;
if (intensity <= 0.875) return 7;
return 8;
}
// Class names corresponding to heat levels 1-8 (used by scroll marker)
const HEAT_CLASS_NAMES = ['cold', 'cool', 'mild', 'warm', 'hot', 'very-hot', 'intense', 'extreme'];
function intensityToClass(intensity) {
const level = intensityToHeatLevel(intensity);
return level === 0 ? null : HEAT_CLASS_NAMES[level - 1];
}
// ============================================================================
// Color Mapping (Intensity to Heat Color)
// ============================================================================
function intensityToColor(intensity) {
const level = intensityToHeatLevel(intensity);
if (level === 0) {
return 'transparent';
}
const rootStyle = getComputedStyle(document.documentElement);
return rootStyle.getPropertyValue(`--heat-${level}`).trim();
}