1 package be
.nikiroo
.jvcard
.tui
;
3 import java
.io
.FileInputStream
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.util
.HashMap
;
8 import java
.util
.MissingResourceException
;
9 import java
.util
.Properties
;
11 import be
.nikiroo
.jvcard
.resources
.bundles
.ColorBundle
;
12 import be
.nikiroo
.jvcard
.resources
.enums
.ColorOption
;
14 import com
.googlecode
.lanterna
.TextColor
;
15 import com
.googlecode
.lanterna
.graphics
.PropertiesTheme
;
16 import com
.googlecode
.lanterna
.graphics
.Theme
;
17 import com
.googlecode
.lanterna
.gui2
.AbstractTextGUI
;
18 import com
.googlecode
.lanterna
.gui2
.Label
;
21 * All colour information must come from here.
26 public class UiColors
extends ColorBundle
{
27 static private Object lock
= new Object();
28 static private UiColors instance
= null;
30 private Map
<String
, TextColor
> colorMap
= null;
34 colorMap
= new HashMap
<String
, TextColor
>();
38 * Create a new {@link Label} with the colours of the given
39 * {@link ColorOption}.
42 * the {@link ColorOption}
44 * the text of the {@link Label}
46 * @return the new {@link Label}
48 static public Label
createLabel(ColorOption el
, String text
) {
52 Label lbl
= new Label(text
);
58 * Theme a {@link Label} with the colours of the given {@link ColorOption}.
61 * the {@link ColorOption}
65 static public void themeLabel(ColorOption el
, Label lbl
) {
66 lbl
.setForegroundColor(getForegroundColor(el
));
67 lbl
.setBackgroundColor(getBackgroundColor(el
));
71 * Return the background colour of the given element.
74 * the {@link ColorOption}
76 * @return its background colour
78 static public TextColor
getBackgroundColor(ColorOption el
) {
79 if (!getInstance().colorMap
.containsKey(el
.name() + "_BG")) {
82 value
= getInstance().map
.getString(el
.name() + "_BG");
83 } catch (MissingResourceException mre
) {
86 getInstance().colorMap
.put(el
.name() + "_BG",
87 convertToColor(value
, TextColor
.ANSI
.BLACK
));
90 return getInstance().colorMap
.get(el
.name() + "_BG");
94 * Return the foreground colour of the given element.
97 * the {@link ColorOption}
99 * @return its foreground colour
101 static public TextColor
getForegroundColor(ColorOption el
) {
102 if (!getInstance().colorMap
.containsKey(el
.name() + "_FG")) {
105 value
= getInstance().map
.getString(el
.name() + "_FG");
106 } catch (MissingResourceException mre
) {
109 getInstance().colorMap
.put(el
.name() + "_FG",
110 convertToColor(value
, TextColor
.ANSI
.WHITE
));
113 return getInstance().colorMap
.get(el
.name() + "_FG");
117 * Return a {@link Theme} following the colours defined in
118 * display.properties.
120 * @return the {@link Theme}
122 static Theme
getCustomTheme() {
123 // Create a properties-theme with our own custom values for some of it
124 Properties properties
= new Properties();
126 ClassLoader classLoader
= AbstractTextGUI
.class.getClassLoader();
127 InputStream resourceAsStream
= classLoader
128 .getResourceAsStream("default-theme.properties");
129 if (resourceAsStream
== null) {
130 resourceAsStream
= new FileInputStream(
131 "src/main/resources/default-theme.properties");
133 properties
.load(resourceAsStream
);
134 resourceAsStream
.close();
135 } catch (IOException e
) {
137 properties
.put("com.googlecode.lanterna.background", "black");
138 PropertiesTheme theme
= new PropertiesTheme(properties
);
144 * Get the (unique) instance of this class.
146 * @return the (unique) instance
148 static private UiColors
getInstance() {
149 synchronized (lock
) {
150 if (instance
== null)
151 instance
= new UiColors();
158 * Convert the given {@link String} value to a {@link TextColor}.
161 * the {@link String} to convert
162 * @param defaultColor
163 * the default {@link TextColor} to return if the conversion
166 * @return the converted colour
168 static private TextColor
convertToColor(String value
, TextColor defaultColor
) {
170 if (value
.startsWith("@")) {
171 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
172 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
173 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
174 return TextColor
.Indexed
.fromRGB(r
, g
, b
);
175 } else if (value
.startsWith("#")) {
176 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
177 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
178 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
179 return new TextColor
.RGB(r
, g
, b
);
180 } else if (value
.replaceAll("[0-9]*", "").length() == 0) {
181 return new TextColor
.Indexed(Integer
.parseInt(value
));
183 return TextColor
.ANSI
.valueOf(value
);
185 } catch (Exception e
) {
186 new Exception("Cannot convert value to colour: " + value
, e
)