From d398cb91aa17c4df6e138717e8cd3b38e206a28e Mon Sep 17 00:00:00 2001 From: weisj Date: Tue, 26 Jan 2021 22:59:01 +0100 Subject: [PATCH] Derive selection color from accent color (on windows) --- core/src/test/java/ui/ComponentDemo.java | 1 + windows/src/main/cpp/ThemeInfo.cpp | 1 - .../windows/WindowsThemePreferenceProvider.java | 17 ++++++++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/ui/ComponentDemo.java b/core/src/test/java/ui/ComponentDemo.java index c899744d..50683bea 100644 --- a/core/src/test/java/ui/ComponentDemo.java +++ b/core/src/test/java/ui/ComponentDemo.java @@ -63,6 +63,7 @@ public interface ComponentDemo { static void showDemo(final ComponentDemo demo, final boolean asDialog) { LafManager.enabledPreferenceChangeReporting(false); + LafManager.addThemePreferenceChangeListener(LafManager::installTheme); LafManager.setDecorationsEnabled(true); LafManager.setLogLevel(Level.FINE); SwingUtilities.invokeLater(() -> { diff --git a/windows/src/main/cpp/ThemeInfo.cpp b/windows/src/main/cpp/ThemeInfo.cpp index e914602e..b5f1dcd9 100644 --- a/windows/src/main/cpp/ThemeInfo.cpp +++ b/windows/src/main/cpp/ThemeInfo.cpp @@ -24,7 +24,6 @@ */ #include "com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows.h" -#include #include #include #include diff --git a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsThemePreferenceProvider.java b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsThemePreferenceProvider.java index 257948a0..566577ba 100644 --- a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsThemePreferenceProvider.java +++ b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsThemePreferenceProvider.java @@ -24,6 +24,7 @@ package com.github.weisj.darklaf.platform.windows; import java.awt.*; import java.util.function.Consumer; +import com.github.weisj.darklaf.color.DarkColorModelHSB; import com.github.weisj.darklaf.theme.info.*; public class WindowsThemePreferenceProvider implements ThemePreferenceProvider { @@ -48,10 +49,18 @@ public class WindowsThemePreferenceProvider implements ThemePreferenceProvider { ContrastRule contrastRule = highContrast ? ContrastRule.HIGH_CONTRAST : ContrastRule.STANDARD; ColorToneRule toneRule = darkMode ? ColorToneRule.DARK : ColorToneRule.LIGHT; FontSizeRule fontSizeRule = FontSizeRule.relativeAdjustment(fontScaling / 100f); - AccentColorRule accentColorRule = AccentColorRule.fromColor(createColorFromRGB(accentColorRGB)); + Color accentColor = createColorFromRGB(accentColorRGB); + Color selectionColor = deriveSelectionColor(accentColor); + AccentColorRule accentColorRule = AccentColorRule.fromColor(accentColor, selectionColor); return new PreferredThemeStyle(contrastRule, toneRule, accentColorRule, fontSizeRule); } + private Color deriveSelectionColor(final Color color) { + if (color == null) return null; + double[] hsb = DarkColorModelHSB.RGBtoHSBValues(color.getRed(), color.getGreen(), color.getBlue()); + return DarkColorModelHSB.getColorFromHSBValues(hsb[0], hsb[1] / 2.5, Math.min(1, hsb[2] * 1.2)); + } + private Color createColorFromRGB(final int rgb) { if (rgb == 0) return null; return new Color(rgb); @@ -98,6 +107,12 @@ public class WindowsThemePreferenceProvider implements ThemePreferenceProvider { return WindowsLibrary.get().isLoaded(); } + @Override + public boolean supportsNativeSelectionColor() { + // Depends on accent color as it is derived from it. + return supportsNativeAccentColor(); + } + @Override public boolean supportsNativeFontSize() { return WindowsLibrary.get().isLoaded();