mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-09 08:15:09 +03:00
UIDefaultsLoader: added changeHue(), changeSaturation(), changeLightness() and changeAlpha() color functions (inspired by Sass CSS color.change() function)
This commit is contained in:
@@ -604,6 +604,10 @@ class UIDefaultsLoader
|
||||
case "fadeout": return parseColorHSLIncreaseDecrease( 3, false, params, resolver, reportError );
|
||||
case "fade": return parseColorFade( params, resolver, reportError );
|
||||
case "spin": return parseColorSpin( params, resolver, reportError );
|
||||
case "changeHue": return parseColorChange( 0, params, resolver, reportError );
|
||||
case "changeSaturation":return parseColorChange( 1, params, resolver, reportError );
|
||||
case "changeLightness": return parseColorChange( 2, params, resolver, reportError );
|
||||
case "changeAlpha": return parseColorChange( 3, params, resolver, reportError );
|
||||
}
|
||||
} finally {
|
||||
parseColorDepth--;
|
||||
@@ -753,6 +757,36 @@ class UIDefaultsLoader
|
||||
return parseFunctionBaseColor( colorStr, function, derived, resolver, reportError );
|
||||
}
|
||||
|
||||
/**
|
||||
* Syntax: changeHue(color,value[,options]) or
|
||||
* changeSaturation(color,value[,options]) or
|
||||
* changeLightness(color,value[,options]) or
|
||||
* changeAlpha(color,value[,options])
|
||||
* - color: a color (e.g. #f00) or a color function
|
||||
* - value: for hue: number of degrees; otherwise: percentage 0-100%
|
||||
* - options: [derived]
|
||||
*/
|
||||
private static Object parseColorChange( int hslIndex,
|
||||
List<String> params, Function<String, String> resolver, boolean reportError )
|
||||
{
|
||||
String colorStr = params.get( 0 );
|
||||
int value = (hslIndex == 0)
|
||||
? parseInteger( params.get( 1 ), true )
|
||||
: parsePercentage( params.get( 1 ) );
|
||||
boolean derived = false;
|
||||
|
||||
if( params.size() > 2 ) {
|
||||
String options = params.get( 2 );
|
||||
derived = options.contains( "derived" );
|
||||
}
|
||||
|
||||
// create function
|
||||
ColorFunction function = new ColorFunctions.HSLChange( hslIndex, value );
|
||||
|
||||
// parse base color, apply function and create derived color
|
||||
return parseFunctionBaseColor( colorStr, function, derived, resolver, reportError );
|
||||
}
|
||||
|
||||
private static Object parseFunctionBaseColor( String colorStr, ColorFunction function,
|
||||
boolean derived, Function<String, String> resolver, boolean reportError )
|
||||
{
|
||||
|
||||
@@ -145,7 +145,46 @@ public class ColorFunctions
|
||||
}
|
||||
}
|
||||
|
||||
//---- class HSLIncreaseDecrease ------------------------------------------
|
||||
//---- class HSLChange ----------------------------------------------------
|
||||
|
||||
/**
|
||||
* Set the hue, saturation, luminance or alpha of a color.
|
||||
*
|
||||
* @since 1.6
|
||||
*/
|
||||
public static class HSLChange
|
||||
implements ColorFunction
|
||||
{
|
||||
public final int hslIndex;
|
||||
public final float value;
|
||||
|
||||
public HSLChange( int hslIndex, float value ) {
|
||||
this.hslIndex = hslIndex;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply( float[] hsla ) {
|
||||
hsla[hslIndex] = (hslIndex == 0)
|
||||
? value % 360
|
||||
: clamp( value );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String name;
|
||||
switch( hslIndex ) {
|
||||
case 0: name = "changeHue"; break;
|
||||
case 1: name = "changeSaturation"; break;
|
||||
case 2: name = "changeLightness"; break;
|
||||
case 3: name = "changeAlpha"; break;
|
||||
default: throw new IllegalArgumentException();
|
||||
}
|
||||
return String.format( "%s(%.0f%s)", name, value, (hslIndex == 0 ? "" : "%") );
|
||||
}
|
||||
}
|
||||
|
||||
//---- class Fade ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Set the alpha of a color.
|
||||
|
||||
@@ -355,8 +355,21 @@ class FlatCompletionProvider
|
||||
"options", "(optional) [derived]" );
|
||||
addFunction( "spin",
|
||||
"color", colorParamDesc,
|
||||
"angle", "number of degrees to rotate",
|
||||
"angle", "number of degrees to rotate (0-360)",
|
||||
"options", "(optional) [derived]" );
|
||||
|
||||
addFunction( "changeHue",
|
||||
"color", colorParamDesc,
|
||||
"angle", "number of degrees (0-360)",
|
||||
"options", "(optional) [derived]" );
|
||||
String[] hslChangeParams = {
|
||||
"color", colorParamDesc,
|
||||
"value", "0-100%",
|
||||
"options", "(optional) [derived]"
|
||||
};
|
||||
addFunction( "changeSaturation", hslChangeParams );
|
||||
addFunction( "changeLightness", hslChangeParams );
|
||||
addFunction( "changeAlpha", hslChangeParams );
|
||||
}
|
||||
|
||||
private void addFunction( String name, String... paramNamesAndDescs ) {
|
||||
|
||||
@@ -60,6 +60,14 @@ public class FlatThemeTokenMaker
|
||||
tokenMap.put( "darken", TOKEN_FUNCTION );
|
||||
tokenMap.put( "saturate", TOKEN_FUNCTION );
|
||||
tokenMap.put( "desaturate", TOKEN_FUNCTION );
|
||||
tokenMap.put( "fadein", TOKEN_FUNCTION );
|
||||
tokenMap.put( "fadeout", TOKEN_FUNCTION );
|
||||
tokenMap.put( "fade", TOKEN_FUNCTION );
|
||||
tokenMap.put( "spin", TOKEN_FUNCTION );
|
||||
tokenMap.put( "changeHue", TOKEN_FUNCTION );
|
||||
tokenMap.put( "changeSaturation", TOKEN_FUNCTION );
|
||||
tokenMap.put( "changeLightness", TOKEN_FUNCTION );
|
||||
tokenMap.put( "changeAlpha", TOKEN_FUNCTION );
|
||||
tokenMap.put( "lazy", TOKEN_FUNCTION );
|
||||
|
||||
// function options
|
||||
|
||||
@@ -49,3 +49,8 @@ Prop.colorFunc13 = spin($Prop.colorFunc12,40)
|
||||
Prop.colorFunc14 = spin($Prop.colorFunc12,-40)
|
||||
Prop.colorFunc15 = spin($Prop.colorFunc12,400)
|
||||
Prop.colorFunc16 = spin($Prop.colorFunc12,-400)
|
||||
|
||||
Prop.colorFunc20 = changeHue(#f00,180)
|
||||
Prop.colorFunc21 = changeSaturation(#f00,50%)
|
||||
Prop.colorFunc22 = changeLightness(#f00,80%)
|
||||
Prop.colorFunc23 = changeAlpha(#f00,50%)
|
||||
|
||||
Reference in New Issue
Block a user