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 * Return a {@link Theme} following the colours defined in
41 * @return the {@link Theme}
43 static public Theme
getCustomTheme() {
44 // Create a properties-theme with our own custom values for some of it
45 Properties properties
= new Properties();
47 ClassLoader classLoader
= AbstractTextGUI
.class.getClassLoader();
48 InputStream resourceAsStream
= classLoader
49 .getResourceAsStream("default-theme.properties");
50 if (resourceAsStream
== null) {
51 resourceAsStream
= new FileInputStream(
52 "src/main/resources/default-theme.properties");
54 properties
.load(resourceAsStream
);
55 resourceAsStream
.close();
56 } catch (IOException e
) {
60 String fg
= getForegroundColor(ColorOption
.DEFAULT
).toString();
61 String bg
= getBackgroundColor(ColorOption
.DEFAULT
).toString();
62 for (String def
: new String
[] { "com.googlecode.lanterna",
63 "com.googlecode.lanterna.gui2.TextBox",
64 "com.googlecode.lanterna.gui2.AbstractListBox",
65 "com.googlecode.lanterna.gui2.Borders$StandardBorder" }) {
66 properties
.put(def
+ ".foreground", fg
);
67 properties
.put(def
+ ".background", bg
);
70 // no bold on borders prelight:
72 .put("com.googlecode.lanterna.gui2.Borders$StandardBorder.sgr[PRELIGHT]",
76 fg
= getForegroundColor(ColorOption
.LINE_MESSAGE_ANS
).toString();
77 bg
= getBackgroundColor(ColorOption
.LINE_MESSAGE_ANS
).toString();
78 String prop
= "com.googlecode.lanterna.gui2.TextBox";
79 properties
.put(prop
+ ".foreground[ACTIVE]", fg
);
80 properties
.put(prop
+ ".background[ACTIVE]", bg
);
82 PropertiesTheme theme
= new PropertiesTheme(properties
);
87 * Create a new {@link Label} with the colours of the given
88 * {@link ColorOption}.
91 * the {@link ColorOption}
93 * the text of the {@link Label}
95 * @return the new {@link Label}
97 static public Label
createLabel(ColorOption el
, String text
) {
101 Label lbl
= new Label(text
);
107 * Theme a {@link Label} with the colours of the given {@link ColorOption}.
110 * the {@link ColorOption}
114 static public void themeLabel(ColorOption el
, Label lbl
) {
115 lbl
.setForegroundColor(getForegroundColor(el
));
116 lbl
.setBackgroundColor(getBackgroundColor(el
));
120 * Return the background colour of the given element.
123 * the {@link ColorOption}
125 * @return its background colour
127 static public TextColor
getBackgroundColor(ColorOption el
) {
128 if (!getInstance().colorMap
.containsKey(el
.name() + "_BG")) {
131 value
= getInstance().map
.getString(el
.name() + "_BG");
132 } catch (MissingResourceException mre
) {
135 getInstance().colorMap
.put(el
.name() + "_BG",
136 convertToColor(value
, TextColor
.ANSI
.BLACK
));
139 return getInstance().colorMap
.get(el
.name() + "_BG");
143 * Return the foreground colour of the given element.
146 * the {@link ColorOption}
148 * @return its foreground colour
150 static public TextColor
getForegroundColor(ColorOption el
) {
151 if (!getInstance().colorMap
.containsKey(el
.name() + "_FG")) {
154 value
= getInstance().map
.getString(el
.name() + "_FG");
155 } catch (MissingResourceException mre
) {
158 getInstance().colorMap
.put(el
.name() + "_FG",
159 convertToColor(value
, TextColor
.ANSI
.WHITE
));
162 return getInstance().colorMap
.get(el
.name() + "_FG");
166 * Get the (unique) instance of this class.
168 * @return the (unique) instance
170 static private UiColors
getInstance() {
171 synchronized (lock
) {
172 if (instance
== null)
173 instance
= new UiColors();
180 * Convert the given {@link String} value to a {@link TextColor}.
183 * the {@link String} to convert
184 * @param defaultColor
185 * the default {@link TextColor} to return if the conversion
188 * @return the converted colour
190 static private TextColor
convertToColor(String value
, TextColor defaultColor
) {
192 if (value
.startsWith("@")) {
193 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
194 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
195 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
196 return TextColor
.Indexed
.fromRGB(r
, g
, b
);
197 } else if (value
.startsWith("#")) {
198 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
199 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
200 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
201 return new TextColor
.RGB(r
, g
, b
);
202 } else if (value
.replaceAll("[0-9]*", "").length() == 0) {
203 return new TextColor
.Indexed(Integer
.parseInt(value
));
205 return TextColor
.ANSI
.valueOf(value
);
207 } catch (Exception e
) {
208 new Exception("Cannot convert value to colour: " + value
, e
)