|
| 1 | +//Output of colors to page element |
| 2 | +const colorArray = document.querySelector("#color-array"); |
| 3 | +//console.log(colorArray); |
| 4 | + |
| 5 | +//array of colors example:testing |
| 6 | +const colors = []; |
| 7 | + |
| 8 | +//length of array |
| 9 | +const arrayLength = colors.length; |
| 10 | +console.log(arrayLength); |
| 11 | + |
| 12 | +//iterate of color array |
| 13 | +//create new div element with card and colorCard class |
| 14 | +// const iterate = colors.map((el) => { |
| 15 | +// const newCard = document.createElement("div"); |
| 16 | +// newCard.className = "card colorCard btn-close"; |
| 17 | +// newCard.id = `${colors.indexOf(el)}`; |
| 18 | +// newCard.style = `background-color: ${el}`; |
| 19 | +// colorArray.appendChild(newCard); |
| 20 | +// }); |
| 21 | + |
| 22 | +///////////////CRUD |
| 23 | + |
| 24 | +//delete box |
| 25 | +colorArray.addEventListener('click', (el) => { |
| 26 | + console.log(el.target); |
| 27 | + colorArray.removeChild(el.target); |
| 28 | +}); |
| 29 | + |
| 30 | +//colorWheel Container |
| 31 | +let parent = document.getElementById("colorWheel-container"); |
| 32 | + |
| 33 | +createHslPicker( |
| 34 | + parent, |
| 35 | + (h, s, l) => { |
| 36 | + let sample = document.getElementById("sample"), |
| 37 | + text = document.getElementById("hsl-values"); |
| 38 | + |
| 39 | + sample.style.background = `hsl(${h}, ${s}%, ${l}%)`; |
| 40 | + //text.innerHTML = `Hue: <b>${h}</b>, Saturation: <b>${s}</b>%, Lightness: <b>${l}</b>%`; |
| 41 | + }, |
| 42 | + 50 |
| 43 | +); |
| 44 | + |
| 45 | +/* |
| 46 | + Main function, creates the HSL picker inside a parent that you provide (such as a div). |
| 47 | + As the user picks different HSL values, you are notified via the callback. |
| 48 | + The HSL values are provided as arguments, and you can use them to update other parts of your UI. |
| 49 | + You can also pass an initial hue, via the thrid argument. |
| 50 | +*/ |
| 51 | +function createHslPicker(parent, callback,initialHue = 50) { |
| 52 | + parent.innerHTML = getHtml(); |
| 53 | + |
| 54 | + let canvas = document.getElementById("canvas-hue"), |
| 55 | + rgSat = document.getElementById("rg-saturation"), |
| 56 | + rgLight = document.getElementById("rg-lightness"), |
| 57 | + hsl = [initialHue, 100, 50]; |
| 58 | + |
| 59 | + drawColorWheel(); |
| 60 | + onHslChanged(); |
| 61 | + |
| 62 | + let xCircle = canvas.width / 2, |
| 63 | + yCircle = canvas.height / 2, |
| 64 | + radius = canvas.width / 2; |
| 65 | + |
| 66 | + canvas.addEventListener("mousemove", (ev) => { |
| 67 | + let dist = Math.sqrt( |
| 68 | + Math.pow(ev.offsetX - xCircle, 2) + Math.pow(ev.offsetY - yCircle, 2) |
| 69 | + ); |
| 70 | + canvas.style.cursor = dist <= radius ? "cell" : "default"; |
| 71 | + }); |
| 72 | + |
| 73 | + canvas.addEventListener("mousedown", (ev) => { |
| 74 | + if (ev.button != 0) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + let dist = Math.sqrt( |
| 79 | + Math.pow(ev.offsetX - xCircle, 2) + Math.pow(ev.offsetY - yCircle, 2) |
| 80 | + ); |
| 81 | + |
| 82 | + if (radius < dist) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + let sine = (yCircle - ev.offsetY) / dist, |
| 87 | + radians = Math.atan2(yCircle - ev.offsetY, ev.offsetX - xCircle); |
| 88 | + |
| 89 | + if (radians < 0) { |
| 90 | + radians = 2 * Math.PI - Math.abs(radians); |
| 91 | + } |
| 92 | + |
| 93 | + let degrees = (radians * 180) / Math.PI, |
| 94 | + hue = Math.round(degrees); |
| 95 | + onHuePicked(hue); |
| 96 | + }); |
| 97 | + |
| 98 | + function onHuePicked(h) { |
| 99 | + hsl[0] = h; |
| 100 | + //convert to hex |
| 101 | + let hex = hslToHex(hsl[0], 100, 50); |
| 102 | + console.log(hex); |
| 103 | + //add hex to array |
| 104 | + colors.push(hex); |
| 105 | + //init change on new hex |
| 106 | + onHslChanged(hex); |
| 107 | + } |
| 108 | + |
| 109 | + function hslToHex(h, s, l) { |
| 110 | + l /= 100; |
| 111 | + const a = (s * Math.min(l, 1 - l)) / 100; |
| 112 | + const f = (n) => { |
| 113 | + const k = (n + h / 30) % 12; |
| 114 | + const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); |
| 115 | + return Math.round(255 * color) |
| 116 | + .toString(16) |
| 117 | + .padStart(2, "0"); // convert to Hex and prefix "0" if needed |
| 118 | + }; |
| 119 | + return `#${f(0)}${f(8)}${f(4)}`; |
| 120 | + } |
| 121 | + |
| 122 | + function onHslChanged() { |
| 123 | + if (callback) { |
| 124 | + let hsl_to_hex = hslToHex(hsl[0], 100, 50); |
| 125 | + callback(...hsl_to_hex); |
| 126 | + genColorCube(hsl_to_hex); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + function genColorCube(hex){ |
| 131 | + const newCard = document.createElement("div"); |
| 132 | + newCard.className = "card colorCard btn-close"; |
| 133 | + newCard.id = `${colors.length}`; |
| 134 | + newCard.style = `background-color: ${hex}`; |
| 135 | + colorArray.appendChild(newCard); |
| 136 | + }; |
| 137 | + |
| 138 | + function drawColorWheel() { |
| 139 | + let ctx = canvas.getContext("2d"), |
| 140 | + radius = canvas.width / 2, |
| 141 | + x = canvas.width / 2, |
| 142 | + y = canvas.height / 2, |
| 143 | + [h, s, l] = hsl; |
| 144 | + |
| 145 | + for (let i = 0; i < 360; i++) { |
| 146 | + let color = `hsl(${i}, ${s}%, ${l}%)`; |
| 147 | + |
| 148 | + ctx.beginPath(); |
| 149 | + |
| 150 | + ctx.moveTo(x, y); |
| 151 | + ctx.arc(x, y, radius, (-(i + 1) * Math.PI) / 180, (-i * Math.PI) / 180); |
| 152 | + ctx.lineTo(x, y); |
| 153 | + ctx.closePath(); |
| 154 | + |
| 155 | + ctx.fillStyle = color; |
| 156 | + ctx.strokeStyle = color; |
| 157 | + |
| 158 | + ctx.fill(); |
| 159 | + ctx.stroke(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + function getHtml() { |
| 164 | + return `<div> |
| 165 | + <div> |
| 166 | + <canvas id="canvas-hue" width="300" height="300"></canvas> |
| 167 | + </div> |
| 168 | + |
| 169 | + <div style="grid-area: saturation; padding-top: 30px; padding-left: 20px;"> |
| 170 | + <input id="rg-saturation" style="display:none;"> |
| 171 | + </div> |
| 172 | + |
| 173 | + <div style="grid-area: lightness; padding-top: 30px; padding-left: 20px;"> |
| 174 | + <input id="rg-lightness" style="display: none;"> |
| 175 | + </div> |
| 176 | + </div>`; |
| 177 | + } |
| 178 | +} |
| 179 | + |
0 commit comments