FlatSVGIcon: some additions to PR #817:

- create new `LinearGradientPaint` only if color has changed
- preserve old `LinearGradientPaint.colorSpace` and `transform`
This commit is contained in:
Karl Tauber
2024-03-12 12:20:31 +01:00
parent 4be97b6ea6
commit 1f1ebc3c44
10 changed files with 142 additions and 10 deletions

View File

@@ -1,6 +1,13 @@
FlatLaf Change Log FlatLaf Change Log
================== ==================
## 3.5-SNAPSHOT
#### Fixed bugs
- Extras: `FlatSVGIcon` color filters now support linear gradients. (PR #817)
## 3.4 ## 3.4
#### New features and improvements #### New features and improvements

View File

@@ -985,11 +985,12 @@ public class FlatSVGIcon
paint = filterColor( (Color) paint ); paint = filterColor( (Color) paint );
else if( paint instanceof LinearGradientPaint ) { else if( paint instanceof LinearGradientPaint ) {
LinearGradientPaint oldPaint = (LinearGradientPaint) paint; LinearGradientPaint oldPaint = (LinearGradientPaint) paint;
paint = new LinearGradientPaint( oldPaint.getStartPoint(), Color[] newColors = filterColors( oldPaint.getColors() );
oldPaint.getEndPoint(), if( newColors != null ) {
oldPaint.getFractions(), paint = new LinearGradientPaint( oldPaint.getStartPoint(), oldPaint.getEndPoint(),
filterColors( oldPaint.getColors() ), oldPaint.getFractions(), newColors, oldPaint.getCycleMethod(),
oldPaint.getCycleMethod() ); oldPaint.getColorSpace(), oldPaint.getTransform() );
}
} }
super.setPaint( paint ); super.setPaint( paint );
} }
@@ -1013,9 +1014,12 @@ public class FlatSVGIcon
private Color[] filterColors( Color[] colors ) { private Color[] filterColors( Color[] colors ) {
Color[] newColors = new Color[colors.length]; Color[] newColors = new Color[colors.length];
for( int i = 0; i < colors.length; i++ ) boolean changed = false;
for( int i = 0; i < colors.length; i++ ) {
newColors[i] = filterColor( colors[i] ); newColors[i] = filterColor( colors[i] );
return newColors; changed = (changed || newColors[i] != colors[i]);
}
return changed ? newColors : null;
} }
} }
} }

View File

@@ -16,6 +16,7 @@
package com.formdev.flatlaf.testing.extras; package com.formdev.flatlaf.testing.extras;
import java.awt.*;
import javax.swing.*; import javax.swing.*;
import com.formdev.flatlaf.extras.*; import com.formdev.flatlaf.extras.*;
import com.formdev.flatlaf.extras.components.*; import com.formdev.flatlaf.extras.components.*;
@@ -75,6 +76,12 @@ public class FlatExtrasTest
disabledTabbedPane2.setIconAt( 0, icon ); disabledTabbedPane2.setIconAt( 0, icon );
disabledTabbedPane2.setDisabledIconAt( 0, disabledIcon ); disabledTabbedPane2.setDisabledIconAt( 0, disabledIcon );
addJSVGIcon( "linearGradient.svg", 64, 128 );
addJSVGIcon( "stripes.svg", 128, 128 );
addJSVGIcon( "gradientText0.svg", 128, 128 );
addJSVGIcon( "gradientText1.svg", 128, 128 );
addJSVGIcon( "gradientText2.svg", 128, 128 );
disabledChanged(); disabledChanged();
} }
@@ -82,6 +89,10 @@ public class FlatExtrasTest
svgIconsPanel.add( new JLabel( new FlatSVGIcon( "com/formdev/flatlaf/demo/extras/svg/" + name ) ) ); svgIconsPanel.add( new JLabel( new FlatSVGIcon( "com/formdev/flatlaf/demo/extras/svg/" + name ) ) );
} }
private void addJSVGIcon( String name, int width, int height ) {
gradientIconsPanel.add( new JLabel( new FlatSVGIcon( "com/formdev/flatlaf/testing/extras/jsvg/" + name, width, height ) ) );
}
private void triStateCheckBox1Changed() { private void triStateCheckBox1Changed() {
triStateLabel1.setText( triStateCheckBox1.getState().toString() ); triStateLabel1.setText( triStateCheckBox1.getState().toString() );
} }
@@ -104,6 +115,9 @@ public class FlatExtrasTest
disabledLabel2.setEnabled( enabled ); disabledLabel2.setEnabled( enabled );
disabledButton2.setEnabled( enabled ); disabledButton2.setEnabled( enabled );
disabledTabbedPane2.setEnabledAt( 0, enabled ); disabledTabbedPane2.setEnabledAt( 0, enabled );
for( Component c : gradientIconsPanel.getComponents() )
c.setEnabled( enabled );
} }
@Override @Override
@@ -141,6 +155,7 @@ public class FlatExtrasTest
disabledButton2 = new JButton(); disabledButton2 = new JButton();
disabledTabbedPane2 = new JTabbedPane(); disabledTabbedPane2 = new JTabbedPane();
label6 = new JLabel(); label6 = new JLabel();
gradientIconsPanel = new JPanel();
//======== this ======== //======== this ========
setLayout(new MigLayout( setLayout(new MigLayout(
@@ -156,6 +171,7 @@ public class FlatExtrasTest
"[]" + "[]" +
"[]" + "[]" +
"[]" + "[]" +
"[]" +
"[]")); "[]"));
//---- label1 ---- //---- label1 ----
@@ -251,6 +267,12 @@ public class FlatExtrasTest
label6.setText("setIcon() and setDisabledIcon()"); label6.setText("setIcon() and setDisabledIcon()");
label6.setEnabled(false); label6.setEnabled(false);
add(label6, "cell 1 6 2 1,gapx 20"); add(label6, "cell 1 6 2 1,gapx 20");
//======== gradientIconsPanel ========
{
gradientIconsPanel.setLayout(new FlowLayout());
}
add(gradientIconsPanel, "cell 1 7 2 1");
// JFormDesigner - End of component initialization //GEN-END:initComponents // JFormDesigner - End of component initialization //GEN-END:initComponents
} }
@@ -275,5 +297,6 @@ public class FlatExtrasTest
private JButton disabledButton2; private JButton disabledButton2;
private JTabbedPane disabledTabbedPane2; private JTabbedPane disabledTabbedPane2;
private JLabel label6; private JLabel label6;
private JPanel gradientIconsPanel;
// JFormDesigner - End of variables declaration //GEN-END:variables // JFormDesigner - End of variables declaration //GEN-END:variables
} }

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "7.0.3.1.342" Java: "15" encoding: "UTF-8" JFDML JFormDesigner: "8.2.0.0.331" Java: "21" encoding: "UTF-8"
new FormModel { new FormModel {
contentType: "form/swing" contentType: "form/swing"
@@ -6,7 +6,7 @@ new FormModel {
add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "ltr,insets dialog,hidemode 3" "$layoutConstraints": "ltr,insets dialog,hidemode 3"
"$columnConstraints": "[][][left]" "$columnConstraints": "[][][left]"
"$rowConstraints": "[][][][][][][]" "$rowConstraints": "[][][][][][][][]"
} ) { } ) {
name: "this" name: "this"
add( new FormComponent( "javax.swing.JLabel" ) { add( new FormComponent( "javax.swing.JLabel" ) {
@@ -143,9 +143,14 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 6 2 1,gapx 20" "value": "cell 1 6 2 1,gapx 20"
} ) } )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.FlowLayout ) ) {
name: "gradientIconsPanel"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 7 2 1"
} )
}, new FormLayoutConstraints( null ) { }, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 ) "location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 595, 470 ) "size": new java.awt.Dimension( 645, 470 )
} ) } )
} }
} }

