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