|
| 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 | +} |
0 commit comments