View File

@@ -0,0 +1,3 @@
The SVG icons in this folder are from JSVG,
which is licensed under the MIT License
See: https://github.com/weisJ/jsvg

View File

@@ -0,0 +1,12 @@
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"
stroke="none">
<defs>
<linearGradient id="myGradient"
gradientTransform="rotate(90)">
<stop offset="5%" stop-color="gold" />
<stop offset="95%" stop-color="red" />
</linearGradient>
</defs>
<rect x="0" y="50" width="100" height="40" fill="url(#myGradient)" />
<text x="0" y="50" fill="url(#myGradient)">Hello lovely World!</text>
</svg>

After

Width:  |  Height:  |  Size: 417 B

View File

@@ -0,0 +1,14 @@
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"
stroke="none">
<defs>
<linearGradient id="myGradient">
<stop offset="5%" stop-color="gold" />
<stop offset="95%" stop-color="red" />
</linearGradient>
</defs>
<text x="0" y="50">
Hello
<tspan fill="url(#myGradient)">lovely</tspan>
World!
</text>
</svg>

After

Width:  |  Height:  |  Size: 336 B

View File

@@ -0,0 +1,14 @@
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"
stroke="none">
<defs>
<linearGradient id="myGradient">
<stop offset="5%" stop-color="gold" />
<stop offset="95%" stop-color="red" />
</linearGradient>
</defs>
<text x="0" y="50" fill="url(#myGradient)">
Hello
<tspan>lovely</tspan>
World!
</text>
</svg>

After

Width:  |  Height:  |  Size: 336 B

View File

@@ -0,0 +1,23 @@
<svg width="120" height="240" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="Gradient1"
gradientUnits="objectBoundingBox">
<stop offset="0%" stop-color="red" />
<stop offset="50%" stop-color="black" stop-opacity="0" />
<stop offset="100%" stop-color="blue" />
</linearGradient>
<linearGradient id="Gradient2" x1="0" x2="0" y1="0"
y2="1">
<stop offset="0%" stop-color="red" />
<stop offset="50%" stop-color="black" stop-opacity="0" />
<stop offset="100%" stop-color="blue" />
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="100"
height="100" fill="url(#Gradient1)" />
<rect x="10" y="120" rx="15" ry="15" width="100" height="100"
fill="url(#Gradient2)" />
</svg>

After

Width:  |  Height:  |  Size: 767 B

View File

@@ -0,0 +1,27 @@
<svg xmlns="http://www.w3.org/2000/svg" width="500px" height="500px">
<defs>
<linearGradient id="stripes">
<stop offset="0%" stop-color="red" />
<stop offset="10%" stop-color="red" />
<stop offset="10%" stop-color="orange" />
<stop offset="20%" stop-color="orange" />
<stop offset="20%" stop-color="yellow" />
<stop offset="30%" stop-color="yellow" />
<stop offset="30%" stop-color="lightgreen" />
<stop offset="40%" stop-color="lightgreen" />
<stop offset="40%" stop-color="green" />
<stop offset="50%" stop-color="green" />
<stop offset="50%" stop-color="purple" />
<stop offset="60%" stop-color="purple" />
<stop offset="60%" stop-color="blue" />
<stop offset="70%" stop-color="blue" />
<stop offset="70%" stop-color="skyblue" />
<stop offset="80%" stop-color="skyblue" />
<stop offset="80%" stop-color="pink" />
<stop offset="90%" stop-color="pink" />
<stop offset="90%" stop-color="white" />
<stop offset="100%" stop-color="white" />
</linearGradient>
</defs>
<rect width="100%" height="100%" fill="url(#stripes)" />
</svg>

After

Width:  |  Height:  |  Size: 1.1 KiB