mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-13 23:37:13 -06:00
UIDefaultsLoader:
- support percentage in rgb() and rgba() functions - support rgba(color,alpha) to add alpha to any color
This commit is contained in:
@@ -515,8 +515,8 @@ class UIDefaultsLoader
|
|||||||
throw new IllegalArgumentException( "missing parameters in function '" + value + "'" );
|
throw new IllegalArgumentException( "missing parameters in function '" + value + "'" );
|
||||||
|
|
||||||
switch( function ) {
|
switch( function ) {
|
||||||
case "rgb": return parseColorRgbOrRgba( false, params );
|
case "rgb": return parseColorRgbOrRgba( false, params, resolver, reportError );
|
||||||
case "rgba": return parseColorRgbOrRgba( true, params );
|
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 parseColorLightenOrDarken( true, params, resolver, reportError );
|
||||||
@@ -527,17 +527,28 @@ class UIDefaultsLoader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Syntax: rgb(red,green,blue) or rgba(red,green,blue,alpha)
|
* Syntax: rgb(red,green,blue) or rgba(red,green,blue,alpha) or rgba(color,alpha)
|
||||||
* - red: an integer 0-255
|
* - red: an integer 0-255 or a percentage 0-100%
|
||||||
* - green: an integer 0-255
|
* - green: an integer 0-255 or a percentage 0-100%
|
||||||
* - blue: an integer 0-255
|
* - blue: an integer 0-255 or a percentage 0-100%
|
||||||
* - alpha: an integer 0-255
|
* - alpha: an integer 0-255 or a percentage 0-100%
|
||||||
*/
|
*/
|
||||||
private static ColorUIResource parseColorRgbOrRgba( boolean hasAlpha, List<String> params ) {
|
private static ColorUIResource parseColorRgbOrRgba( boolean hasAlpha, List<String> params,
|
||||||
int red = parseInteger( params.get( 0 ), 0, 255 );
|
Function<String, String> resolver, boolean reportError )
|
||||||
int green = parseInteger( params.get( 1 ), 0, 255 );
|
{
|
||||||
int blue = parseInteger( params.get( 2 ), 0, 255 );
|
if( hasAlpha && params.size() == 2 ) {
|
||||||
int alpha = hasAlpha ? parseInteger( params.get( 3 ), 0, 255 ) : 255;
|
// syntax rgba(color,alpha), which allows adding alpha to any color
|
||||||
|
String colorStr = params.get( 0 );
|
||||||
|
int alpha = parseInteger( params.get( 1 ), 0, 255, true );
|
||||||
|
|
||||||
|
ColorUIResource color = (ColorUIResource) parseColorOrFunction( resolver.apply( colorStr ), resolver, reportError );
|
||||||
|
return new ColorUIResource( new Color( ((alpha & 0xff) << 24) | (color.getRGB() & 0xffffff), true ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
int red = parseInteger( params.get( 0 ), 0, 255, true );
|
||||||
|
int green = parseInteger( params.get( 1 ), 0, 255, true );
|
||||||
|
int blue = parseInteger( params.get( 2 ), 0, 255, true );
|
||||||
|
int alpha = hasAlpha ? parseInteger( params.get( 3 ), 0, 255, true ) : 255;
|
||||||
|
|
||||||
return hasAlpha
|
return hasAlpha
|
||||||
? new ColorUIResource( new Color( red, green, blue, alpha ) )
|
? new ColorUIResource( new Color( red, green, blue, alpha ) )
|
||||||
@@ -552,7 +563,7 @@ class UIDefaultsLoader
|
|||||||
* - alpha: a percentage 0-100%
|
* - alpha: a percentage 0-100%
|
||||||
*/
|
*/
|
||||||
private static ColorUIResource parseColorHslOrHsla( boolean hasAlpha, List<String> params ) {
|
private static ColorUIResource parseColorHslOrHsla( boolean hasAlpha, List<String> params ) {
|
||||||
int hue = parseInteger( params.get( 0 ), 0, 360 );
|
int hue = parseInteger( params.get( 0 ), 0, 360, false );
|
||||||
int saturation = parsePercentage( params.get( 1 ) );
|
int saturation = parsePercentage( params.get( 1 ) );
|
||||||
int lightness = parsePercentage( params.get( 2 ) );
|
int lightness = parsePercentage( params.get( 2 ) );
|
||||||
int alpha = hasAlpha ? parsePercentage( params.get( 3 ) ) : 100;
|
int alpha = hasAlpha ? parsePercentage( params.get( 3 ) ) : 100;
|
||||||
@@ -629,7 +640,12 @@ class UIDefaultsLoader
|
|||||||
return value.charAt( 0 );
|
return value.charAt( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Integer parseInteger( String value, int min, int max ) {
|
private static Integer parseInteger( String value, int min, int max, boolean allowPercentage ) {
|
||||||
|
if( allowPercentage && value.endsWith( "%" ) ) {
|
||||||
|
int percent = parsePercentage( value );
|
||||||
|
return (max * percent) / 100;
|
||||||
|
}
|
||||||
|
|
||||||
Integer integer = parseInteger( value, true );
|
Integer integer = parseInteger( value, true );
|
||||||
if( integer.intValue() < min || integer.intValue() > max )
|
if( integer.intValue() < min || integer.intValue() > max )
|
||||||
throw new NumberFormatException( "integer '" + value + "' out of range (" + min + '-' + max + ')' );
|
throw new NumberFormatException( "integer '" + value + "' out of range (" + min + '-' + max + ')' );
|
||||||
|
|||||||
Reference in New Issue
Block a user