script.js 699 B

12345678910111213141516171819202122
  1. const targetColor = U.obs();
  2. const randomizeTargetColor = () => {
  3. targetColor.value = d3
  4. .hsl(Math.random() * 360, Math.random(), Math.random())
  5. .formatHex();
  6. };
  7. U.element("colorSelect", ({ randomButton }, form) => {
  8. U.field(form.elements.colorText, { obs: targetColor });
  9. U.field(form.elements.colorPicker, { obs: targetColor });
  10. U.form(form);
  11. randomButton.addEventListener("click", randomizeTargetColor);
  12. });
  13. randomizeTargetColor();
  14. U.reactive(() => {
  15. const contrast = getContrastingTextColor(targetColor.value);
  16. const style = document.querySelector(":root").style;
  17. style.setProperty("--background", targetColor.value);
  18. style.setProperty("--highlight", contrast);
  19. });