mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-07 06:20:53 +03:00
FlatSVGIcon: support light and dark mappings in single color filter
This commit is contained in:
@@ -29,6 +29,7 @@ import java.awt.image.BufferedImage;
|
|||||||
import java.awt.image.RGBImageFilter;
|
import java.awt.image.RGBImageFilter;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
@@ -478,7 +479,7 @@ public class FlatSVGIcon
|
|||||||
|
|
||||||
private static Boolean darkLaf;
|
private static Boolean darkLaf;
|
||||||
|
|
||||||
private static boolean isDarkLaf() {
|
public static boolean isDarkLaf() {
|
||||||
if( darkLaf == null ) {
|
if( darkLaf == null ) {
|
||||||
lafChanged();
|
lafChanged();
|
||||||
|
|
||||||
@@ -512,7 +513,8 @@ public class FlatSVGIcon
|
|||||||
private static ColorFilter instance;
|
private static ColorFilter instance;
|
||||||
|
|
||||||
private Map<Integer, String> rgb2keyMap;
|
private Map<Integer, String> rgb2keyMap;
|
||||||
private Map<Color, Color> color2colorMap;
|
private Map<Color, Color> colorMap;
|
||||||
|
private Map<Color, Color> darkColorMap;
|
||||||
private Function<Color, Color> mapper;
|
private Function<Color, Color> mapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -582,29 +584,85 @@ public class FlatSVGIcon
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds color mappings.
|
* Returns the color mappings used for light themes.
|
||||||
|
*
|
||||||
|
* @since 1.2
|
||||||
*/
|
*/
|
||||||
public void addAll( Map<Color, Color> from2toMap ) {
|
public Map<Color, Color> getLightColorMap() {
|
||||||
if( color2colorMap == null )
|
return (colorMap != null)
|
||||||
color2colorMap = new HashMap<>();
|
? Collections.unmodifiableMap( colorMap )
|
||||||
color2colorMap.putAll( from2toMap );
|
: Collections.emptyMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a color mapping.
|
* Returns the color mappings used for dark themes.
|
||||||
|
*
|
||||||
|
* @since 1.2
|
||||||
|
*/
|
||||||
|
public Map<Color, Color> getDarkColorMap() {
|
||||||
|
return (darkColorMap != null)
|
||||||
|
? Collections.unmodifiableMap( darkColorMap )
|
||||||
|
: getLightColorMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds color mappings. Used for light and dark themes.
|
||||||
|
*/
|
||||||
|
public void addAll( Map<Color, Color> from2toMap ) {
|
||||||
|
ensureColorMap();
|
||||||
|
|
||||||
|
colorMap.putAll( from2toMap );
|
||||||
|
if( darkColorMap != null )
|
||||||
|
darkColorMap.putAll( from2toMap );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a color mappings, which has different colors for light and dark themes.
|
||||||
|
*
|
||||||
|
* @since 1.2
|
||||||
|
*/
|
||||||
|
public void addAll( Map<Color, Color> from2toLightMap, Map<Color, Color> from2toDarkMap ) {
|
||||||
|
ensureColorMap();
|
||||||
|
ensureDarkColorMap();
|
||||||
|
|
||||||
|
colorMap.putAll( from2toLightMap );
|
||||||
|
darkColorMap.putAll( from2toDarkMap );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a color mapping. Used for light and dark themes.
|
||||||
*/
|
*/
|
||||||
public void add( Color from, Color to ) {
|
public void add( Color from, Color to ) {
|
||||||
if( color2colorMap == null )
|
ensureColorMap();
|
||||||
color2colorMap = new HashMap<>();
|
|
||||||
color2colorMap.put( from, to );
|
colorMap.put( from, to );
|
||||||
|
if( darkColorMap != null )
|
||||||
|
darkColorMap.put( from, to );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a color mapping, which has different colors for light and dark themes.
|
||||||
|
*
|
||||||
|
* @since 1.2
|
||||||
|
*/
|
||||||
|
public void add( Color from, Color toLight, Color toDark ) {
|
||||||
|
ensureColorMap();
|
||||||
|
ensureDarkColorMap();
|
||||||
|
|
||||||
|
if( toLight != null )
|
||||||
|
colorMap.put( from, toLight );
|
||||||
|
if( toDark != null )
|
||||||
|
darkColorMap.put( from, toDark );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a specific color mapping.
|
* Removes a specific color mapping.
|
||||||
*/
|
*/
|
||||||
public void remove( Color from ) {
|
public void remove( Color from ) {
|
||||||
if( color2colorMap != null )
|
if( colorMap != null )
|
||||||
color2colorMap.remove( from );
|
colorMap.remove( from );
|
||||||
|
if( darkColorMap != null )
|
||||||
|
darkColorMap.remove( from );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -613,8 +671,18 @@ public class FlatSVGIcon
|
|||||||
* @since 1.2
|
* @since 1.2
|
||||||
*/
|
*/
|
||||||
public void removeAll() {
|
public void removeAll() {
|
||||||
if( color2colorMap != null )
|
colorMap = null;
|
||||||
color2colorMap.clear();
|
darkColorMap = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureColorMap() {
|
||||||
|
if( colorMap == null )
|
||||||
|
colorMap = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureDarkColorMap() {
|
||||||
|
if( darkColorMap == null )
|
||||||
|
darkColorMap = new HashMap<>( colorMap );
|
||||||
}
|
}
|
||||||
|
|
||||||
public Color filter( Color color ) {
|
public Color filter( Color color ) {
|
||||||
@@ -629,8 +697,9 @@ public class FlatSVGIcon
|
|||||||
};
|
};
|
||||||
|
|
||||||
private Color applyMappings( Color color ) {
|
private Color applyMappings( Color color ) {
|
||||||
if( color2colorMap != null ) {
|
if( colorMap != null ) {
|
||||||
Color newColor = color2colorMap.get( color );
|
Map<Color, Color> map = (darkColorMap != null && isDarkLaf()) ? darkColorMap : colorMap;
|
||||||
|
Color newColor = map.get( color );
|
||||||
if( newColor != null )
|
if( newColor != null )
|
||||||
return newColor;
|
return newColor;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user