JavaScript
Color Conversion
Convert between RGB and HEX colors.
By EZ4Code Team
colorconversion
Code
function rgbToHex(r, g, b) {
return "#" + [r, g, b].map(x => x.toString(16).padStart(2, "0")).join("");
}
function hexToRgb(hex) {
const m = hex.replace("#", "").match(/.{2}/g);
return m ? m.map(x => parseInt(x, 16)) : null;
}
function hexToRgba(hex, alpha = 1) {
const [r, g, b] = hexToRgb(hex);
return `rgba(${r},${g},${b},${alpha})`;
}Explanation
Implements color format conversion between RGB/HEX/RGBA.
More JavaScript Snippets
Array Map Filter Reduce
Chain map, filter, reduce, find, some, and every on arrays.
Array Deduplication
Deduplicate an array using Set.
Deep Clone
Deep clone objects, supporting common data types.
Debounce Function
Wait a period of time after an event triggers before executing; reset the timer if triggered again during the wait.
Throttle Function
Limit a function to execute at most once within a time interval.
Promise.all Concurrency Control
A Promise executor with concurrency limit.