Browse Source

New form logic

Kirk Trombley 2 years ago
parent
commit
c4899425d2
3 changed files with 195 additions and 0 deletions
  1. 52 0
      form.js
  2. 127 0
      index.html
  3. 16 0
      styles.css

+ 52 - 0
form.js

@@ -0,0 +1,52 @@
+const selectors = {
+  get sortControl() {
+    return document.forms.sortControl.elements;
+  },
+  get clusterControl() {
+    return document.forms.clusterControl.elements;
+  },
+};
+
+const onMetricChange = (elements) => {
+  elements.sortOrderLabel.value = elements.sortOrder.checked
+    ? "Maximizing"
+    : "Minimizing";
+  const kind = elements.metricKind.value;
+  elements.whole.disabled = kind !== "whole";
+  elements.mean.disabled = kind !== "mean";
+  elements.statistic.disabled = kind !== "statistic";
+  elements.sortMetric.value = elements[kind].value;
+};
+
+const syncColorInputs = (inputValue) => {
+  console.log(inputValue);
+  const colorInput = "#" + (inputValue?.replace("#", "") ?? "FFFFFF");
+
+  if (colorInput.length !== 7) {
+    return;
+  }
+
+  const rgb = d3.color(colorInput);
+  if (!rgb) {
+    return;
+  }
+
+  const refomatted = rgb.formatHex();
+  selectors.sortControl.colorText.value = refomatted;
+  selectors.sortControl.colorPicker.value = refomatted;
+};
+
+window.onload = () => {
+  const metricTemplate = document.getElementById("metric-form-template").content;
+
+  selectors.sortControl.metric.appendChild(metricTemplate.cloneNode(true));
+  selectors.sortControl.metricKind.value = "whole";
+  selectors.sortControl.whole.value = "alpha";
+  onMetricChange(selectors.sortControl);
+
+  selectors.clusterControl.metric.appendChild(metricTemplate.cloneNode(true));
+  selectors.clusterControl.sortOrder.checked = true;
+  selectors.clusterControl.metricKind.value = "statistic";
+  selectors.clusterControl.statistic.value = "importance";
+  onMetricChange(selectors.clusterControl);
+};

+ 127 - 0
index.html

@@ -0,0 +1,127 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8" />
+    <title>Pokemon By Color</title>
+    <link rel="stylesheet" href="styles.css" />
+    <script src="https://unpkg.com/d3-color@3.0.1/dist/d3-color.min.js"></script>
+    <script src="https://unpkg.com/d3-cam02@0.1.5/build/d3-cam02.min.js"></script>
+    <script src="database-v3.js"></script>
+    <script src="form.js"></script>
+  </head>
+  <body>
+    <noscript>Requires javascript</noscript>
+
+    <template id="metric-form-template">
+      <div>
+        <label>
+          <input type="checkbox" name="sortOrder" hidden />
+          <output name="sortOrderLabel"></output>
+          (click to toggle)
+        </label>
+      </div>
+      <div>
+        <label>
+          <input type="radio" name="metricKind" value="whole" />
+          Set Comparison
+        </label>
+      </div>
+      <div>
+        <label>
+          <input type="radio" name="metricKind" value="mean" />
+          Mean Comparison
+        </label>
+      </div>
+      <div>
+        <label>
+          <input type="radio" name="metricKind" value="statistic" />
+          Set Statistics
+        </label>
+      </div>
+      <select name="whole" disabled>
+        <option value="sigma">RMS Deviation (σ)</option>
+        <option value="bigTheta">Cosine Difference (Θ)</option>
+        <option value="alpha">Geometric Difference (α)</option>
+      </select>
+      <select name="mean" disabled>
+        <option value="theta">Angular Difference (θ)</option>
+        <option value="phi">Hue Azimuth (ϕ)</option>
+        <option value="delta">Euclidean (δ)</option>
+        <option value="manhattan">Manhattan (M)</option>
+        <option value="ch">Chebyshev (Ч)</option>
+        <option value="lightnessDiff">Lightness (ℓ)</option>
+      </select>
+      <select name="statistic" disabled>
+        <option value="inertia">Inertia (I)</option>
+        <option value="variance">Variance (V)</option>
+        <option value="muNuAngle">Mu-Nu Angle (Φ)</option>
+        <option value="size">Size (N)</option>
+        <option value="lightness">Mean Lightness (L)</option>
+        <option value="chroma">Mean Chroma (C)</option>
+        <option value="importance">Visual Importance (β)</option>
+      </select>
+      <output name="sortMetric" hidden aria-hidden="true"></output>
+    </template>
+
+    <form action="javascript:void(0);" id="sortControl" autocomplete="off">
+      <div>Target Color</div>
+      <div>
+        <input
+          name="colorText"
+          type="text"
+          maxlength="7"
+          oninput="syncColorInputs(event.target.value)"
+        />
+        <input
+          name="colorPicker"
+          type="color"
+          onchange="syncColorInputs(event.target.value)"
+        />
+      </div>
+      <div>Color Space</div>
+      <div>
+        <label>
+          <input type="radio" name="color-space" value="jab" checked />
+          CIECAM
+        </label>
+      </div>
+      <div>
+        <label>
+          <input type="radio" name="color-space" value="rgb" />
+          sRGB
+        </label>
+      </div>
+      <fieldset name="metric" onchange="onMetricChange(event.target.form.elements)">
+        <legend>Distance Metric</legend>
+        <!-- template mount point -->
+      </fieldset>
+    </form>
+
+    <form action="javascript:void(0);" id="clusterControl" autocomplete="off">
+      <fieldset
+        onchange="
+          const elements = event.target.form.elements;
+          elements.metric.hidden = elements.useClusters.value === 'off';
+        "
+      >
+        <legend>Compare With</legend>
+        <div>
+          <label>
+            <input type="radio" name="useClusters" value="off" />
+            Whole Image
+          </label>
+        </div>
+        <div>
+          <label>
+            <input type="radio" name="useClusters" value="on" checked />
+            Best Cluster
+          </label>
+        </div>
+        <fieldset name="metric" onchange="onMetricChange(event.target.form.elements)">
+          <legend>Cluster Ranking</legend>
+          <!-- template mount point -->
+        </fieldset>
+      </fieldset>
+    </form>
+  </body>
+</html>

+ 16 - 0
styles.css

@@ -0,0 +1,16 @@
+:root {
+  --highlight: #ddd;
+  --background: #222;
+  --tile-width: 484px;
+}
+
+body {
+  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu",
+    "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
+  color: var(--highlight);
+  background-color: var(--background);
+}
+
+select:disabled {
+  display: none;
+}