JSFiddle - React, Tailwind, and code Playground

by Sebastian Kay

HTML

<script src="https://unpkg.com/[email protected]/dist/chroma.min.cjs"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<div class="container my-5">
  <!-- Bootstrap 5 switch -->
  <div class="form-check form-switch">
    <input
      class="form-check-input"
      type="checkbox"
      id="darkModeSwitch"
      checked
    />
    <label class="form-check-label" for="darkModeSwitch">Dark Mode</label>
  </div>
</div>

<div class="container my-5">
  <div class="row">
    <div class="col-auto">
      <input
        type="color"
        class="form-control form-control-color col-auto"
        id="input_color_select"
        value="#e9a530"
        title="Choose your color"
      />
    </div>
    <div class="col-auto">
      <input type="text" class="form-control" id="input_color_text" />
    </div>
  </div>
</div>

JavaScript

const inputColorText = document.querySelector("#input_color_text")
const inputColorSelect = document.querySelector("#input_color_select")

document.addEventListener("DOMContentLoaded", (event) => {
  const htmlElement = document.documentElement
  const switchElement = document.getElementById("darkModeSwitch")

  // Set the default theme to dark if no setting is found in local storage
  const currentTheme = localStorage.getItem("bsTheme") || "dark"
  htmlElement.setAttribute("data-bs-theme", currentTheme)
  switchElement.checked = currentTheme === "dark"

  switchElement.addEventListener("change", function () {
    if (this.checked) {
      htmlElement.setAttribute("data-bs-theme", "dark")
      localStorage.setItem("bsTheme", "dark")
    } else {
      htmlElement.setAttribute("data-bs-theme", "light")
      localStorage.setItem("bsTheme", "light")
    }
  })
})

inputColorSelect.onchange = (event) => {
  console.log(Math.ceil(chroma(event.target.value).get("lab.l")))
  inputColorText.value = event.target.value

  const complement_color = chroma(event.target.value).set("hsl.h", "+180").hex()
  const complement_color_brighten = brighten_color(complement_color)
  const complement_color_shade = shade_color(complement_color)
  console.log("Complement: " + complement_color)
  console.log("Complement brighten 1: " + complement_color_brighten[0])
  console.log("Complement brighten 2: " + complement_color_brighten[1])
  console.log("Complement shade: " + complement_color_shade)
}

inputColorText.onchange = (event) => {
  console.log(Math.ceil(chroma(event.target.value).get("lab.l")))
  inputColorSelect.value = chroma(event.target.value).hex()

  const complement_color = chroma(event.target.value).set("hsl.h", "+180").hex()
  const complement_color_brighten = brighten_color(complement_color)
  const complement_color_shade = shade_color(complement_color)
  console.log("Complement: " + complement_color)
  console.log("Complement brighten 1: " +...