<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Contrasting text on changing background]]></title><description><![CDATA[<p dir="auto">I’m trying to come up with a configuration that (mostly) guarantees text visibility in the following way:</p>
<p dir="auto">White background shows black text and vice versa<br />
Yellow background shows blue text and vice versa</p>
<p dir="auto">…the point being that the text color should ALWAYS be different from the background color, so that it can be read  even if the background color changes. I’d also like that to happen with randomly-selected wallpapers.<br />
So I’m wondering: Is it something I can do with CSS?<br />
Or do I need to try something else?</p>
]]></description><link>https://forum.magicmirror.builders/topic/15314/contrasting-text-on-changing-background</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Jul 2026 08:57:02 GMT</lastBuildDate><atom:link href="https://forum.magicmirror.builders/topic/15314.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 27 Jul 2021 12:36:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Contrasting text on changing background on Wed, 26 Feb 2025 16:17:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sdetweil" aria-label="Profile: sdetweil">@<bdi>sdetweil</bdi></a> said in <a href="/post/93190">Contrasting text on changing background</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mwm341" aria-label="Profile: mwm341">@<bdi>mwm341</bdi></a> I want that too. text over photos.  haven’t figured out a way yet.</p>
</blockquote>
<p dir="auto">I kludged something together today with the MMM-Wallpaper module. It’s not elegant, but it’s been working for me so far.</p>
<p dir="auto">What I’m doing is having the module draw the image onto an off-screen canvas and compute its average brightness using the luminance formula. Based on whether this brightness exceeds a defined threshold (115 has been working for me), the module then updates a global CSS variable with either a light or dark text color.</p>
<p dir="auto">I’m using <code>color: var(--dynamic-text-color);</code> as the variable. Since it’s determining global brightness, it can still “miss” picking a good color for each module.</p>
<p dir="auto">Depending on compute load, I’m thinking an update would be to determine that brightness value for each quadrant of an image then creating a variable for each quadrant and setting the text style in those areas to that color.  A downside to this approach is that it won’t work perfectly with different display aspect ratios, but if you’re only ever using a 16:9 display that would be mitigated.</p>
<p dir="auto"><strong>Brightness function:</strong></p>
<pre><code>  // Helper function to compute average brightness of an image.
  getAverageBrightness: function(image, callback) {
    var canvas = document.createElement("canvas");
    var width = image.naturalWidth;
    var height = image.naturalHeight;
    canvas.width = width;
    canvas.height = height;
    var context = canvas.getContext("2d");

    context.drawImage(image, 0, 0, width, height);

    try {
      var imageData = context.getImageData(0, 0, width, height);
    } catch (error) {
      console.error("Error accessing image data:", error);
      callback(255);  // Assume bright background if error.
      return;
    }
    
    var data = imageData.data;
    var colorSum = 0;
    var pixels = data.length / 4;

    for (var i = 0; i &lt; data.length; i += 4) {
      var r = data[i];
      var g = data[i + 1];
      var b = data[i + 2];
      // Calculate brightness using the luminance formula.
      var brightness = 0.299 * r + 0.587 * g + 0.114 * b;
      colorSum += brightness;
    }

    var averageBrightness = colorSum / pixels;
    callback(averageBrightness);
  },
</code></pre>
<p dir="auto"><strong>Updated onImageLoaded function:</strong></p>
<pre><code>  onImageLoaded: function(imageData, element) {
    var self = this;
    return () =&gt; {
      self.resetLoadImageTimer();

      element.className = `wallpaper ${self.config.crossfade ? "crossfade-image" : ""}`;
      element.style.opacity = 1;
      
      // Analyze the image brightness and adjust text color accordingly.
      // This will update both the module's caption and a global CSS variable.
      self.getAverageBrightness(element, function(brightness) {
        var threshold = 128; // Adjust this threshold as needed.
        var textColor = brightness &gt; threshold ? "black" : "white";
        self.title.style.color = textColor;
        // Set a global CSS variable for dynamic text color.
        document.documentElement.style.setProperty('--dynamic-text-color', textColor);
      });

      self.title.style.display = "none";

      setTimeout(() =&gt; {
        var caption = imageData.caption;
        if (self.config.caption &amp;&amp; caption) {
          self.title.innerHTML = caption;
          self.title.style.display = "initial";
        }

        if (self.imageElement !== null) {
          self.content.removeChild(self.imageElement);
        }
        self.imageElement = self.nextImageElement;
        self.nextImageElement = null;
      }, self.config.crossfade ? 1000 : 0);
    };
  },
</code></pre>
]]></description><link>https://forum.magicmirror.builders/post/124332</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/124332</guid><dc:creator><![CDATA[botts85]]></dc:creator><pubDate>Wed, 26 Feb 2025 16:17:16 GMT</pubDate></item><item><title><![CDATA[Reply to Contrasting text on changing background on Thu, 29 Jul 2021 11:11:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sdetweil" aria-label="Profile: sdetweil">@<bdi>sdetweil</bdi></a> I guess one could cheat a little by doing something with an animated (slow) colour gradient, assuming that the text will be visible some of the time (in the place where I do it) - but that not much of a solution… (sigh)</p>
]]></description><link>https://forum.magicmirror.builders/post/93220</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/93220</guid><dc:creator><![CDATA[mwm341]]></dc:creator><pubDate>Thu, 29 Jul 2021 11:11:18 GMT</pubDate></item><item><title><![CDATA[Reply to Contrasting text on changing background on Tue, 27 Jul 2021 12:59:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mwm341" aria-label="Profile: mwm341">@<bdi>mwm341</bdi></a> I want that too. text over photos.  haven’t figured out a way yet.</p>
<p dir="auto">some kind of css transform i think</p>
<p dir="auto"><a href="https://css-tricks.com/methods-contrasting-text-backgrounds/" target="_blank" rel="noopener noreferrer nofollow ugc">https://css-tricks.com/methods-contrasting-text-backgrounds/</a><br />
<a href="https://stackoverflow.com/questions/50986688/invert-text-color-based-on-background-in-css" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/50986688/invert-text-color-based-on-background-in-css</a></p>
]]></description><link>https://forum.magicmirror.builders/post/93190</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/93190</guid><dc:creator><![CDATA[sdetweil]]></dc:creator><pubDate>Tue, 27 Jul 2021 12:59:35 GMT</pubDate></item></channel></rss>