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 f7931fb7..035cc5ea 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java @@ -511,8 +511,10 @@ class UIDefaultsLoader case "rgba": return parseColorRgbOrRgba( true, params, resolver, reportError ); case "hsl": return parseColorHslOrHsla( false, params ); case "hsla": return parseColorHslOrHsla( true, params ); - case "lighten": return parseColorLightenOrDarken( true, params, resolver, reportError ); - case "darken": return parseColorLightenOrDarken( false, params, resolver, reportError ); + case "lighten": return parseColorHSLIncreaseDecrease( 2, true, params, resolver, reportError ); + case "darken": return parseColorHSLIncreaseDecrease( 2, false, params, resolver, reportError ); + case "saturate": return parseColorHSLIncreaseDecrease( 1, true, params, resolver, reportError ); + case "desaturate": return parseColorHSLIncreaseDecrease( 1, false, params, resolver, reportError ); } throw new IllegalArgumentException( "unknown color function '" + value + "'" ); @@ -565,13 +567,14 @@ class UIDefaultsLoader } /** - * Syntax: lighten(color,amount[,options]) or darken(color,amount[,options]) + * Syntax: lighten(color,amount[,options]) or darken(color,amount[,options]) or + * saturate(color,amount[,options]) or desaturate(color,amount[,options]) * - color: a color (e.g. #f00) or a color function * - amount: percentage 0-100% * - options: [relative] [autoInverse] [lazy] [derived] */ - private static Object parseColorLightenOrDarken( boolean lighten, List params, - Function resolver, boolean reportError ) + private static Object parseColorHSLIncreaseDecrease( int hslIndex, boolean increase, + List params, Function resolver, boolean reportError ) { String colorStr = params.get( 0 ); int amount = parsePercentage( params.get( 1 ) ); @@ -588,9 +591,8 @@ class UIDefaultsLoader derived = options.contains( "derived" ); } - ColorFunctions.ColorFunction function = lighten - ? new ColorFunctions.Lighten( amount, relative, autoInverse ) - : new ColorFunctions.Darken( amount, relative, autoInverse ); + ColorFunctions.ColorFunction function = new ColorFunctions.HSLIncreaseDecrease( + hslIndex, increase, amount, relative, autoInverse ); if( derived ) { ColorUIResource color = (ColorUIResource) parseColorOrFunction( resolver.apply( colorStr ), resolver, reportError ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/ColorFunctions.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/ColorFunctions.java index 00f60561..9f0592a7 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/ColorFunctions.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/ColorFunctions.java @@ -49,20 +49,26 @@ public class ColorFunctions void apply( float[] hsl ); } - //---- class Lighten ------------------------------------------------------ + //---- class HSLIncreaseDecrease ------------------------------------------ /** - * Increase the lightness of a color in the HSL color space by an absolute - * or relative amount. + * Increase or decrease hue, saturation or luminance of a color in the HSL color space + * by an absolute or relative amount. */ - public static class Lighten + public static class HSLIncreaseDecrease implements ColorFunction { - private final float amount; - private final boolean relative; - private final boolean autoInverse; + public final int hslIndex; + public final boolean increase; + public final float amount; + public final boolean relative; + public final boolean autoInverse; - public Lighten( float amount, boolean relative, boolean autoInverse ) { + public HSLIncreaseDecrease( int hslIndex, boolean increase, + float amount, boolean relative, boolean autoInverse ) + { + this.hslIndex = hslIndex; + this.increase = increase; this.amount = amount; this.relative = relative; this.autoInverse = autoInverse; @@ -70,33 +76,17 @@ public class ColorFunctions @Override public void apply( float[] hsl ) { - float amount2 = autoInverse && shouldInverse( hsl ) ? -amount : amount; - hsl[2] = clamp( relative - ? (hsl[2] * ((100 + amount2) / 100)) - : (hsl[2] + amount2) ); + float amount2 = increase ? amount : -amount; + amount2 = autoInverse && shouldInverse( hsl ) ? -amount2 : amount2; + hsl[hslIndex] = clamp( relative + ? (hsl[hslIndex] * ((100 + amount2) / 100)) + : (hsl[hslIndex] + amount2) ); } protected boolean shouldInverse( float[] hsl ) { - return hsl[2] >= 50; - } - } - - //---- class Darken ------------------------------------------------------- - - /** - * Decrease the lightness of a color in the HSL color space by an absolute - * or relative amount. - */ - public static class Darken - extends Lighten - { - public Darken( float amount, boolean relative, boolean autoInverse ) { - super( -amount, relative, autoInverse ); - } - - @Override - protected boolean shouldInverse( float[] hsl ) { - return hsl[2] < 50; + return increase + ? hsl[hslIndex] >= 50 + : hsl[hslIndex] < 50; } } }