.eximgcolor to the image widget Then put the the class .excolor to the container that the background color must change.

changelog

Live Example

[own_shortcode1]

JavaScript

<script>
(function () {
  const WHITE_THR = 245;
  const BLACK_THR = 15;
  const CHROMA_THR = 12;

  async function processWidget(widget) {
    if (!widget || widget.nodeType !== 1) return;
    const img = widget.querySelector("img");
    if (!img) return;
    const key = img.currentSrc || img.src || "";
    if (widget.dataset.eximgProcessed === key) return;
    try {
      if (!img.complete || !img.naturalWidth) {
        if (img.decode) await img.decode();
      }
    } catch (_) {}
    if (!img.naturalWidth) return;
    try {
      const w = img.naturalWidth, h = img.naturalHeight || 1;
      const sampleH = Math.min(15, h);
      const canvas = document.createElement("canvas");
      canvas.width = w;
      canvas.height = sampleH;
      const ctx = canvas.getContext("2d", { willReadFrequently: true });
      let bestS = -1, br = 0, bg = 0, bb = 0;
      for (let y = 0; y <= h - sampleH; y += sampleH) {
        ctx.clearRect(0, 0, w, sampleH);
        ctx.drawImage(img, 0, y, w, sampleH, 0, 0, w, sampleH);
        const data = ctx.getImageData(0, 0, w, sampleH).data;
        for (let i = 0; i < data.length; i += 4) {
          const R = data[i], G = data[i + 1], B = data[i + 2];
          if ((R >= WHITE_THR && G >= WHITE_THR && B >= WHITE_THR) ||
              (R <= BLACK_THR && G <= BLACK_THR && B <= BLACK_THR)) continue;
          const maxc = Math.max(R, G, B), minc = Math.min(R, G, B);
          const chroma = maxc - minc;
          if (chroma < CHROMA_THR || maxc === 0) continue;
          const S = chroma / maxc;
          if (S > bestS) { bestS = S; br = R; bg = G; bb = B; }
        }
      }
      if (bestS >= 0) {
        const container = widget.closest(".excolor") || widget;
        container.style.backgroundColor = `rgba(${br}, ${bg}, ${bb}, 0.2)`;
      }
      widget.dataset.eximgProcessed = key;
    } catch (e) {
      console.error("Erreur analyse image :", e);
    }
  }

  function processAll() {
    document.querySelectorAll(".eximgcolor").forEach((w) => {
      requestAnimationFrame(() => processWidget(w));
    });
  }

  const mo = new MutationObserver((mutations) => {
    for (const m of mutations) {
      if (m.type === "childList") {
        m.addedNodes.forEach((node) => {
          if (!(node instanceof Element)) return;
          if (node.matches?.(".eximgcolor")) {
            processWidget(node);
          }
          node.querySelectorAll?.(".eximgcolor").forEach(processWidget);
          if (node.matches?.(".eximgcolor img")) {
            const w = node.closest(".eximgcolor");
            if (w) processWidget(w);
          }
        });
      }
      if (m.type === "attributes" && m.attributeName === "src") {
        const el = m.target;
        if (el instanceof HTMLImageElement) {
          const w = el.closest(".eximgcolor");
          if (w) {
            delete w.dataset.eximgProcessed;
            processWidget(w);
          }
        }
      }
    }
  });

  function start() {
    processAll();
    mo.observe(document.documentElement, {
      childList: true,
      subtree: true,
      attributes: true,
      attributeFilter: ["src"]
    });
  }

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", start, { once: true });
  } else {
    start();
  }
})();
</script>

Extract CSS color from voxel image field

The Mission

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

There are no results matching your search

AI Assistant