Files

34 lines
970 B
JavaScript

/**
* VIZ 2.0 - Utility Helpers
*/
/**
* Formats a duration in seconds as mm:ss
* @param {number} seconds
* @returns {string}
*/
export function formatTime(seconds) {
if (isNaN(seconds) || seconds < 0) return '00:00';
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
/**
* Converts a hex color (#rgb or #rrggbb) to an rgba() string
* @param {string} hex
* @param {number} alpha
* @returns {string}
*/
export function hexToRgba(hex, alpha = 1.0) {
if (!hex || typeof hex !== 'string') return `rgba(255,255,255,${alpha})`;
let c = hex.replace('#', '');
if (c.length === 3) c = c.split('').map(x => x + x).join('');
const num = parseInt(c, 16);
if (isNaN(num)) return `rgba(255,255,255,${alpha})`;
const r = (num >> 16) & 255;
const g = (num >> 8) & 255;
const b = num & 255;
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}