diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/extras/ExtrasPanel.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/extras/ExtrasPanel.java
index 37cfec1f..9614920a 100644
--- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/extras/ExtrasPanel.java
+++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/extras/ExtrasPanel.java
@@ -18,8 +18,10 @@ package com.formdev.flatlaf.demo.extras;
import javax.swing.*;
import com.formdev.flatlaf.extras.*;
+import com.formdev.flatlaf.extras.FlatSVGIcon.ColorFilter;
import com.formdev.flatlaf.extras.components.FlatTriStateCheckBox;
import net.miginfocom.swing.*;
+import java.awt.*;
/**
* @author Karl Tauber
@@ -27,6 +29,8 @@ import net.miginfocom.swing.*;
public class ExtrasPanel
extends JPanel
{
+ public int counter = 0;
+
public ExtrasPanel() {
initComponents();
@@ -69,6 +73,12 @@ public class ExtrasPanel
label2 = new JLabel();
svgIconsPanel = new JPanel();
label3 = new JLabel();
+ separator1 = new JSeparator();
+ label5 = new JLabel();
+ label6 = new JLabel();
+ label7 = new JLabel();
+ rainbowIcon = new JLabel();
+ toggleButton1 = new JToggleButton();
//======== this ========
setLayout(new MigLayout(
@@ -81,6 +91,9 @@ public class ExtrasPanel
"[]para" +
"[]" +
"[]" +
+ "[]" +
+ "[]" +
+ "[]" +
"[]"));
//---- label4 ----
@@ -119,9 +132,55 @@ public class ExtrasPanel
//---- label3 ----
label3.setText("The icons may change colors when switching to another theme.");
add(label3, "cell 1 3 2 1");
+
+ add(separator1, "cell 1 4, grow");
+
+ //---- label5 ----
+ label5.setText("Color filters can be also applied to icons. Globally or for each instance.");
+ add(label5, "cell 1 5");
+
+ //---- label6 ----
+ label6.setText( "Rainbow color filter" );
+ add(label6, "cell 1 6");
+
+ //---- rainbowIcon ----
+ rainbowIcon = createRainbowIcon("informationDialog.svg");
+ add(rainbowIcon, "cell 1 6");
+
+ //---- label7 ----
+ label7.setText( "Global icon color filter" );
+ add(label7, "cell 1 7");
+
+ // ---- button1 ----
+ toggleButton1.setText( "Toggle red" );
+ add(toggleButton1, "cell 1 7");
+
+ // ---- toggleButton1 ----
+ toggleButton1.addActionListener( (e) -> {
+ if (toggleButton1.isSelected())
+ FlatSVGIcon.ColorFilter.getInstance().setFilter( color -> Color.RED );
+ else
+ FlatSVGIcon.ColorFilter.getInstance().setFilter( null );
+ SwingUtilities.getRootPane( toggleButton1 ).repaint();
+ } );
+
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
+ private JLabel createRainbowIcon(String name) {
+ FlatSVGIcon rainbowIcon = new FlatSVGIcon( "com/formdev/flatlaf/demo/extras/svg/" + name);
+ rainbowIcon.setFilter( new ColorFilter( (color) -> {
+ counter+=1;
+ counter%=255;
+ return Color.getHSBColor(counter/255f, 1, 1);
+ }) );
+ JLabel label = new JLabel(rainbowIcon);
+ new Timer(30, (e) -> {
+ label.repaint();
+ }).start();
+ return label;
+ }
+
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JLabel label4;
private JLabel label1;
@@ -130,5 +189,11 @@ public class ExtrasPanel
private JLabel label2;
private JPanel svgIconsPanel;
private JLabel label3;
+ private JLabel label5;
+ private JLabel label6;
+ private JLabel label7;
+ private JSeparator separator1;
+ private JLabel rainbowIcon;
+ private JToggleButton toggleButton1;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
diff --git a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatRGBFilter.java b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatRGBFilter.java
deleted file mode 100644
index 83c1917c..00000000
--- a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatRGBFilter.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package com.formdev.flatlaf.extras;
-
-import java.awt.*;
-import java.awt.image.RGBImageFilter;
-
-/**
- * A simplified RGBImageFilter that presents individual rgba components as a Color object.
- * Can be used to modify the color of a {@link FlatSVGIcon}-
- */
-public abstract class FlatRGBFilter extends RGBImageFilter
-{
- @Override
- public int filterRGB(int x, int y, int rgb) {
- return filterRGB(new Color(rgb)).getRGB();
- }
-
- /**
- * @param c Original color
- * @return Modified color
- */
- public abstract Color filterRGB(Color c);
-}
-
diff --git a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatSVGIcon.java b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatSVGIcon.java
index 2951c8a3..fd0c01ef 100644
--- a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatSVGIcon.java
+++ b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatSVGIcon.java
@@ -66,7 +66,7 @@ public class FlatSVGIcon
private final boolean disabled;
private final ClassLoader classLoader;
- private RGBImageFilter userFilter = null;
+ private ColorFilter userColorFilter = null;
private SVGDiagram diagram;
private boolean dark;
@@ -161,7 +161,7 @@ public class FlatSVGIcon
this( name, -1, -1, scale, false, classLoader );
}
- private 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.classLoader = classLoader;
this.width = width;
@@ -171,16 +171,29 @@ public class FlatSVGIcon
}
/**
- * Sets an RGBImageFilter to be used when painting the icon.
- * For simple RGB modifications you can use the {@link FlatRGBFilter}.
- * @param filter
+ * Sets a color filter that can freely modify colors of this icon during painting.
+ *
+ * This method accepts a {@link ColorFilter}. Usually you would want to use a ColorFilter created using the
+ * {@link ColorFilter#ColorFilter(Function)} constructor.
+ *
+ * This can be used to brighten colors of the icon:
+ *
icon.setFilter( new FlatSVGIcon.ColorFilter( color -> color.brighter() ) );
icon.setFilter( new FlatSVGIcon.ColorFilter( color -> Color.RED ) );
new ColorFilter( color -> color.brighter() );+ *
new ColorFilter( color -> Color.RED );+ *
filter.setFilter( color -> color.brighter() );+ *
filter.setFilter( color -> Color.RED );+ *