From dd06b554daa01e1b0efad362529f6b07e3f0b02f Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Wed, 22 Apr 2020 09:48:58 +0200 Subject: [PATCH] ToggleButton: compute selected background color based on current component background (issue #32) --- CHANGELOG.md | 2 ++ .../java/com/formdev/flatlaf/UIDefaultsLoader.java | 11 +++++++++-- .../com/formdev/flatlaf/util/DerivedColor.java | 4 ++-- .../com/formdev/flatlaf/FlatDarkLaf.properties | 10 +++++----- .../com/formdev/flatlaf/FlatLightLaf.properties | 10 +++++----- .../uidefaults/FlatDarkLaf_1.8.0_202-mac.txt | 14 +++++++------- .../testing/uidefaults/FlatDarkLaf_1.8.0_202.txt | 14 +++++++------- .../uidefaults/FlatLightLaf_1.8.0_202-mac.txt | 14 +++++++------- .../testing/uidefaults/FlatLightLaf_1.8.0_202.txt | 14 +++++++------- 9 files changed, 51 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dca8796..5b7a9caa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ FlatLaf Change Log ## Unreleased +- ToggleButton: Compute selected background color based on current component + background. (issue #32) - IntelliJ Themes: Fixed button and toggle button colors. (issue #86) - Updated IntelliJ Themes in demo to the latest versions. diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java index da3557d5..661cd65b 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java @@ -547,7 +547,7 @@ class UIDefaultsLoader * Syntax: lighten([color,]amount[,options]) or darken([color,]amount[,options]) * - color: a color (e.g. #f00) or a color function * - amount: percentage 0-100% - * - options: [relative] [autoInverse] [lazy] + * - options: [relative] [autoInverse] [lazy] [derived] */ private static Object parseColorLightenOrDarken( boolean lighten, List params, Function resolver, boolean reportError ) @@ -559,12 +559,14 @@ class UIDefaultsLoader boolean relative = false; boolean autoInverse = false; boolean lazy = false; + boolean derived = false; if( params.size() > nextParam ) { String options = params.get( nextParam++ ); relative = options.contains( "relative" ); autoInverse = options.contains( "autoInverse" ); lazy = options.contains( "lazy" ); + derived = options.contains( "derived" ); } ColorFunctions.ColorFunction function = lighten @@ -572,7 +574,12 @@ class UIDefaultsLoader : new ColorFunctions.Darken( amount, relative, autoInverse ); if( isDerived ) - return new DerivedColor( function ); + return new DerivedColor( null, function ); + + if( derived ) { + ColorUIResource color = (ColorUIResource) parseColorOrFunction( resolver.apply( colorStr ), resolver, reportError ); + return new DerivedColor( ColorFunctions.applyFunctions( color, function ), function ); + } if( lazy ) { return (LazyValue) t -> { diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/DerivedColor.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/DerivedColor.java index e845b9c2..4692a531 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/DerivedColor.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/DerivedColor.java @@ -32,8 +32,8 @@ public class DerivedColor { private final ColorFunction[] functions; - public DerivedColor( ColorFunction... functions ) { - super( Color.red ); + public DerivedColor( Color defaultColor, ColorFunction... functions ) { + super( (defaultColor != null) ? defaultColor : Color.red ); this.functions = functions; } diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties index 014cf7a4..28443694 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties @@ -91,8 +91,8 @@ Button.default.focusedBorderColor=#537699 Button.default.focusColor=#43688c Button.default.boldText=true -Button.toolbar.hoverBackground=#4c5052 -Button.toolbar.pressedBackground=#555a5d +Button.toolbar.hoverBackground=lighten($Button.background,1%,derived autoInverse) +Button.toolbar.pressedBackground=lighten($Button.background,4%,derived autoInverse) #---- CheckBox ---- @@ -253,11 +253,11 @@ TableHeader.bottomSeparatorColor=$TableHeader.separatorColor #---- ToggleButton ---- -ToggleButton.selectedBackground=#64696C +ToggleButton.selectedBackground=lighten($ToggleButton.background,10%,derived autoInverse) ToggleButton.selectedForeground=@foreground -ToggleButton.disabledSelectedBackground=#525658 +ToggleButton.disabledSelectedBackground=lighten($ToggleButton.background,3%,derived autoInverse) -ToggleButton.toolbar.selectedBackground=#5c6164 +ToggleButton.toolbar.selectedBackground=lighten($ToggleButton.background,7%,derived autoInverse) #---- ToolTip ---- diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties index c643863f..ee3714d5 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties @@ -93,8 +93,8 @@ Button.default.focusedBorderColor=$Button.focusedBorderColor Button.default.focusColor=$Component.focusColor Button.default.borderWidth=2 -Button.toolbar.hoverBackground=#dfdfdf -Button.toolbar.pressedBackground=#d8d8d8 +Button.toolbar.hoverBackground=darken($Button.background,12%,derived autoInverse) +Button.toolbar.pressedBackground=darken($Button.background,15%,derived autoInverse) #---- CheckBox ---- @@ -260,11 +260,11 @@ TableHeader.bottomSeparatorColor=$TableHeader.separatorColor #---- ToggleButton ---- -ToggleButton.selectedBackground=#cfcfcf +ToggleButton.selectedBackground=darken($ToggleButton.background,20%,derived autoInverse) ToggleButton.selectedForeground=@foreground -ToggleButton.disabledSelectedBackground=#dfdfdf +ToggleButton.disabledSelectedBackground=darken($ToggleButton.background,13%,derived autoInverse) -ToggleButton.toolbar.selectedBackground=#cfcfcf +ToggleButton.toolbar.selectedBackground=$ToggleButton.selectedBackground #---- ToolTip ---- diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202-mac.txt b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202-mac.txt index 3264077f..16651f1a 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202-mac.txt +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202-mac.txt @@ -94,9 +94,9 @@ Button.rollover true Button.shadow #646464 javax.swing.plaf.ColorUIResource [UI] Button.textIconGap 4 Button.textShiftOffset 0 -Button.toolbar.hoverBackground #4c5052 javax.swing.plaf.ColorUIResource [UI] +Button.toolbar.hoverBackground #4e5355 com.formdev.flatlaf.util.DerivedColor [UI] Button.toolbar.margin 3,3,3,3 javax.swing.plaf.InsetsUIResource [UI] -Button.toolbar.pressedBackground #555a5d javax.swing.plaf.ColorUIResource [UI] +Button.toolbar.pressedBackground #565a5d com.formdev.flatlaf.util.DerivedColor [UI] Button.toolbar.spacingInsets 1,2,1,2 javax.swing.plaf.InsetsUIResource [UI] ButtonUI com.formdev.flatlaf.ui.FlatButtonUI @@ -1018,7 +1018,7 @@ TitledPanelUI com.formdev.flatlaf.swingx.ui.FlatTitledPanelUI ToggleButton.background #4c5052 javax.swing.plaf.ColorUIResource [UI] ToggleButton.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatButtonBorder [UI] ToggleButton.darkShadow #7e7e7e javax.swing.plaf.ColorUIResource [UI] -ToggleButton.disabledSelectedBackground #525658 javax.swing.plaf.ColorUIResource [UI] +ToggleButton.disabledSelectedBackground #53585a com.formdev.flatlaf.util.DerivedColor [UI] ToggleButton.disabledText #777777 javax.swing.plaf.ColorUIResource [UI] ToggleButton.font [active] $defaultFont [UI] ToggleButton.foreground #bbbbbb javax.swing.plaf.ColorUIResource [UI] @@ -1028,7 +1028,7 @@ ToggleButton.light #313131 javax.swing.plaf.ColorUIResource [UI] ToggleButton.margin 2,14,2,14 javax.swing.plaf.InsetsUIResource [UI] ToggleButton.pressedBackground #ff0000 com.formdev.flatlaf.util.DerivedColor [UI] ToggleButton.rollover true -ToggleButton.selectedBackground #64696c javax.swing.plaf.ColorUIResource [UI] +ToggleButton.selectedBackground #656a6c com.formdev.flatlaf.util.DerivedColor [UI] ToggleButton.selectedForeground #bbbbbb javax.swing.plaf.ColorUIResource [UI] ToggleButton.shadow #646464 javax.swing.plaf.ColorUIResource [UI] ToggleButton.tab.disabledUnderlineColor #7a7a7a javax.swing.plaf.ColorUIResource [UI] @@ -1038,9 +1038,9 @@ ToggleButton.tab.underlineColor #4a88c7 javax.swing.plaf.ColorUIResource [UI] ToggleButton.tab.underlineHeight 2 ToggleButton.textIconGap 4 ToggleButton.textShiftOffset 0 -ToggleButton.toolbar.hoverBackground #4c5052 javax.swing.plaf.ColorUIResource [UI] -ToggleButton.toolbar.pressedBackground #555a5d javax.swing.plaf.ColorUIResource [UI] -ToggleButton.toolbar.selectedBackground #5c6164 javax.swing.plaf.ColorUIResource [UI] +ToggleButton.toolbar.hoverBackground #4e5355 com.formdev.flatlaf.util.DerivedColor [UI] +ToggleButton.toolbar.pressedBackground #565a5d com.formdev.flatlaf.util.DerivedColor [UI] +ToggleButton.toolbar.selectedBackground #5d6265 com.formdev.flatlaf.util.DerivedColor [UI] ToggleButtonUI com.formdev.flatlaf.ui.FlatToggleButtonUI diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt index 48c4e209..2b5b4bb2 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt @@ -94,9 +94,9 @@ Button.rollover true Button.shadow #646464 javax.swing.plaf.ColorUIResource [UI] Button.textIconGap 4 Button.textShiftOffset 0 -Button.toolbar.hoverBackground #4c5052 javax.swing.plaf.ColorUIResource [UI] +Button.toolbar.hoverBackground #4e5355 com.formdev.flatlaf.util.DerivedColor [UI] Button.toolbar.margin 3,3,3,3 javax.swing.plaf.InsetsUIResource [UI] -Button.toolbar.pressedBackground #555a5d javax.swing.plaf.ColorUIResource [UI] +Button.toolbar.pressedBackground #565a5d com.formdev.flatlaf.util.DerivedColor [UI] Button.toolbar.spacingInsets 1,2,1,2 javax.swing.plaf.InsetsUIResource [UI] ButtonUI com.formdev.flatlaf.ui.FlatButtonUI @@ -1016,7 +1016,7 @@ TitledPanelUI com.formdev.flatlaf.swingx.ui.FlatTitledPanelUI ToggleButton.background #4c5052 javax.swing.plaf.ColorUIResource [UI] ToggleButton.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatButtonBorder [UI] ToggleButton.darkShadow #7e7e7e javax.swing.plaf.ColorUIResource [UI] -ToggleButton.disabledSelectedBackground #525658 javax.swing.plaf.ColorUIResource [UI] +ToggleButton.disabledSelectedBackground #53585a com.formdev.flatlaf.util.DerivedColor [UI] ToggleButton.disabledText #777777 javax.swing.plaf.ColorUIResource [UI] ToggleButton.font [active] $defaultFont [UI] ToggleButton.foreground #bbbbbb javax.swing.plaf.ColorUIResource [UI] @@ -1026,7 +1026,7 @@ ToggleButton.light #313131 javax.swing.plaf.ColorUIResource [UI] ToggleButton.margin 2,14,2,14 javax.swing.plaf.InsetsUIResource [UI] ToggleButton.pressedBackground #ff0000 com.formdev.flatlaf.util.DerivedColor [UI] ToggleButton.rollover true -ToggleButton.selectedBackground #64696c javax.swing.plaf.ColorUIResource [UI] +ToggleButton.selectedBackground #656a6c com.formdev.flatlaf.util.DerivedColor [UI] ToggleButton.selectedForeground #bbbbbb javax.swing.plaf.ColorUIResource [UI] ToggleButton.shadow #646464 javax.swing.plaf.ColorUIResource [UI] ToggleButton.tab.disabledUnderlineColor #7a7a7a javax.swing.plaf.ColorUIResource [UI] @@ -1036,9 +1036,9 @@ ToggleButton.tab.underlineColor #4a88c7 javax.swing.plaf.ColorUIResource [UI] ToggleButton.tab.underlineHeight 2 ToggleButton.textIconGap 4 ToggleButton.textShiftOffset 0 -ToggleButton.toolbar.hoverBackground #4c5052 javax.swing.plaf.ColorUIResource [UI] -ToggleButton.toolbar.pressedBackground #555a5d javax.swing.plaf.ColorUIResource [UI] -ToggleButton.toolbar.selectedBackground #5c6164 javax.swing.plaf.ColorUIResource [UI] +ToggleButton.toolbar.hoverBackground #4e5355 com.formdev.flatlaf.util.DerivedColor [UI] +ToggleButton.toolbar.pressedBackground #565a5d com.formdev.flatlaf.util.DerivedColor [UI] +ToggleButton.toolbar.selectedBackground #5d6265 com.formdev.flatlaf.util.DerivedColor [UI] ToggleButtonUI com.formdev.flatlaf.ui.FlatToggleButtonUI diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202-mac.txt b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202-mac.txt index 06f41702..2c9539da 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202-mac.txt +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202-mac.txt @@ -95,9 +95,9 @@ Button.rollover true Button.shadow #c4c4c4 javax.swing.plaf.ColorUIResource [UI] Button.textIconGap 4 Button.textShiftOffset 0 -Button.toolbar.hoverBackground #dfdfdf javax.swing.plaf.ColorUIResource [UI] +Button.toolbar.hoverBackground #e0e0e0 com.formdev.flatlaf.util.DerivedColor [UI] Button.toolbar.margin 3,3,3,3 javax.swing.plaf.InsetsUIResource [UI] -Button.toolbar.pressedBackground #d8d8d8 javax.swing.plaf.ColorUIResource [UI] +Button.toolbar.pressedBackground #d9d9d9 com.formdev.flatlaf.util.DerivedColor [UI] Button.toolbar.spacingInsets 1,2,1,2 javax.swing.plaf.InsetsUIResource [UI] ButtonUI com.formdev.flatlaf.ui.FlatButtonUI @@ -1020,7 +1020,7 @@ TitledPanelUI com.formdev.flatlaf.swingx.ui.FlatTitledPanelUI ToggleButton.background #ffffff javax.swing.plaf.ColorUIResource [UI] ToggleButton.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatButtonBorder [UI] ToggleButton.darkShadow #9e9e9e javax.swing.plaf.ColorUIResource [UI] -ToggleButton.disabledSelectedBackground #dfdfdf javax.swing.plaf.ColorUIResource [UI] +ToggleButton.disabledSelectedBackground #dedede com.formdev.flatlaf.util.DerivedColor [UI] ToggleButton.disabledText #8c8c8c javax.swing.plaf.ColorUIResource [UI] ToggleButton.font [active] $defaultFont [UI] ToggleButton.foreground #000000 javax.swing.plaf.ColorUIResource [UI] @@ -1030,7 +1030,7 @@ ToggleButton.light #e3e3e3 javax.swing.plaf.ColorUIResource [UI] ToggleButton.margin 2,14,2,14 javax.swing.plaf.InsetsUIResource [UI] ToggleButton.pressedBackground #ff0000 com.formdev.flatlaf.util.DerivedColor [UI] ToggleButton.rollover true -ToggleButton.selectedBackground #cfcfcf javax.swing.plaf.ColorUIResource [UI] +ToggleButton.selectedBackground #cccccc com.formdev.flatlaf.util.DerivedColor [UI] ToggleButton.selectedForeground #000000 javax.swing.plaf.ColorUIResource [UI] ToggleButton.shadow #c4c4c4 javax.swing.plaf.ColorUIResource [UI] ToggleButton.tab.disabledUnderlineColor #ababab javax.swing.plaf.ColorUIResource [UI] @@ -1040,9 +1040,9 @@ ToggleButton.tab.underlineColor #4083c9 javax.swing.plaf.ColorUIResource [UI] ToggleButton.tab.underlineHeight 2 ToggleButton.textIconGap 4 ToggleButton.textShiftOffset 0 -ToggleButton.toolbar.hoverBackground #dfdfdf javax.swing.plaf.ColorUIResource [UI] -ToggleButton.toolbar.pressedBackground #d8d8d8 javax.swing.plaf.ColorUIResource [UI] -ToggleButton.toolbar.selectedBackground #cfcfcf javax.swing.plaf.ColorUIResource [UI] +ToggleButton.toolbar.hoverBackground #e0e0e0 com.formdev.flatlaf.util.DerivedColor [UI] +ToggleButton.toolbar.pressedBackground #d9d9d9 com.formdev.flatlaf.util.DerivedColor [UI] +ToggleButton.toolbar.selectedBackground #cccccc com.formdev.flatlaf.util.DerivedColor [UI] ToggleButtonUI com.formdev.flatlaf.ui.FlatToggleButtonUI diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt index 163d5c60..8e25985d 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt @@ -95,9 +95,9 @@ Button.rollover true Button.shadow #c4c4c4 javax.swing.plaf.ColorUIResource [UI] Button.textIconGap 4 Button.textShiftOffset 0 -Button.toolbar.hoverBackground #dfdfdf javax.swing.plaf.ColorUIResource [UI] +Button.toolbar.hoverBackground #e0e0e0 com.formdev.flatlaf.util.DerivedColor [UI] Button.toolbar.margin 3,3,3,3 javax.swing.plaf.InsetsUIResource [UI] -Button.toolbar.pressedBackground #d8d8d8 javax.swing.plaf.ColorUIResource [UI] +Button.toolbar.pressedBackground #d9d9d9 com.formdev.flatlaf.util.DerivedColor [UI] Button.toolbar.spacingInsets 1,2,1,2 javax.swing.plaf.InsetsUIResource [UI] ButtonUI com.formdev.flatlaf.ui.FlatButtonUI @@ -1018,7 +1018,7 @@ TitledPanelUI com.formdev.flatlaf.swingx.ui.FlatTitledPanelUI ToggleButton.background #ffffff javax.swing.plaf.ColorUIResource [UI] ToggleButton.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatButtonBorder [UI] ToggleButton.darkShadow #9e9e9e javax.swing.plaf.ColorUIResource [UI] -ToggleButton.disabledSelectedBackground #dfdfdf javax.swing.plaf.ColorUIResource [UI] +ToggleButton.disabledSelectedBackground #dedede com.formdev.flatlaf.util.DerivedColor [UI] ToggleButton.disabledText #8c8c8c javax.swing.plaf.ColorUIResource [UI] ToggleButton.font [active] $defaultFont [UI] ToggleButton.foreground #000000 javax.swing.plaf.ColorUIResource [UI] @@ -1028,7 +1028,7 @@ ToggleButton.light #e3e3e3 javax.swing.plaf.ColorUIResource [UI] ToggleButton.margin 2,14,2,14 javax.swing.plaf.InsetsUIResource [UI] ToggleButton.pressedBackground #ff0000 com.formdev.flatlaf.util.DerivedColor [UI] ToggleButton.rollover true -ToggleButton.selectedBackground #cfcfcf javax.swing.plaf.ColorUIResource [UI] +ToggleButton.selectedBackground #cccccc com.formdev.flatlaf.util.DerivedColor [UI] ToggleButton.selectedForeground #000000 javax.swing.plaf.ColorUIResource [UI] ToggleButton.shadow #c4c4c4 javax.swing.plaf.ColorUIResource [UI] ToggleButton.tab.disabledUnderlineColor #ababab javax.swing.plaf.ColorUIResource [UI] @@ -1038,9 +1038,9 @@ ToggleButton.tab.underlineColor #4083c9 javax.swing.plaf.ColorUIResource [UI] ToggleButton.tab.underlineHeight 2 ToggleButton.textIconGap 4 ToggleButton.textShiftOffset 0 -ToggleButton.toolbar.hoverBackground #dfdfdf javax.swing.plaf.ColorUIResource [UI] -ToggleButton.toolbar.pressedBackground #d8d8d8 javax.swing.plaf.ColorUIResource [UI] -ToggleButton.toolbar.selectedBackground #cfcfcf javax.swing.plaf.ColorUIResource [UI] +ToggleButton.toolbar.hoverBackground #e0e0e0 com.formdev.flatlaf.util.DerivedColor [UI] +ToggleButton.toolbar.pressedBackground #d9d9d9 com.formdev.flatlaf.util.DerivedColor [UI] +ToggleButton.toolbar.selectedBackground #cccccc com.formdev.flatlaf.util.DerivedColor [UI] ToggleButtonUI com.formdev.flatlaf.ui.FlatToggleButtonUI