UIDefaultsLoader: added saturate() and desaturate() color functions

This commit is contained in:
Karl Tauber
2020-05-16 18:59:05 +02:00
parent 069a4e8f0b
commit 19426394e2
2 changed files with 32 additions and 40 deletions

View File

@@ -511,8 +511,10 @@ class UIDefaultsLoader
case "rgba": return parseColorRgbOrRgba( true, params, resolver, reportError ); case "rgba": return parseColorRgbOrRgba( true, params, resolver, reportError );
case "hsl": return parseColorHslOrHsla( false, params ); case "hsl": return parseColorHslOrHsla( false, params );
case "hsla": return parseColorHslOrHsla( true, params ); case "hsla": return parseColorHslOrHsla( true, params );
case "lighten": return parseColorLightenOrDarken( true, params, resolver, reportError ); case "lighten": return parseColorHSLIncreaseDecrease( 2, true, params, resolver, reportError );
case "darken": return parseColorLightenOrDarken( false, 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 + "'" ); 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 * - color: a color (e.g. #f00) or a color function
* - amount: percentage 0-100% * - amount: percentage 0-100%
* - options: [relative] [autoInverse] [lazy] [derived] * - options: [relative] [autoInverse] [lazy] [derived]
*/ */
private static Object parseColorLightenOrDarken( boolean lighten, List<String> params, private static Object parseColorHSLIncreaseDecrease( int hslIndex, boolean increase,
Function<String, String> resolver, boolean reportError ) List<String> params, Function<String, String> resolver, boolean reportError )
{ {
String colorStr = params.get( 0 ); String colorStr = params.get( 0 );
int amount = parsePercentage( params.get( 1 ) ); int amount = parsePercentage( params.get( 1 ) );
@@ -588,9 +591,8 @@ class UIDefaultsLoader
derived = options.contains( "derived" ); derived = options.contains( "derived" );
} }
ColorFunctions.ColorFunction function = lighten ColorFunctions.ColorFunction function = new ColorFunctions.HSLIncreaseDecrease(
? new ColorFunctions.Lighten( amount, relative, autoInverse ) hslIndex, increase, amount, relative, autoInverse );
: new ColorFunctions.Darken( amount, relative, autoInverse );
if( derived ) { if( derived ) {
ColorUIResource color = (ColorUIResource) parseColorOrFunction( resolver.apply( colorStr ), resolver, reportError ); ColorUIResource color = (ColorUIResource) parseColorOrFunction( resolver.apply( colorStr ), resolver, reportError );

View File

@@ -49,20 +49,26 @@ public class ColorFunctions
void apply( float[] hsl ); void apply( float[] hsl );
} }
//---- class Lighten ------------------------------------------------------ //---- class HSLIncreaseDecrease ------------------------------------------
/** /**
* Increase the lightness of a color in the HSL color space by an absolute * Increase or decrease hue, saturation or luminance of a color in the HSL color space
* or relative amount. * by an absolute or relative amount.
*/ */
public static class Lighten public static class HSLIncreaseDecrease
implements ColorFunction implements ColorFunction
{ {
private final float amount; public final int hslIndex;
private final boolean relative; public final boolean increase;
private final boolean autoInverse; 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.amount = amount;
this.relative = relative; this.relative = relative;
this.autoInverse = autoInverse; this.autoInverse = autoInverse;
@@ -70,33 +76,17 @@ public class ColorFunctions
@Override @Override
public void apply( float[] hsl ) { public void apply( float[] hsl ) {
float amount2 = autoInverse && shouldInverse( hsl ) ? -amount : amount; float amount2 = increase ? amount : -amount;
hsl[2] = clamp( relative amount2 = autoInverse && shouldInverse( hsl ) ? -amount2 : amount2;
? (hsl[2] * ((100 + amount2) / 100)) hsl[hslIndex] = clamp( relative
: (hsl[2] + amount2) ); ? (hsl[hslIndex] * ((100 + amount2) / 100))
: (hsl[hslIndex] + amount2) );
} }
protected boolean shouldInverse( float[] hsl ) { protected boolean shouldInverse( float[] hsl ) {
return hsl[2] >= 50; return increase
} ? hsl[hslIndex] >= 50
} : hsl[hslIndex] < 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;
} }
} }
} }