Skip to content

Commit 6040f01

Browse files
filter icons according to color theme
1 parent 7ca3f2d commit 6040f01

35 files changed

+491
-171
lines changed

api/src/main/java/lsfusion/base/BaseUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class BaseUtils {
5757
private static final int STRING_SERIALIZATION_CHUNK_SIZE = 65535/3;
5858

5959
public static Integer getApiVersion() {
60-
return 115;
60+
return 116;
6161
}
6262

6363
public static String getPlatformVersion() {

api/src/main/java/lsfusion/base/file/SerializableImageIconHolder.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ public ImageIcon getImage() {
3838
}
3939

4040
public ImageIcon getImage(ColorTheme colorTheme) {
41-
ImageIcon themeImage = images.get(colorTheme);
42-
return themeImage != null ? themeImage : images.get(DEFAULT);
41+
return images.get(colorTheme);
42+
}
43+
44+
public void putImage(ColorTheme colorTheme, ImageIcon image) {
45+
images.put(colorTheme, image);
4346
}
4447

4548
public void setImage(ImageIcon image, String imagePath) {
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
package lsfusion.interop.base.view;
22

3-
import java.awt.*;
43
import java.util.HashMap;
54
import java.util.Map;
65

7-
import static java.lang.Math.max;
8-
import static java.lang.Math.min;
9-
106
public enum ColorTheme {
11-
LIGHT("light", null), DARK("dark", 0.33f);
7+
LIGHT("light"), DARK("dark");
128

139
public final static ColorTheme DEFAULT = LIGHT;
1410

1511
private final String sid;
16-
private final Float colorInvertFactor;
1712

18-
ColorTheme(String sid, Float invertFactor) {
13+
ColorTheme(String sid) {
1914
this.sid = sid;
20-
this.colorInvertFactor = invertFactor;
2115
}
2216

2317
public String getSid() {
@@ -58,39 +52,4 @@ public String getImagePath(String path) {
5852
public static ColorTheme get(String sid) {
5953
return lookup.get(sid);
6054
}
61-
62-
// based on java.awt.Color's darker()
63-
public Color getDisplayBackground(Color baseColor) {
64-
if (baseColor != null && colorInvertFactor != null) {
65-
return new Color(max((int) (baseColor.getRed() * colorInvertFactor), 0),
66-
max((int) (baseColor.getGreen() * colorInvertFactor), 0),
67-
max((int) (baseColor.getBlue() * colorInvertFactor), 0),
68-
baseColor.getAlpha());
69-
}
70-
return baseColor;
71-
}
72-
73-
// based on java.awt.Color's brighter()
74-
public Color getDisplayForeground(Color baseColor) {
75-
if (baseColor != null && colorInvertFactor != null) {
76-
int r = baseColor.getRed();
77-
int g = baseColor.getGreen();
78-
int b = baseColor.getBlue();
79-
int alpha = baseColor.getAlpha();
80-
81-
int i = (int) (1.0 / (1.0 - colorInvertFactor));
82-
if (r == 0 && g == 0 && b == 0) {
83-
return new Color(i, i, i, alpha);
84-
}
85-
if (r > 0 && r < i) r = i;
86-
if (g > 0 && g < i) g = i;
87-
if (b > 0 && b < i) b = i;
88-
89-
return new Color(min((int) (r / colorInvertFactor), 255),
90-
min((int) (g / colorInvertFactor), 255),
91-
min((int) (b / colorInvertFactor), 255),
92-
alpha);
93-
}
94-
return baseColor;
95-
}
9655
}

api/src/main/java/lsfusion/interop/form/design/ComponentDesign.java

-13
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,6 @@ public SerializableImageIconHolder getImageHolder() {
4646
return imageHolder;
4747
}
4848

49-
public void designComponent(JComponent comp, ColorTheme colorTheme) {
50-
installFont(comp);
51-
52-
if (background != null) {
53-
comp.setBackground(colorTheme.getDisplayBackground(background));
54-
comp.setOpaque(true);
55-
}
56-
57-
if (foreground != null) {
58-
comp.setForeground(colorTheme.getDisplayForeground(foreground));
59-
}
60-
}
61-
6249
public void installFont(JComponent comp) {
6350
if (font != null) {
6451
comp.setFont(getFont(comp));

build/platform-base/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<validation.api.version>1.0.0.GA</validation.api.version>
7676
<json.version>20180813</json.version>
7777
<junrar.version>0.7</junrar.version>
78+
<ph.css.version>6.2.2</ph.css.version>
7879
<jstl.version>1.2</jstl.version>
7980
<commons.fileupload.version>1.2.2</commons.fileupload.version>
8081
<url.rewriter.version>4.0.3</url.rewriter.version>
@@ -484,6 +485,11 @@
484485
<artifactId>junrar</artifactId>
485486
<version>${junrar.version}</version>
486487
</dependency>
488+
<dependency>
489+
<groupId>com.helger</groupId>
490+
<artifactId>ph-css</artifactId>
491+
<version>${ph.css.version}</version>
492+
</dependency>
487493
<dependency>
488494
<groupId>jstl</groupId>
489495
<artifactId>jstl</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package lsfusion.client.base.view;
2+
3+
import lsfusion.client.controller.MainController;
4+
import lsfusion.interop.form.design.ComponentDesign;
5+
6+
import javax.swing.*;
7+
import java.awt.*;
8+
import java.awt.image.FilteredImageSource;
9+
import java.awt.image.ImageProducer;
10+
import java.awt.image.RGBImageFilter;
11+
12+
import static java.lang.Math.max;
13+
import static java.lang.Math.min;
14+
import static lsfusion.client.base.view.SwingDefaults.getDefaultThemePanelBackground;
15+
16+
public class ClientColorUtils {
17+
public static int filterColor(int baseRGB, Color baseBackgroundColor, Color newBackgroundColor, Color customLimitColor) {
18+
if (baseRGB != 0) {
19+
Color color = new Color(baseRGB, true);
20+
float[] hsb = Color.RGBtoHSB(
21+
max(min(baseBackgroundColor.getRed() - color.getRed() + newBackgroundColor.getRed(), customLimitColor.getRed()), 0),
22+
max(min(baseBackgroundColor.getGreen() - color.getGreen() + newBackgroundColor.getGreen(), customLimitColor.getGreen()), 0),
23+
max(min(baseBackgroundColor.getBlue() - color.getBlue() + newBackgroundColor.getBlue(), customLimitColor.getBlue()), 0),
24+
null
25+
);
26+
Color color1 = new Color(Color.HSBtoRGB(Math.abs(0.5f + hsb[0]), hsb[1], hsb[2]));
27+
return new Color(color1.getRed(), color1.getGreen(), color1.getBlue(), color.getAlpha()).getRGB();
28+
}
29+
30+
return baseRGB;
31+
}
32+
33+
public static Color getDisplayColor(Color baseColor) {
34+
if (baseColor != null && !MainController.colorTheme.isDefault()) {
35+
return new Color(filterColor(baseColor.getRGB(),
36+
SwingDefaults.getDefaultThemeTableCellBackground(),
37+
SwingDefaults.getTableCellBackground(),
38+
SwingDefaults.getTableCellForeground()));
39+
}
40+
return baseColor;
41+
}
42+
43+
public static ImageIcon createFilteredImageIcon(ImageIcon i, Color baseBackgroundColor, Color newBackgroundColor, Color customLimitColor) {
44+
ImageProducer prod = new FilteredImageSource(i.getImage().getSource(), new ImageThemeFilter(baseBackgroundColor, newBackgroundColor, customLimitColor));
45+
return new ImageIcon(new Label().createImage(prod));
46+
}
47+
48+
public static ImageIcon createFilteredImageIcon(ImageIcon i) {
49+
return createFilteredImageIcon(i, getDefaultThemePanelBackground(), SwingDefaults.getPanelBackground(), SwingDefaults.getButtonForeground());
50+
}
51+
52+
public static void designComponent(JComponent comp, ComponentDesign design) {
53+
if (design.background != null) {
54+
comp.setBackground(getDisplayColor(design.background));
55+
comp.setOpaque(true);
56+
}
57+
58+
if (design.foreground != null) {
59+
comp.setForeground(getDisplayColor(design.foreground));
60+
}
61+
}
62+
63+
public static class ImageThemeFilter extends RGBImageFilter {
64+
private Color baseBackgroundColor;
65+
Color newBackgroundColor;
66+
Color customLimitColor;
67+
68+
public ImageThemeFilter(Color baseBackgroundColor, Color newBackgroundColor, Color customLimitColor) {
69+
this.baseBackgroundColor = baseBackgroundColor;
70+
this.newBackgroundColor = newBackgroundColor;
71+
this.customLimitColor = customLimitColor;
72+
}
73+
74+
@Override
75+
public int filterRGB(int x, int y, int rgb) {
76+
return ClientColorUtils.filterColor(rgb, baseBackgroundColor, newBackgroundColor, customLimitColor);
77+
}
78+
}
79+
}

desktop-client/src/main/java/lsfusion/client/base/view/ClientImages.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package lsfusion.client.base.view;
22

3+
import lsfusion.base.file.SerializableImageIconHolder;
34
import lsfusion.client.controller.MainController;
5+
import lsfusion.interop.base.view.ColorTheme;
46

57
import javax.swing.*;
68
import java.util.HashMap;
79
import java.util.Map;
810

911
import static lsfusion.base.ResourceUtils.readImage;
12+
import static lsfusion.interop.base.view.ColorTheme.DEFAULT;
1013

1114
public class ClientImages {
1215
private static Map<String, ImageIcon> images = new HashMap<>();
@@ -18,13 +21,37 @@ public static ImageIcon get(String path) {
1821
if (image != null) {
1922
images.put(path, image);
2023
} else {
21-
images.put(path, image = readImage(path)); // default color theme
24+
ImageIcon defaultThemeImage = readImage(DEFAULT.getImagePath(path));
25+
if (defaultThemeImage != null) {
26+
image = ClientColorUtils.createFilteredImageIcon(defaultThemeImage);
27+
images.put(path, image);
28+
}
2229
}
2330
}
2431
return image;
2532
}
2633

2734
public static void reset() {
2835
images.clear();
29-
}
36+
}
37+
38+
public static ImageIcon getImage(SerializableImageIconHolder imageHolder) {
39+
return getImage(imageHolder, MainController.colorTheme);
40+
}
41+
42+
public static ImageIcon getImage(SerializableImageIconHolder imageHolder, ColorTheme colorTheme) {
43+
if (imageHolder == null) {
44+
return null;
45+
}
46+
47+
ImageIcon themeImage = imageHolder.getImage(colorTheme);
48+
if (themeImage == null) {
49+
ImageIcon imageIcon = ClientColorUtils.createFilteredImageIcon(imageHolder.getImage(DEFAULT));
50+
imageHolder.putImage(colorTheme, imageIcon);
51+
return imageIcon;
52+
}
53+
return themeImage;
54+
}
55+
56+
3057
}

0 commit comments

Comments
 (0)