the differences in how different color modes interpolate the journey from one color to the next.
A Pen by HARUN PEHLİVAN on CodePen.
the differences in how different color modes interpolate the journey from one color to the next.
A Pen by HARUN PEHLİVAN on CodePen.
| <main> | |
| <nav> | |
| <input id="color-a" type="color" value="#00c4ff" /> | |
| <input id="steps" type="number" value="3" min="3" max="24" /> | |
| <input id="color-b" type="color" value="#ff6347" /> | |
| </nav> | |
| <section></section> | |
| </main> |
| console.clear(); | |
| const section = document.querySelector("section"); | |
| const nav = document.querySelector("nav"); | |
| const colorA = document.getElementById("color-a"); | |
| const colorB = document.getElementById("color-b"); | |
| const steps = document.getElementById("steps"); | |
| const lib = {}; | |
| colorA.addEventListener("input", run); | |
| colorB.addEventListener("input", run); | |
| steps.addEventListener("input", run); | |
| run(); | |
| function run() { | |
| const a = colorA.value; | |
| const b = colorB.value; | |
| const scales = generateScales(a, b, parseInt(steps.value)); | |
| nav.style.background = `linear-gradient(90deg, ${a} 0%, ${a} 49.999%, ${b} 50%, ${b} 100%)`; | |
| for (let mode in scales) { | |
| let item = lib[mode]; | |
| let span; | |
| let ul; | |
| if (item) { | |
| span = item.span; | |
| ul = item.ul; | |
| } else { | |
| const div = document.createElement("div"); | |
| span = document.createElement("span"); | |
| ul = document.createElement("ul"); | |
| const p = document.createElement("p"); | |
| p.innerText = mode.toUpperCase(); | |
| div.appendChild(span); | |
| div.appendChild(ul); | |
| div.appendChild(p); | |
| section.appendChild(div); | |
| lib[mode] = { span, ul }; | |
| } | |
| const length = scales[mode].length; | |
| const steps = scales[mode].map((step, i) => `${step} ${(i / (length - 1) * 100).toFixed(3)}%`); | |
| const background = `linear-gradient(90deg, ${steps.join(", ")})`; | |
| // ul.innerHTML = scales[mode].map((hex) => `<li><span>${hex}</span></li>`).join("") + `<li><span>${background}</span></li>`; | |
| ul.innerHTML = `<li><span>${background}</span></li>`; | |
| span.style.background = background; | |
| } | |
| } | |
| function generateScales(colorA, colorB, steps = 3) { | |
| const hsl = chroma.scale([colorA, colorB]).mode("hsl"); | |
| const lab = chroma.scale([colorA, colorB]).mode("lab"); | |
| const lch = chroma.scale([colorA, colorB]).mode("lch"); | |
| const num = chroma.scale([colorA, colorB]).mode("num"); | |
| const rgb = chroma.scale([colorA, colorB]).mode("rgb"); | |
| const res = { lch: [], lab: [], rgb: [], hsl: [], num: [] }; | |
| const step = 1 / (steps - 1); | |
| for (let i = 0; i <= steps - 1; i++) { | |
| const amt = i * step; | |
| res.hsl.push(hsl(amt).hex()); | |
| res.lab.push(lab(amt).hex()); | |
| res.lch.push(lch(amt).hex()); | |
| res.num.push(num(amt).hex()); | |
| res.rgb.push(rgb(amt).hex()); | |
| } | |
| return res; | |
| } |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/chroma-js/2.1.0/chroma.min.js"></script> |
| html, body, main { | |
| height: 100%; | |
| } | |
| body { | |
| background: black; | |
| } | |
| main { | |
| display: flex; | |
| flex-direction: column; | |
| } | |
| nav { | |
| display: flex; | |
| justify-content: center; | |
| } | |
| nav input { | |
| border: 2px solid #f0f0f0; | |
| border-radius: 4px; | |
| margin: 1rem 0.5rem; | |
| } | |
| section { | |
| display: flex; | |
| flex-direction: column; | |
| flex: 1; | |
| } | |
| div { | |
| flex: 1; | |
| position: relative; | |
| } | |
| div > span { | |
| display: block; | |
| height: 100%; | |
| width: 100%; | |
| } | |
| div > ul { | |
| display: flex; | |
| flex-wrap: wrap; | |
| left: 0; | |
| list-style: none; | |
| margin: 0; | |
| opacity: 0; | |
| padding: 0; | |
| position: absolute; | |
| top: 50%; | |
| transform: translateY(-50%); | |
| width: 100%; | |
| } | |
| div:hover > ul { | |
| opacity: 1; | |
| } | |
| div:hover > p { | |
| opacity: 0; | |
| } | |
| div > ul li { | |
| flex: 1; | |
| font-size: 0.7rem; | |
| letter-spacing: 0.0625em; | |
| line-height: 1; | |
| text-align: center; | |
| } | |
| div > ul li:last-child { | |
| display: block; | |
| flex: auto; | |
| /* margin-top: 1rem; */ | |
| width: 100%; | |
| } | |
| div > ul li span { | |
| background: white; | |
| border-radius: 2px; | |
| padding: 0.125rem; | |
| } | |
| div > p { | |
| background: white; | |
| border-radius: 2px; | |
| display: inline-block; | |
| font-size: 0.7rem; | |
| left: 50%; | |
| letter-spacing: 0.0625em; | |
| line-height: 1; | |
| margin: 0; | |
| padding: 0.25rem; | |
| position: absolute; | |
| text-align: center; | |
| top: 50%; | |
| transform: translateX(-50%) translateY(-50%); | |
| } |