scrimba
JS color tool
Display Percentage from Slider
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

AboutCommentsNotes
Display Percentage from Slider
Expand for more info
index.js
run
preview
console
const hexInput = document.getElementById('hexInput');
const inputColor = document.getElementById('inputColor');

hexInput.addEventListener('keyup', () => {

const hex = hexInput.value;
if(!isValidHex(hex)) return;

const strippedHex = hex.replace('#', '');

inputColor.style.backgroundColor = "#" + strippedHex;
})

const isValidHex = (hex) => {
if(!hex) return false;

const strippedHex = hex.replace('#', '');
return strippedHex.length === 3 || strippedHex.length === 6;
}

const convertHexToRGB = (hex) => {
if(!isValidHex(hex)) return null;

let strippedHex = hex.replace('#', '');

if(strippedHex.length === 3){
strippedHex = strippedHex[0] + strippedHex[0]
+ strippedHex[1] + strippedHex[1]
+ strippedHex[2] + strippedHex[2];
}

const r = parseInt(strippedHex.substring(0,2), 16);
const g = parseInt(strippedHex.substring(2,4), 16);
const b = parseInt(strippedHex.substring(4,6), 16);

return {r,g,b};
}

const convertRGBToHex = (r,g,b) => {
const firstPair = ("0" + r.toString(16)).slice(-2);
const secondPair = ("0" + g.toString(16)).slice(-2);
const thirdPair = ("0" + b.toString(16)).slice(-2);

const hex = "#" + firstPair + secondPair + thirdPair;
return hex;
}

//get a reference to the slider and sliderText DOM elements
//create an input event listener
//display the value of the slider
Console
"#00ffff"
,
/index.html
-3:07