mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-07 22:40:53 +03:00
FlatSVGIcon: color filters now can access painting component to implement component state based color mappings (PR #906)
This commit is contained in:
@@ -1,6 +1,14 @@
|
|||||||
FlatLaf Change Log
|
FlatLaf Change Log
|
||||||
==================
|
==================
|
||||||
|
|
||||||
|
## 3.6-SNAPSHOT
|
||||||
|
|
||||||
|
#### New features and improvements
|
||||||
|
|
||||||
|
- Extras: `FlatSVGIcon` color filters now can access painting component to
|
||||||
|
implement component state based color mappings. (PR #906)
|
||||||
|
|
||||||
|
|
||||||
## 3.5.3
|
## 3.5.3
|
||||||
|
|
||||||
#### Fixed bugs
|
#### Fixed bugs
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import java.net.URL;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import javax.swing.Icon;
|
import javax.swing.Icon;
|
||||||
import javax.swing.ImageIcon;
|
import javax.swing.ImageIcon;
|
||||||
@@ -588,7 +589,12 @@ public class FlatSVGIcon
|
|||||||
: GrayFilter.createDisabledIconFilter( dark );
|
: GrayFilter.createDisabledIconFilter( dark );
|
||||||
}
|
}
|
||||||
|
|
||||||
Graphics2D g2 = new GraphicsFilter( (Graphics2D) g.create(), colorFilter, ColorFilter.getInstance(), grayFilter );
|
ColorFilter globalColorFilter = ColorFilter.getInstance();
|
||||||
|
globalColorFilter.c = c;
|
||||||
|
if( colorFilter != null )
|
||||||
|
colorFilter.c = c;
|
||||||
|
|
||||||
|
Graphics2D g2 = new GraphicsFilter( (Graphics2D) g.create(), colorFilter, globalColorFilter, grayFilter );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setRenderingHints( g2 );
|
setRenderingHints( g2 );
|
||||||
@@ -596,6 +602,10 @@ public class FlatSVGIcon
|
|||||||
paintSvg( g2, x, y );
|
paintSvg( g2, x, y );
|
||||||
} finally {
|
} finally {
|
||||||
g2.dispose();
|
g2.dispose();
|
||||||
|
|
||||||
|
globalColorFilter.c = null;
|
||||||
|
if( colorFilter != null )
|
||||||
|
colorFilter.c = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -760,6 +770,8 @@ public class FlatSVGIcon
|
|||||||
private Map<Color, Color> colorMap;
|
private Map<Color, Color> colorMap;
|
||||||
private Map<Color, Color> darkColorMap;
|
private Map<Color, Color> darkColorMap;
|
||||||
private Function<Color, Color> mapper;
|
private Function<Color, Color> mapper;
|
||||||
|
private BiFunction<Component, Color, Color> mapperEx;
|
||||||
|
private Component c;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the global ColorFilter that is applied to all icons.
|
* Returns the global ColorFilter that is applied to all icons.
|
||||||
@@ -800,6 +812,22 @@ public class FlatSVGIcon
|
|||||||
setMapper( mapper );
|
setMapper( mapper );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a color modifying function that changes painted colors.
|
||||||
|
* The {@link BiFunction} gets passed the component and
|
||||||
|
* the original color and returns a modified one.
|
||||||
|
* <p>
|
||||||
|
* Examples:
|
||||||
|
* A ColorFilter can be used to brighten colors of the icon (depending on component state if desired):
|
||||||
|
* <pre>new ColorFilter( (c, color) -> c.isEnabled() ? color.brighter() : color );</pre>
|
||||||
|
*
|
||||||
|
* @param mapperEx The color mapper function
|
||||||
|
* @since 3.6
|
||||||
|
*/
|
||||||
|
public ColorFilter( BiFunction<Component, Color, Color> mapperEx ) {
|
||||||
|
setMapperEx( mapperEx );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a color modifying function or {@code null}
|
* Returns a color modifying function or {@code null}
|
||||||
*
|
*
|
||||||
@@ -821,12 +849,39 @@ public class FlatSVGIcon
|
|||||||
* <pre>filter.setMapper( color -> Color.RED );</pre>
|
* <pre>filter.setMapper( color -> Color.RED );</pre>
|
||||||
*
|
*
|
||||||
* @param mapper The color mapper function
|
* @param mapper The color mapper function
|
||||||
|
* @see #setMapperEx(BiFunction)
|
||||||
* @since 1.2
|
* @since 1.2
|
||||||
*/
|
*/
|
||||||
public void setMapper( Function<Color, Color> mapper ) {
|
public void setMapper( Function<Color, Color> mapper ) {
|
||||||
this.mapper = mapper;
|
this.mapper = mapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a color modifying function or {@code null}
|
||||||
|
*
|
||||||
|
* @since 3.6
|
||||||
|
*/
|
||||||
|
public BiFunction<Component, Color, Color> getMapperEx() {
|
||||||
|
return mapperEx;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a color modifying function that changes painted colors.
|
||||||
|
* The {@link BiFunction} gets passed the component and
|
||||||
|
* the original color and returns a modified one.
|
||||||
|
* <p>
|
||||||
|
* Examples:
|
||||||
|
* A ColorFilter can be used to brighten colors of the icon (depending on component state if desired):
|
||||||
|
* <pre>filter.setMapperEx( (c, color) -> c.isEnabled() ? color.brighter() : color );</pre>
|
||||||
|
*
|
||||||
|
* @param mapperEx The color mapper function
|
||||||
|
* @see #setMapper(Function)
|
||||||
|
* @since 3.6
|
||||||
|
*/
|
||||||
|
public void setMapperEx( BiFunction<Component, Color, Color> mapperEx ) {
|
||||||
|
this.mapperEx = mapperEx;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the color mappings used for light themes.
|
* Returns the color mappings used for light themes.
|
||||||
*
|
*
|
||||||
@@ -936,6 +991,11 @@ public class FlatSVGIcon
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Color filter( Color color ) {
|
public Color filter( Color color ) {
|
||||||
|
return filter( c, color );
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @since 3.6 */
|
||||||
|
public Color filter( Component c, Color color ) {
|
||||||
// apply mappings
|
// apply mappings
|
||||||
color = applyMappings( color );
|
color = applyMappings( color );
|
||||||
|
|
||||||
@@ -943,6 +1003,10 @@ public class FlatSVGIcon
|
|||||||
if( mapper != null )
|
if( mapper != null )
|
||||||
color = mapper.apply( color );
|
color = mapper.apply( color );
|
||||||
|
|
||||||
|
// apply mapperEx function
|
||||||
|
if( mapperEx != null )
|
||||||
|
color = mapperEx.apply( c, color );
|
||||||
|
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -974,6 +1038,16 @@ public class FlatSVGIcon
|
|||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the component passed to {@link FlatSVGIcon#paintIcon(Component, Graphics, int, int)}.
|
||||||
|
* This allows color mapping depend on component state (e.g. enabled, selected, hover, etc).
|
||||||
|
*
|
||||||
|
* @since 3.6
|
||||||
|
*/
|
||||||
|
public Component getPaintingComponent() {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a color modifying function that uses {@link RGBImageFilter#filterRGB(int, int, int)}.
|
* Creates a color modifying function that uses {@link RGBImageFilter#filterRGB(int, int, int)}.
|
||||||
* Can be set to a {@link ColorFilter} using {@link ColorFilter#setMapper(Function)}.
|
* Can be set to a {@link ColorFilter} using {@link ColorFilter#setMapper(Function)}.
|
||||||
|
|||||||
Reference in New Issue
Block a user