Teraz coś małego ale jakże przydatnego. Konwersja HEX koloru np. #00a1ff na RGB.
function hexTorgb(fullhex) {
const hex = fullhex.substring(1,7);
return {
R: parseInt(hex.substring(0,2), 16),
G: parseInt(hex.substring(2,4), 16),
B: parseInt(hex.substring(4,6), 16)
}
}
A wywołanie to zwyczajne przekazanie stringa do funkcji.
console.log(hexTorgb('#00a1ff'));
// => {R: 0, G: 161, B: 255 } w wyniku mamy obiekt
Oczywiście nic nie stoi na przeszkodzie aby modyfikować tą funkcję.