Accent color:

- Demo: added accent color switching to toolbar
- added `FlatLaf.setGlobalExtraDefaults()` for easy setting accent color at runtime

(issue #233)
This commit is contained in:
Karl Tauber
2021-08-31 14:06:41 +02:00
parent 7f9cf6f45c
commit d333d0c9e4
7 changed files with 220 additions and 19 deletions

View File

@@ -86,6 +86,7 @@ public abstract class FlatLaf
private static final String DESKTOPFONTHINTS = "awt.font.desktophints";
private static List<Object> customDefaultsSources;
private static Map<String, String> globalExtraDefaults;
private Map<String, String> extraDefaults;
private String desktopPropertyName;
@@ -465,11 +466,14 @@ public abstract class FlatLaf
}
protected Properties getAdditionalDefaults() {
if( extraDefaults == null )
if( globalExtraDefaults == null && extraDefaults == null )
return null;
Properties properties = new Properties();
properties.putAll( extraDefaults );
if( globalExtraDefaults != null )
properties.putAll( globalExtraDefaults );
if( extraDefaults != null )
properties.putAll( extraDefaults );
return properties;
}
@@ -785,6 +789,38 @@ public abstract class FlatLaf
customDefaultsSources.remove( folder );
}
/**
* Gets global extra UI defaults; or {@code null}.
*
* @since 1.6
*/
public static Map<String, String> getGlobalExtraDefaults() {
return globalExtraDefaults;
}
/**
* Sets global extra UI defaults, which are only used when setting up the application look and feel.
* E.g. using {@link UIManager#setLookAndFeel(LookAndFeel)} or {@link #setup(LookAndFeel)}.
* <p>
* The global extra defaults are useful for smaller additional defaults that may change.
* E.g. accent color. Otherwise FlatLaf properties files should be used.
* See {@link #registerCustomDefaultsSource(String)}.
* <p>
* The keys and values are strings in same format as in FlatLaf properties files.
* <p>
* Sample that setups "FlatLaf Light" theme with red accent color:
* <pre>{@code
* FlatLaf.setGlobalExtraDefaults( Collections.singletonMap( "@accentColor", "#f00" ) );
* FlatLightLaf.setup();
* }</pre>
*
* @see #setExtraDefaults(Map)
* @since 1.6
*/
public static void setGlobalExtraDefaults( Map<String, String> globalExtraDefaults ) {
FlatLaf.globalExtraDefaults = globalExtraDefaults;
}
/**
* Gets extra UI defaults; or {@code null}.
*
@@ -811,6 +847,7 @@ public abstract class FlatLaf
* FlatLaf.setup( laf );
* }</pre>
*
* @see #setGlobalExtraDefaults(Map)
* @since 1.6
*/
public void setExtraDefaults( Map<String, String> extraDefaults ) {

View File

@@ -49,6 +49,9 @@ public class ColorFunctions
/**
* Returns a color that is a mixture of two colors.
* <p>
* This can be used to animate a color change from {@code color1} to {@code color2}
* by invoking this method multiple times with growing {@code weight} (from 0 to 1).
*
* @param color1 first color
* @param color2 second color
@@ -79,6 +82,34 @@ public class ColorFunctions
Math.round( a2 + ((a1 - a2) * weight) ) );
}
/**
* Mix color with white, which makes the color brighter.
* This is the same as {@link #mix}{@code (Color.white, color, weight)}.
*
* @param color second color
* @param weight the weight (in range 0-1) to mix the two colors.
* Larger weight uses more of first color, smaller weight more of second color.
* @return mixture of colors
* @since 1.6
*/
public static Color tint( Color color, float weight ) {
return mix( Color.white, color, weight );
}
/**
* Mix color with black, which makes the color darker.
* This is the same as {@link #mix}{@code (Color.black, color, weight)}.
*
* @param color second color
* @param weight the weight (in range 0-1) to mix the two colors.
* Larger weight uses more of first color, smaller weight more of second color.
* @return mixture of colors
* @since 1.6
*/
public static Color shade( Color color, float weight ) {
return mix( Color.black, color, weight );
}
/**
* Calculates the luma (perceptual brightness) of the given color.
* <p>

View File

@@ -41,7 +41,7 @@
Button.focusedBackground = null
Button.default.background = @accentButtonDefaultBackground
Button.default.foreground = #fff
Button.default.foreground = contrast($Button.default.background,lighten(@foreground,50%),#fff,50%)
Button.default.focusedBackground = null
Button.default.borderColor = shade($Button.default.background,15%)
Button.default.hoverBorderColor = tint($Button.default.background,50%)