Something went wrong. Try again.
A color lighten/darken tool from scratch built using HTML, CSS, and vanilla JavaScript as a part of updating my skillset. decoded-cipher.github.io/colortool
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144var hexInput = document.getElementById('hexInput');var inputColor = document.getElementById('inputColor');
var sliderText = document.getElementById('sliderText');var slider = document.getElementById('slider');
var alteredColor = document.getElementById('alteredColor');
var lightenText = document.getElementById('lightenText');var darkenText = document.getElementById('darkenText');var toggleBtn = document.getElementById('toggleBtn');
hexInput.addEventListener('keyup', () => { var hex = hexInput.value; if (!isValidHex(hex)) return;
var strippedHex = hex.replace('#', ''); inputColor.style.backgroundColor = "#" + strippedHex; reset();})
var isValidHex = (hex) => { if (!hex) { return false; } var strippedHex = hex.replace('#', ''); // console.log(strippedHex); return strippedHex.length === 3 || strippedHex.length === 6;}
var convertHexToRGB = (hex) => { if (!hex) { return null; } var strippedHex = hex.replace('#', '');
if (strippedHex.length === 3) { strippedHex = strippedHex[0] + strippedHex[0] + strippedHex[1] + strippedHex[1] + strippedHex[2] + strippedHex[2]; } // console.log(strippedHex); var r = parseInt(strippedHex.substring(0, 2), 16); var g = parseInt(strippedHex.substring(2, 4), 16); var b = parseInt(strippedHex.substring(4, 6), 16); return { r, g, b };}// console.log(convertHexToRGB("ffe"));
var convertRGBtoHex = (r, g, b) => { var firstPair = ('0' + r.toString(16)).slice(-2); var secondPair = ('0' + g.toString(16)).slice(-2); var thirdPair = ('0' + b.toString(16)).slice(-2);
var hex = '#' + firstPair + secondPair + thirdPair; return hex;}// console.log(convertRGBtoHex(255, 238, 255));
var alterColor = (hex, per) => { var { r, g, b } = convertHexToRGB(hex);
var amount = Math.floor((per / 100) * 255); // console.log(amount);
var newR = increaseWithinRange(r, amount); var newG = increaseWithinRange(g, amount); var newB = increaseWithinRange(b, amount); // console.log({ // newR, // newG, // newB // });
return convertRGBtoHex(newR, newG, newB);}// console.log(alterColor('fff', 10));
var increaseWithinRange = (hex, amount) => { var newHex = hex + amount; if (newHex > 255) return 255; if (newHex < 0) return 0; return newHex;}// console.log(alterColor('000', 10));
slider.addEventListener('input', () => { if (!isValidHex(hexInput.value)) return;
// console.log(slider.value); sliderText.textContent = `${slider.value}%`;
var valueAddition = toggleBtn.classList.contains('toggled') ? -slider.value : slider.value;
// var alteredHex = alterColor(hexInput.value, slider.value); var alteredHex = alterColor(hexInput.value, valueAddition); alteredColor.style.backgroundColor = alteredHex; console.log(alteredHex); alteredColorText.innerHTML = `Altered Color ${alteredHex}`;})
toggleBtn.addEventListener('click', () => { if (toggleBtn.classList.contains('toggled')) { toggleBtn.classList.remove('toggled'); lightenText.classList.remove('unselected'); darkenText.classList.add('unselected'); } else { toggleBtn.classList.add('toggled'); lightenText.classList.add('unselected'); darkenText.classList.remove('unselected'); } reset();})
var reset = () => { slider.value = 0; sliderText.innerText = '0%';
var strippedResetHex = hexInput.value.replace('#', ''); var resetHex = '#' + strippedResetHex; alteredColor.style.backgroundColor = resetHex; alteredColorText.innerText = `Altered Color ${resetHex}`;}