b631507294103cfa5318fce24a870c2629876458
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / ColorHelper.java
1 package net.mograsim.logic.ui;
2
3 import java.util.function.Consumer;
4 import java.util.function.Supplier;
5
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.graphics.Color;
8 import org.eclipse.swt.graphics.Device;
9
10 import net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.mograsim.logic.core.types.ColorDefinition;
12 import net.mograsim.logic.core.types.ColorDefinition.BuiltInColor;
13
14 //TODO replace with a proper ColorManager
15 public class ColorHelper
16 {
17         public static void executeWithDifferentForeground(GeneralGC gc, ColorDefinition col, Runnable exec)
18         {
19                 executeWithDifferentColor(gc.getDevice(), col, gc::getForeground, gc::setForeground, exec);
20         }
21
22         public static void executeWithDifferentBackground(GeneralGC gc, ColorDefinition col, Runnable exec)
23         {
24                 executeWithDifferentColor(gc.getDevice(), col, gc::getBackground, gc::setBackground, exec);
25         }
26
27         private static void executeWithDifferentColor(Device device, ColorDefinition col, Supplier<Color> getColor, Consumer<Color> setColor,
28                         Runnable exec)
29         {
30                 Color oldColor = getColor.get();
31                 boolean isNoSystemColor = col.builtInColor == null;
32                 Color newColor;
33                 if (isNoSystemColor)
34                         newColor = new Color(device, col.r, col.g, col.b);
35                 else
36                         newColor = device.getSystemColor(ColorHelper.toSWTColorConstant(col.builtInColor));
37                 setColor.accept(newColor);
38
39                 exec.run();
40
41                 setColor.accept(oldColor);
42                 if (isNoSystemColor)
43                         newColor.dispose();
44         }
45
46         public static int toSWTColorConstant(BuiltInColor col)
47         {
48                 switch (col)
49                 {
50                 case COLOR_BLACK:
51                         return SWT.COLOR_BLACK;
52                 case COLOR_BLUE:
53                         return SWT.COLOR_BLUE;
54                 case COLOR_CYAN:
55                         return SWT.COLOR_CYAN;
56                 case COLOR_DARK_BLUE:
57                         return SWT.COLOR_DARK_BLUE;
58                 case COLOR_DARK_CYAN:
59                         return SWT.COLOR_DARK_CYAN;
60                 case COLOR_DARK_GRAY:
61                         return SWT.COLOR_DARK_GRAY;
62                 case COLOR_DARK_GREEN:
63                         return SWT.COLOR_DARK_GREEN;
64                 case COLOR_DARK_MAGENTA:
65                         return SWT.COLOR_DARK_MAGENTA;
66                 case COLOR_DARK_RED:
67                         return SWT.COLOR_DARK_RED;
68                 case COLOR_DARK_YELLOW:
69                         return SWT.COLOR_DARK_YELLOW;
70                 case COLOR_GRAY:
71                         return SWT.COLOR_GRAY;
72                 case COLOR_GREEN:
73                         return SWT.COLOR_GREEN;
74                 case COLOR_MAGENTA:
75                         return SWT.COLOR_MAGENTA;
76                 case COLOR_RED:
77                         return SWT.COLOR_RED;
78                 case COLOR_WHITE:
79                         return SWT.COLOR_WHITE;
80                 case COLOR_YELLOW:
81                         return SWT.COLOR_YELLOW;
82                 default:
83                         throw new IllegalArgumentException("Unknown enum constant: " + col);
84                 }
85         }
86
87         private ColorHelper()
88         {
89                 throw new UnsupportedOperationException("No instances of ColorHelper");
90         }
91 }