mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-08 06:50:56 +03:00
Added an option to specify an RGBImageFilter to a FlatSVGIcon
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -66,6 +66,8 @@ public class FlatSVGIcon
|
|||||||
private final boolean disabled;
|
private final boolean disabled;
|
||||||
private final ClassLoader classLoader;
|
private final ClassLoader classLoader;
|
||||||
|
|
||||||
|
private RGBImageFilter userFilter = null;
|
||||||
|
|
||||||
private SVGDiagram diagram;
|
private SVGDiagram diagram;
|
||||||
private boolean dark;
|
private boolean dark;
|
||||||
|
|
||||||
@@ -168,6 +170,19 @@ public class FlatSVGIcon
|
|||||||
this.disabled = disabled;
|
this.disabled = disabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets an RGBImageFilter to be used when painting the icon.
|
||||||
|
* For simple RGB modifications you can use the {@link FlatRGBFilter}.
|
||||||
|
* @param filter
|
||||||
|
*/
|
||||||
|
public void setFilter(RGBImageFilter filter) {
|
||||||
|
this.userFilter = filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RGBImageFilter getFilter() {
|
||||||
|
return userFilter;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new icon with given width and height, which is derived from this icon.
|
* Creates a new icon with given width and height, which is derived from this icon.
|
||||||
*
|
*
|
||||||
@@ -303,7 +318,7 @@ public class FlatSVGIcon
|
|||||||
: GrayFilter.createDisabledIconFilter( dark );
|
: GrayFilter.createDisabledIconFilter( dark );
|
||||||
}
|
}
|
||||||
|
|
||||||
Graphics2D g2 = new GraphicsFilter( (Graphics2D) g.create(), ColorFilter.getInstance(), grayFilter );
|
Graphics2D g2 = new GraphicsFilter( (Graphics2D) g.create(), ColorFilter.getInstance(), grayFilter, this.userFilter );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FlatUIUtils.setRenderingHints( g2 );
|
FlatUIUtils.setRenderingHints( g2 );
|
||||||
@@ -457,11 +472,13 @@ public class FlatSVGIcon
|
|||||||
{
|
{
|
||||||
private final ColorFilter colorFilter;
|
private final ColorFilter colorFilter;
|
||||||
private final RGBImageFilter grayFilter;
|
private final RGBImageFilter grayFilter;
|
||||||
|
private final RGBImageFilter userFilter;
|
||||||
|
|
||||||
public GraphicsFilter( Graphics2D delegate, ColorFilter colorFilter, RGBImageFilter grayFilter ) {
|
public GraphicsFilter( Graphics2D delegate, ColorFilter colorFilter, RGBImageFilter grayFilter, RGBImageFilter userFilter) {
|
||||||
super( delegate );
|
super( delegate );
|
||||||
this.colorFilter = colorFilter;
|
this.colorFilter = colorFilter;
|
||||||
this.grayFilter = grayFilter;
|
this.grayFilter = grayFilter;
|
||||||
|
this.userFilter = userFilter;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -477,6 +494,11 @@ public class FlatSVGIcon
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Color filterColor( Color color ) {
|
private Color filterColor( Color color ) {
|
||||||
|
if( userFilter != null ) {
|
||||||
|
int oldRGB = color.getRGB();
|
||||||
|
int newRGB = userFilter.filterRGB( 0, 0, oldRGB );
|
||||||
|
color = (newRGB != oldRGB) ? new Color( newRGB, true ) : color;
|
||||||
|
}
|
||||||
if( colorFilter != null )
|
if( colorFilter != null )
|
||||||
color = colorFilter.filter( color );
|
color = colorFilter.filter( color );
|
||||||
if( grayFilter != null ) {
|
if( grayFilter != null ) {
|
||||||
|
|||||||
Reference in New Issue
Block a user