b44a975ebee7815fe2f1ff288e21c1cbcd639334
[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.graphics.Color;
7 import org.eclipse.swt.graphics.Device;
8
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.mograsim.preferences.ColorDefinition;
11 import net.mograsim.preferences.ColorManager;
12
13 public class ColorHelper
14 {
15         public static void executeWithDifferentForeground(GeneralGC gc, ColorDefinition col, Runnable exec)
16         {
17                 executeWithDifferentColor(gc.getDevice(), col, gc::getForeground, gc::setForeground, exec);
18         }
19
20         public static void executeWithDifferentBackground(GeneralGC gc, ColorDefinition col, Runnable exec)
21         {
22                 executeWithDifferentColor(gc.getDevice(), col, gc::getBackground, gc::setBackground, exec);
23         }
24
25         private static void executeWithDifferentColor(Device device, ColorDefinition col, Supplier<Color> getColor, Consumer<Color> setColor,
26                         Runnable exec)
27         {
28                 ColorManager cm = ColorManager.current();
29                 Color oldColor = getColor.get();
30                 Color newColor = cm.toColor(device, col);
31                 setColor.accept(newColor);
32                 exec.run();
33                 setColor.accept(oldColor);
34                 cm.dispose(newColor);
35         }
36
37         private ColorHelper()
38         {
39                 throw new UnsupportedOperationException("No instances of ColorHelper");
40         }
41 }