Improve contrast of tooltips on some Linux machines that show white text against yellow ballon

This commit is contained in:
Bane Stojkovic
2025-12-26 12:06:31 +01:00
parent f74fe19b77
commit 04e44e174e
3 changed files with 79 additions and 0 deletions
@@ -433,6 +433,10 @@
<changeType>IMPROVEMENT</changeType>
<description>Improved sensitivity of 'Multimeter Probe' that required pixel-level precision to detect connectivity; improved rendering of the probe in outline mode</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType>
<description>Improve contrast of tooltips on some Linux machines that show white text against yellow ballon</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>BUG_FIX</changeType>
<description>Fixed underside rendering and mirroring position issues that affected only SPDT 'Mini Toggle Switch' types</description>
@@ -42,6 +42,7 @@ import org.diylc.serialization.ProjectFileManager;
import org.diylc.swing.gui.MainFrame;
import org.diylc.swing.gui.TemplateDialog;
import org.diylc.swing.plugins.file.ProjectDrawingProvider;
import org.diylc.utils.SwingUtils;
/**
* Main class that runs DIYLC.
@@ -138,6 +139,9 @@ public class DIYLCStarter {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// Fix tooltip colors for Linux systems where they may be unreadable
SwingUtils.fixTooltipColors();
// Apply configurable font scaling from VM argument: -Dorg.diylc.fontScaleFactor=X
org.diylc.utils.FontScalingUtils.applyFontScaling();
} catch (Exception e) {
@@ -0,0 +1,71 @@
/*
*
* DIY Layout Creator (DIYLC).
* Copyright (c) 2009-2025 held jointly by the individual authors.
*
* This file is part of DIYLC.
*
* DIYLC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DIYLC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.diylc.utils;
import java.awt.Color;
import java.util.function.Function;
import javax.swing.UIManager;
/**
* Utility class for Swing UI-related operations.
*
* @author Branislav Stojkovic
*/
public class SwingUtils {
/**
* Fixes tooltip colors for Linux systems where they may be unreadable.
* Some Linux L&F themes set white text on light backgrounds, making tooltips invisible.
* This method ensures tooltips always have dark text on light background for readability.
*
* Should be called after UIManager.setLookAndFeel() to override system defaults.
*/
public static void fixTooltipColors() {
Color tooltipBg = UIManager.getColor("ToolTip.background");
Color tooltipFg = UIManager.getColor("ToolTip.foreground");
// Helper function to calculate brightness (0.0 = black, 1.0 = white)
Function<Color, Double> getBrightness = (c) -> {
return (0.299 * c.getRed() + 0.587 * c.getGreen() + 0.114 * c.getBlue()) / 255.0;
};
// Check if we have a problematic color combination (light text on light background)
boolean needsFix = false;
if (tooltipFg != null && tooltipBg != null) {
double fgBrightness = getBrightness.apply(tooltipFg);
double bgBrightness = getBrightness.apply(tooltipBg);
// If both foreground and background are light, it's unreadable
if (fgBrightness > 0.7 && bgBrightness > 0.5) {
needsFix = true;
}
} else if (tooltipFg == null || tooltipBg == null) {
// Missing color definitions - set safe defaults
needsFix = true;
}
if (needsFix) {
UIManager.put("ToolTip.background", new Color(255, 255, 220)); // Light yellow
UIManager.put("ToolTip.foreground", Color.BLACK);
}
}
}