forked from Bram-Hub/LEGUP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin work on better color theming such as for color blindness so use…
…rs can specify their own colors for Bram-Hub#426.
- Loading branch information
1 parent
32371ed
commit 46b7abb
Showing
7 changed files
with
156 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/edu/rpi/legup/ui/color/ColorPreferences.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package edu.rpi.legup.ui.color; | ||
|
||
import java.awt.*; | ||
import java.io.*; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.*; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class ColorPreferences { | ||
|
||
private static final String COLOR_THEME_FILE_NAME = "color-theme.txt"; | ||
|
||
private static final Map<UIColor, Color> COLOR_MAP = new EnumMap<>(UIColor.class); | ||
|
||
public enum UIColor { | ||
|
||
CORRECT, | ||
INCORRECT | ||
|
||
; | ||
|
||
public String configKey() { | ||
return this.toString().toLowerCase().replace('_', '-'); | ||
} | ||
|
||
public Color get() { | ||
return COLOR_MAP.get(this); | ||
} | ||
} | ||
|
||
public static void loadColorScheme() { | ||
final File file = Path.of(COLOR_THEME_FILE_NAME).toFile(); | ||
final InputStream input = ClassLoader.getSystemClassLoader().getResourceAsStream(COLOR_THEME_FILE_NAME); | ||
BufferedReader reader; | ||
boolean copyResourceToFile = false; | ||
if (!file.exists()) { | ||
try { | ||
file.createNewFile(); | ||
copyResourceToFile = true; | ||
} | ||
catch (IOException e) { | ||
System.err.println("Could not create " + COLOR_THEME_FILE_NAME); | ||
} | ||
if (input == null) { | ||
throw new RuntimeException("Could not find resource " + COLOR_THEME_FILE_NAME); | ||
} | ||
reader = new BufferedReader(new InputStreamReader(input)); | ||
} | ||
else { | ||
try { | ||
reader = new BufferedReader(new FileReader(file)); | ||
} | ||
catch (FileNotFoundException e) { | ||
throw new RuntimeException("Could not find file " + file.getAbsoluteFile()); | ||
} | ||
} | ||
final List<String> lines = reader.lines().toList(); | ||
if (copyResourceToFile) { | ||
final Path path = file.toPath(); | ||
lines.forEach(line -> { | ||
try { | ||
Files.writeString(path, line + System.lineSeparator(), StandardOpenOption.APPEND); | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
COLOR_MAP.putAll(lines.stream() | ||
.filter(l -> !l.startsWith("//")) // Use // for comments | ||
.map(l -> l.split(":")) | ||
.filter(a -> a.length == 2) | ||
.peek(a -> System.out.println(Arrays.toString(a))) | ||
.collect(Collectors.toMap(e -> UIColor.valueOf(e[0]), e -> colorFromString(e[1].strip())))); | ||
System.out.println("Colors: " + COLOR_MAP); | ||
} | ||
|
||
public static Color colorFromString(String color) { | ||
try { | ||
return (Color) Color.class.getField(color).get(null); | ||
} | ||
catch (NullPointerException | NoSuchFieldException | IllegalAccessException | ClassCastException e) { | ||
return Color.getColor(color); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
CORRECT: BLUE | ||
INCORRECT: RED |