mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-13 15:27:16 -06:00
FlatSVGIcon:
- renamed FlatSVGIcon.setFilter(...) to setColorFilter() - renamed ColorFilter.setFilter(Function) to setMapper(Function) - replaced ColorFilter.createGrayFilterFunction(int,int,int) with universal createRGBImageFilterFunction(RGBImageFilter) - ColorFilter: use default color palette mapping only in global filter
This commit is contained in:
@@ -20,7 +20,6 @@ import javax.swing.*;
|
|||||||
import com.formdev.flatlaf.extras.*;
|
import com.formdev.flatlaf.extras.*;
|
||||||
import com.formdev.flatlaf.extras.FlatSVGIcon.ColorFilter;
|
import com.formdev.flatlaf.extras.FlatSVGIcon.ColorFilter;
|
||||||
import com.formdev.flatlaf.extras.components.FlatTriStateCheckBox;
|
import com.formdev.flatlaf.extras.components.FlatTriStateCheckBox;
|
||||||
import com.formdev.flatlaf.util.GrayFilter;
|
|
||||||
import net.miginfocom.swing.*;
|
import net.miginfocom.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
@@ -159,9 +158,9 @@ public class ExtrasPanel
|
|||||||
// ---- toggleButton1 ----
|
// ---- toggleButton1 ----
|
||||||
toggleButton1.addActionListener( (e) -> {
|
toggleButton1.addActionListener( (e) -> {
|
||||||
if (toggleButton1.isSelected())
|
if (toggleButton1.isSelected())
|
||||||
FlatSVGIcon.ColorFilter.getInstance().setFilter( color -> color.brighter().brighter() );
|
FlatSVGIcon.ColorFilter.getInstance().setMapper( color -> color.brighter() );
|
||||||
else
|
else
|
||||||
FlatSVGIcon.ColorFilter.getInstance().setFilter( null );
|
FlatSVGIcon.ColorFilter.getInstance().setMapper( null );
|
||||||
SwingUtilities.getRootPane( toggleButton1 ).repaint();
|
SwingUtilities.getRootPane( toggleButton1 ).repaint();
|
||||||
} );
|
} );
|
||||||
|
|
||||||
@@ -170,7 +169,7 @@ public class ExtrasPanel
|
|||||||
|
|
||||||
private JLabel createRainbowIcon(String name) {
|
private JLabel createRainbowIcon(String name) {
|
||||||
FlatSVGIcon rainbowIcon = new FlatSVGIcon( "com/formdev/flatlaf/demo/extras/svg/" + name);
|
FlatSVGIcon rainbowIcon = new FlatSVGIcon( "com/formdev/flatlaf/demo/extras/svg/" + name);
|
||||||
rainbowIcon.setFilter( new ColorFilter( (color) -> {
|
rainbowIcon.setColorFilter( new ColorFilter( (color) -> {
|
||||||
counter+=1;
|
counter+=1;
|
||||||
counter%=255;
|
counter%=255;
|
||||||
return Color.getHSBColor(counter/255f, 1, 1);
|
return Color.getHSBColor(counter/255f, 1, 1);
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ public class FlatSVGIcon
|
|||||||
private final boolean disabled;
|
private final boolean disabled;
|
||||||
private final ClassLoader classLoader;
|
private final ClassLoader classLoader;
|
||||||
|
|
||||||
private ColorFilter userColorFilter = null;
|
private ColorFilter colorFilter;
|
||||||
|
|
||||||
private SVGDiagram diagram;
|
private SVGDiagram diagram;
|
||||||
private boolean dark;
|
private boolean dark;
|
||||||
@@ -163,37 +163,11 @@ public class FlatSVGIcon
|
|||||||
|
|
||||||
protected FlatSVGIcon( String name, int width, int height, float scale, boolean disabled, ClassLoader classLoader ) {
|
protected FlatSVGIcon( String name, int width, int height, float scale, boolean disabled, ClassLoader classLoader ) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.classLoader = classLoader;
|
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.scale = scale;
|
this.scale = scale;
|
||||||
this.disabled = disabled;
|
this.disabled = disabled;
|
||||||
}
|
this.classLoader = classLoader;
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets a color filter that can freely modify colors of this icon during painting.<br>
|
|
||||||
* <br>
|
|
||||||
* This method accepts a {@link ColorFilter}. Usually you would want to use a ColorFilter created using the
|
|
||||||
* {@link ColorFilter#ColorFilter(Function)} constructor.<br>
|
|
||||||
* <br>
|
|
||||||
* This can be used to brighten colors of the icon:
|
|
||||||
* <pre>icon.setFilter( new FlatSVGIcon.ColorFilter( color -> color.brighter() ) );</pre><br>
|
|
||||||
* <br>
|
|
||||||
* Using a filter, icons can also be turned monochrome (painted with a single color):
|
|
||||||
* <pre>icon.setFilter( new FlatSVGIcon.ColorFilter( color -> Color.RED ) );</pre><br>
|
|
||||||
* <br>
|
|
||||||
* Note: If a filter is already set, it will be replaced.
|
|
||||||
* @param filter The color filter
|
|
||||||
*/
|
|
||||||
public void setFilter(ColorFilter filter) {
|
|
||||||
this.userColorFilter = filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return The currently active {@link ColorFilter} or null.
|
|
||||||
*/
|
|
||||||
public ColorFilter getFilter() {
|
|
||||||
return userColorFilter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -245,6 +219,36 @@ public class FlatSVGIcon
|
|||||||
return icon;
|
return icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the currently active color filter or {@code null}.
|
||||||
|
*
|
||||||
|
* @since 1.2
|
||||||
|
*/
|
||||||
|
public ColorFilter getColorFilter() {
|
||||||
|
return colorFilter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a color filter that can freely modify colors of this icon during painting.
|
||||||
|
* <p>
|
||||||
|
* This method accepts a {@link ColorFilter}. Usually you would want to use a ColorFilter created using the
|
||||||
|
* {@link ColorFilter#ColorFilter(Function)} constructor.
|
||||||
|
* <p>
|
||||||
|
* This can be used to brighten colors of the icon:
|
||||||
|
* <pre>icon.setColorFilter( new FlatSVGIcon.ColorFilter( color -> color.brighter() ) );</pre>
|
||||||
|
* <p>
|
||||||
|
* Using a filter, icons can also be turned monochrome (painted with a single color):
|
||||||
|
* <pre>icon.setColorFilter( new FlatSVGIcon.ColorFilter( color -> Color.RED ) );</pre>
|
||||||
|
* <p>
|
||||||
|
* Note: If a filter is already set, it will be replaced.
|
||||||
|
*
|
||||||
|
* @param colorFilter The color filter
|
||||||
|
* @since 1.2
|
||||||
|
*/
|
||||||
|
public void setColorFilter( ColorFilter colorFilter ) {
|
||||||
|
this.colorFilter = colorFilter;
|
||||||
|
}
|
||||||
|
|
||||||
private void update() {
|
private void update() {
|
||||||
if( dark == isDarkLaf() && diagram != null )
|
if( dark == isDarkLaf() && diagram != null )
|
||||||
return;
|
return;
|
||||||
@@ -331,7 +335,7 @@ public class FlatSVGIcon
|
|||||||
: GrayFilter.createDisabledIconFilter( dark );
|
: GrayFilter.createDisabledIconFilter( dark );
|
||||||
}
|
}
|
||||||
|
|
||||||
Graphics2D g2 = new GraphicsFilter( (Graphics2D) g.create(), ColorFilter.getInstance(), this.userColorFilter, grayFilter );
|
Graphics2D g2 = new GraphicsFilter( (Graphics2D) g.create(), colorFilter, ColorFilter.getInstance(), grayFilter );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FlatUIUtils.setRenderingHints( g2 );
|
FlatUIUtils.setRenderingHints( g2 );
|
||||||
@@ -430,93 +434,91 @@ public class FlatSVGIcon
|
|||||||
//---- class ColorFilter --------------------------------------------------
|
//---- class ColorFilter --------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A color filter that can modify colors of a painted {@link FlatSVGIcon}.<br>
|
* A color filter that can modify colors of a painted {@link FlatSVGIcon}.
|
||||||
* <br>
|
* <p>
|
||||||
* The ColorFilter modifes color in two ways.<br>
|
* The ColorFilter modifies color in two ways.
|
||||||
* Either using a color map, where specific colors are mapped to different ones.<br>
|
* Either using a color map, where specific colors are mapped to different ones.
|
||||||
* And/or by modifying the colors directly, applying a modification to their rgba values.<br>
|
* And/or by modifying the colors in a mapper function.
|
||||||
* <br>
|
* <p>
|
||||||
* When filtering a color. Mappings are applied first, then an rgb color filter is applied.<br>
|
* When filtering a color, mappings are applied first, then the mapper function is applied.
|
||||||
* <br>
|
* <p>
|
||||||
* Global {@link FlatSVGIcon} ColorFilter can be retrieved using the {@link ColorFilter#getInstance()} method.
|
* Global {@link FlatSVGIcon} ColorFilter can be retrieved using the {@link ColorFilter#getInstance()} method.
|
||||||
*
|
|
||||||
* @see ColorFilter#ColorFilter(Function)
|
|
||||||
* @see ColorFilter#ColorFilter(boolean)
|
|
||||||
*/
|
*/
|
||||||
public static class ColorFilter
|
public static class ColorFilter
|
||||||
{
|
{
|
||||||
private static ColorFilter instance;
|
private static ColorFilter instance;
|
||||||
|
|
||||||
//Color maps
|
|
||||||
private final Map<Integer, String> rgb2keyMap = new HashMap<>();
|
private final Map<Integer, String> rgb2keyMap = new HashMap<>();
|
||||||
private final Map<Color, Color> color2colorMap = new HashMap<>();
|
private final Map<Color, Color> color2colorMap = new HashMap<>();
|
||||||
|
private Function<Color, Color> mapper;
|
||||||
//Color modification
|
|
||||||
private Function<Color, Color> colorFilter = null;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the global ColorFilter instance. If one doesn't exist, a new one is created with default color maps.
|
* Returns the global ColorFilter that is applied to all icons.
|
||||||
*/
|
*/
|
||||||
public static ColorFilter getInstance() {
|
public static ColorFilter getInstance() {
|
||||||
if( instance == null )
|
if( instance == null ) {
|
||||||
instance = new ColorFilter();
|
instance = new ColorFilter();
|
||||||
|
|
||||||
|
// add default color palette
|
||||||
|
for( FlatIconColors c : FlatIconColors.values() )
|
||||||
|
instance.rgb2keyMap.put( c.rgb, c.key );
|
||||||
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a color filter with default color mappings.
|
* Creates an empty color filter.
|
||||||
*/
|
*/
|
||||||
public ColorFilter() {
|
public ColorFilter() {
|
||||||
this(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a color filter. Default color maps can be optionally created.
|
* Creates a color filter with a color modifying function that changes painted colors.
|
||||||
* @param createDefaultColorMaps If true, default FlatLaf color maps are created.
|
|
||||||
*/
|
|
||||||
public ColorFilter(boolean createDefaultColorMaps) {
|
|
||||||
if (createDefaultColorMaps) {
|
|
||||||
for( FlatIconColors c : FlatIconColors.values() )
|
|
||||||
rgb2keyMap.put( c.rgb, c.key );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a color filter with a color modifying function that changes painted colors.<br>
|
|
||||||
* The {@link Function} gets passed the original color and returns a modified one.
|
* The {@link Function} gets passed the original color and returns a modified one.
|
||||||
* <br>
|
* <p>
|
||||||
* Examples:
|
* Examples:
|
||||||
* A ColorFilter can be used to brighten colors of the icon:
|
* A ColorFilter can be used to brighten colors of the icon:
|
||||||
* <pre>new ColorFilter( color -> color.brighter() );</pre>
|
* <pre>new ColorFilter( color -> color.brighter() );</pre>
|
||||||
* <br>
|
* <p>
|
||||||
* Using a ColorFilter, icons can also be turned monochrome (painted with a single color):
|
* Using a ColorFilter, icons can also be turned monochrome (painted with a single color):
|
||||||
* <pre>new ColorFilter( color -> Color.RED );</pre>
|
* <pre>new ColorFilter( color -> Color.RED );</pre>
|
||||||
* <br>
|
*
|
||||||
* @param filter The color filter function
|
* @param mapper The color mapper function
|
||||||
|
* @since 1.2
|
||||||
*/
|
*/
|
||||||
public ColorFilter(Function<Color, Color> filter) {
|
public ColorFilter( Function<Color, Color> mapper ) {
|
||||||
setFilter(filter);
|
setMapper( mapper );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a color modifying function that changes painted colors.<br>
|
* Returns a color modifying function or {@code null}
|
||||||
|
*
|
||||||
|
* @since 1.2
|
||||||
|
*/
|
||||||
|
public Function<Color, Color> getMapper() {
|
||||||
|
return mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a color modifying function that changes painted colors.
|
||||||
* The {@link Function} gets passed the original color and returns a modified one.
|
* The {@link Function} gets passed the original color and returns a modified one.
|
||||||
* <br>
|
* <p>
|
||||||
* Examples:
|
* Examples:
|
||||||
* A ColorFilter can be used to brighten colors of the icon:
|
* A ColorFilter can be used to brighten colors of the icon:
|
||||||
* <pre>filter.setFilter( color -> color.brighter() );</pre>
|
* <pre>filter.setMapper( color -> color.brighter() );</pre>
|
||||||
* <br>
|
* <p>
|
||||||
* Using a ColorFilter, icons can also be turned monochrome (painted with a single color):
|
* Using a ColorFilter, icons can also be turned monochrome (painted with a single color):
|
||||||
* <pre>filter.setFilter( color -> Color.RED );</pre>
|
* <pre>filter.setMapper( color -> Color.RED );</pre>
|
||||||
* <br>
|
*
|
||||||
* @param filter The color filter function
|
* @param mapper The color mapper function
|
||||||
|
* @since 1.2
|
||||||
*/
|
*/
|
||||||
public void setFilter(Function<Color, Color> filter) {
|
public void setMapper( Function<Color, Color> mapper ) {
|
||||||
this.colorFilter = filter;
|
this.mapper = mapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a color mappings.
|
* Adds color mappings.
|
||||||
*/
|
*/
|
||||||
public void addAll( Map<Color, Color> from2toMap ) {
|
public void addAll( Map<Color, Color> from2toMap ) {
|
||||||
color2colorMap.putAll( from2toMap );
|
color2colorMap.putAll( from2toMap );
|
||||||
@@ -537,23 +539,26 @@ public class FlatSVGIcon
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes all current color mappings.
|
* Removes all color mappings.
|
||||||
|
*
|
||||||
|
* @since 1.2
|
||||||
*/
|
*/
|
||||||
public void removeAll() {
|
public void removeAll() {
|
||||||
for ( Color from : color2colorMap.keySet()) {
|
color2colorMap.clear();
|
||||||
color2colorMap.remove( from );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Color filter( Color color ) {
|
public Color filter( Color color ) {
|
||||||
color = filterMappings( color );
|
// apply mappings
|
||||||
if (colorFilter != null) {
|
color = applyMappings( color );
|
||||||
color = colorFilter.apply( color );
|
|
||||||
}
|
// apply mapper function
|
||||||
|
if( mapper != null )
|
||||||
|
color = mapper.apply( color );
|
||||||
|
|
||||||
return color;
|
return color;
|
||||||
};
|
};
|
||||||
|
|
||||||
protected Color filterMappings( Color color ) {
|
private Color applyMappings( Color color ) {
|
||||||
Color newColor = color2colorMap.get( color );
|
Color newColor = color2colorMap.get( color );
|
||||||
if( newColor != null )
|
if( newColor != null )
|
||||||
return newColor;
|
return newColor;
|
||||||
@@ -572,16 +577,18 @@ public class FlatSVGIcon
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a color modifying function that changes color brightness, contrast and alpha.
|
* Creates a color modifying function that uses {@link RGBImageFilter#filterRGB(int, int, int)}.
|
||||||
* Can be set to a {@link ColorFilter} using {@link ColorFilter#setFilter(Function)}.
|
* Can be set to a {@link ColorFilter} using {@link ColorFilter#setMapper(Function)}.
|
||||||
*
|
*
|
||||||
* @param brightness in range [-100..100] where 0 has no effect
|
|
||||||
* @param contrast in range [-100..100] where 0 has no effect
|
|
||||||
* @param alpha in range [0..100] where 0 is transparent, 100 has no effect
|
|
||||||
* @see GrayFilter
|
* @see GrayFilter
|
||||||
|
* @since 1.2
|
||||||
*/
|
*/
|
||||||
public static Function<Color, Color> createGrayFilterFunction(int brightness, int contrast, int alpha) {
|
public static Function<Color, Color> createRGBImageFilterFunction( RGBImageFilter rgbImageFilter ) {
|
||||||
return color -> new Color(new GrayFilter(brightness, contrast, alpha).filterRGB( 0, 0, color.getRGB() ));
|
return color -> {
|
||||||
|
int oldRGB = color.getRGB();
|
||||||
|
int newRGB = rgbImageFilter.filterRGB( 0, 0, oldRGB );
|
||||||
|
return (newRGB != oldRGB) ? new Color( newRGB, true ) : color;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -591,15 +598,16 @@ public class FlatSVGIcon
|
|||||||
extends Graphics2DProxy
|
extends Graphics2DProxy
|
||||||
{
|
{
|
||||||
private final ColorFilter colorFilter;
|
private final ColorFilter colorFilter;
|
||||||
|
private final ColorFilter globalColorFilter;
|
||||||
private final RGBImageFilter grayFilter;
|
private final RGBImageFilter grayFilter;
|
||||||
private final ColorFilter userFilter;
|
|
||||||
|
|
||||||
public GraphicsFilter( Graphics2D delegate, ColorFilter globalColorFilter, ColorFilter userColorFilter,
|
GraphicsFilter( Graphics2D delegate, ColorFilter colorFilter,
|
||||||
RGBImageFilter grayFilter ) {
|
ColorFilter globalColorFilter, RGBImageFilter grayFilter )
|
||||||
|
{
|
||||||
super( delegate );
|
super( delegate );
|
||||||
this.colorFilter = globalColorFilter;
|
this.colorFilter = colorFilter;
|
||||||
|
this.globalColorFilter = globalColorFilter;
|
||||||
this.grayFilter = grayFilter;
|
this.grayFilter = grayFilter;
|
||||||
this.userFilter = userColorFilter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -615,10 +623,10 @@ public class FlatSVGIcon
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Color filterColor( Color color ) {
|
private Color filterColor( Color color ) {
|
||||||
if( userFilter != null )
|
|
||||||
color = userFilter.filter( color );
|
|
||||||
if( colorFilter != null )
|
if( colorFilter != null )
|
||||||
color = colorFilter.filter( color );
|
color = colorFilter.filter( color );
|
||||||
|
if( globalColorFilter != null )
|
||||||
|
color = globalColorFilter.filter( color );
|
||||||
if( grayFilter != null ) {
|
if( grayFilter != null ) {
|
||||||
int oldRGB = color.getRGB();
|
int oldRGB = color.getRGB();
|
||||||
int newRGB = grayFilter.filterRGB( 0, 0, oldRGB );
|
int newRGB = grayFilter.filterRGB( 0, 0, oldRGB );
|
||||||
|
|||||||
Reference in New Issue
Block a user