FlatSVGIcon: Removed unnecessary getInstance method. Changed the demo a little and added a utility method to ColorFilter to easily create a brightness/contrast/alpha filter.

This commit is contained in:
DUDSS
2021-04-09 13:26:10 +02:00
parent ba0f43455b
commit d3bf4433b7
2 changed files with 25 additions and 18 deletions

View File

@@ -20,6 +20,7 @@ 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.*;
@@ -152,13 +153,13 @@ public class ExtrasPanel
add(label7, "cell 1 7"); add(label7, "cell 1 7");
// ---- button1 ---- // ---- button1 ----
toggleButton1.setText( "Toggle red" ); toggleButton1.setText( "Toggle brighter" );
add(toggleButton1, "cell 1 7"); add(toggleButton1, "cell 1 7");
// ---- toggleButton1 ---- // ---- toggleButton1 ----
toggleButton1.addActionListener( (e) -> { toggleButton1.addActionListener( (e) -> {
if (toggleButton1.isSelected()) if (toggleButton1.isSelected())
FlatSVGIcon.ColorFilter.getInstance().setFilter( color -> Color.RED ); FlatSVGIcon.ColorFilter.getInstance().setFilter( color -> color.brighter().brighter() );
else else
FlatSVGIcon.ColorFilter.getInstance().setFilter( null ); FlatSVGIcon.ColorFilter.getInstance().setFilter( null );
SwingUtilities.getRootPane( toggleButton1 ).repaint(); SwingUtilities.getRootPane( toggleButton1 ).repaint();

View File

@@ -331,7 +331,7 @@ public class FlatSVGIcon
: GrayFilter.createDisabledIconFilter( dark ); : GrayFilter.createDisabledIconFilter( dark );
} }
Graphics2D g2 = new GraphicsFilter( (Graphics2D) g.create(), ColorFilter.getInstance(), grayFilter, this.userColorFilter ); Graphics2D g2 = new GraphicsFilter( (Graphics2D) g.create(), ColorFilter.getInstance(), this.userColorFilter, grayFilter );
try { try {
FlatUIUtils.setRenderingHints( g2 ); FlatUIUtils.setRenderingHints( g2 );
@@ -438,7 +438,7 @@ public class FlatSVGIcon
* <br> * <br>
* 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 an rgb color filter is applied.<br>
* <br> * <br>
* Global {@link FlatSVGIcon} ColorFilter can be retrieved using the {@link ColorFilter#getInstance()} methods. * Global {@link FlatSVGIcon} ColorFilter can be retrieved using the {@link ColorFilter#getInstance()} method.
* *
* @see ColorFilter#ColorFilter(Function) * @see ColorFilter#ColorFilter(Function)
* @see ColorFilter#ColorFilter(boolean) * @see ColorFilter#ColorFilter(boolean)
@@ -455,17 +455,9 @@ public class FlatSVGIcon
private Function<Color, Color> colorFilter = null; private Function<Color, Color> colorFilter = null;
/** /**
* Returns the global ColorFilter instance. If one doesn't exist, a new one is created. * Returns the global ColorFilter instance. If one doesn't exist, a new one is created with default color maps.
*/ */
public static ColorFilter getInstance() { public static ColorFilter getInstance() {
return(getInstance(true));
}
/**
* Returns the global ColorFilter instance. If one doesn't exist, a new one is created.
* @param createDefaultColorMaps If true, default FlatLaf color maps are created.
*/
public static ColorFilter getInstance(boolean createDefaultColorMaps) {
if( instance == null ) if( instance == null )
instance = new ColorFilter(); instance = new ColorFilter();
return instance; return instance;
@@ -490,7 +482,7 @@ public class FlatSVGIcon
} }
/** /**
* Creates a color modifying function that changes painted colors.<br> * 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> * <br>
* Examples: * Examples:
@@ -545,7 +537,7 @@ public class FlatSVGIcon
} }
/** /**
* Removes all color mappings. * Removes all current color mappings.
*/ */
public void removeAll() { public void removeAll() {
for ( Color from : color2colorMap.keySet()) { for ( Color from : color2colorMap.keySet()) {
@@ -578,6 +570,19 @@ public class FlatSVGIcon
? new Color( (newColor.getRGB() & 0x00ffffff) | (color.getRGB() & 0xff000000) ) ? new Color( (newColor.getRGB() & 0x00ffffff) | (color.getRGB() & 0xff000000) )
: newColor; : newColor;
} }
/**
* Creates a color modifying function that changes color brightness, contrast and alpha.
* Can be set to a {@link ColorFilter} using {@link ColorFilter#setFilter(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
*/
public static Function<Color, Color> createGrayFilterFunction(int brightness, int contrast, int alpha) {
return color -> new Color(new GrayFilter().filterRGB( 0, 0, color.getRGB() ));
}
} }
//---- class GraphicsFilter ----------------------------------------------- //---- class GraphicsFilter -----------------------------------------------
@@ -589,11 +594,12 @@ public class FlatSVGIcon
private final RGBImageFilter grayFilter; private final RGBImageFilter grayFilter;
private final ColorFilter userFilter; private final ColorFilter userFilter;
public GraphicsFilter( Graphics2D delegate, ColorFilter colorFilter, RGBImageFilter grayFilter,ColorFilter userFilter) { public GraphicsFilter( Graphics2D delegate, ColorFilter globalColorFilter, ColorFilter userColorFilter,
RGBImageFilter grayFilter ) {
super( delegate ); super( delegate );
this.colorFilter = colorFilter; this.colorFilter = globalColorFilter;
this.grayFilter = grayFilter; this.grayFilter = grayFilter;
this.userFilter = userFilter; this.userFilter = userColorFilter;
} }
@Override @Override