Add color picker

This commit is contained in:
lolabird 2023-11-10 12:51:07 -07:00
commit faeeb0cd2c
2 changed files with 39 additions and 16 deletions

View file

@ -21,6 +21,11 @@
<button id="trans">Transparent</button> <button id="trans">Transparent</button>
<button id="erase">Erase</button> <button id="erase">Erase</button>
<div id = col-picker>
<label for="custom-color">Pick a color:</label>
<input type="color" id="custom-color" value="#ff0000">
</div>
<div class="slider"> <div class="slider">
<label for="gridSlider">Grid Size: <span id="sliderLabel"></span></label> <label for="gridSlider">Grid Size: <span id="sliderLabel"></span></label>
<input name="gridSlider" id="gridSlider" type="range" min="1" max="100" value="16"> <input name="gridSlider" id="gridSlider" type="range" min="1" max="100" value="16">

View file

@ -4,6 +4,7 @@ const conWidth = container.clientWidth;
const grid = document.createElement("div"); const grid = document.createElement("div");
const gridSlider = document.querySelector("#gridSlider"); const gridSlider = document.querySelector("#gridSlider");
const buttons = document.querySelectorAll("button"); const buttons = document.querySelectorAll("button");
const colorPicker = document.querySelector("#custom-color");
let colorValue = "black"; let colorValue = "black";
let numGrid = gridSlider.value; let numGrid = gridSlider.value;
let sliderLabel = document.querySelector("#sliderLabel"); let sliderLabel = document.querySelector("#sliderLabel");
@ -13,10 +14,14 @@ let isTint;
let isShade; let isShade;
let isOpaque; let isOpaque;
let isTrans; let isTrans;
let isBlack;
let isWhite;
let isErase;
buttons.forEach((button) => { buttons.forEach((button) => {
button.addEventListener ("click", changeColor); button.addEventListener ("click", changeColor);
}); });
colorPicker.addEventListener("input", colorGrid);
function createGrid() { function createGrid() {
@ -54,17 +59,24 @@ function getGridDimensions() {
function colorGrid() { function colorGrid() {
if (isRgb) { if (isRgb) {
this.style.background = randomizeColor(); this.style.backgroundColor = randomizeColor();
} else if (isTint) { } else if (isTint) {
this.style.background = tintColor(this.style.background); this.style.backgroundColor = tintColor(this.style.backgroundColor);
} else if (isShade) { } else if (isShade) {
this.style.background = shadeColor(this.style.background); this.style.backgroundColor = shadeColor(this.style.backgroundColor);
} else if (isOpaque){ } else if (isOpaque){
this.style.opacity = makeOpaque(this.style.opacity); this.style.opacity = makeOpaque(this.style.opacity);
} else if (isTrans) { } else if (isTrans) {
this.style.opacity = makeTransparent(this.style.opacity); this.style.opacity = makeTransparent(this.style.opacity);
} else if (isBlack) {
this.style.backgroundColor = "rgb(0, 0, 0)";
} else if (isWhite) {
this.style.backgroundColor = "rgb(255, 255, 255)";
} else if (isErase) {
this.style.backgroundColor = "";
} else { } else {
this.style.background = colorValue; //this.style.backgroundColor = colorPicker.value;
this.style.background = colorPicker.value;
} }
} }
@ -101,16 +113,22 @@ function changeColor() {
isTrans = false isTrans = false
} }
switch (buttonID) { if (buttonID === "whiteColor") {
case "whiteColor": isWhite = true;
colorValue = "rgb(255, 255, 255)"; } else {
break; isWhite = false
case "erase": }
colorValue = "transparent";
break; if (buttonID === "blackColor") {
default: isBlack = true;
colorValue = "rgb(0, 0, 0)"; } else {
break; isBlack = false
}
if (buttonID === "erase") {
isErase = true;
} else {
isErase = false
} }
} }
@ -159,5 +177,5 @@ function makeTransparent(value) {
return num; return num;
} }
createGrid(); createGrid();
//setGridListeners();