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
.ColorBundle
;
12 import be
.nikiroo
.jvcard
.resources
.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
{
27 static private Object lock
= new Object();
28 static private UiColors instance
= null;
30 private Map
<String
, TextColor
> colorMap
= null;
31 private ColorBundle bundle
;
34 bundle
= new ColorBundle();
35 colorMap
= new HashMap
<String
, TextColor
>();
39 * Return a {@link Theme} following the colours defined in
42 * @return the {@link Theme}
44 static public Theme
getCustomTheme() {
45 // Create a properties-theme with our own custom values for some of it
46 Properties properties
= new Properties();
48 ClassLoader classLoader
= AbstractTextGUI
.class.getClassLoader();
49 InputStream resourceAsStream
= classLoader
50 .getResourceAsStream("default-theme.properties");
51 if (resourceAsStream
== null) {
52 resourceAsStream
= new FileInputStream(
53 "src/main/resources/default-theme.properties");
55 properties
.load(resourceAsStream
);
56 resourceAsStream
.close();
57 } catch (IOException e
) {
61 String fg
= getForegroundColor(ColorOption
.DEFAULT
).toString();
62 String bg
= getBackgroundColor(ColorOption
.DEFAULT
).toString();
63 for (String def
: new String
[] { "com.googlecode.lanterna",
64 "com.googlecode.lanterna.gui2.TextBox",
65 "com.googlecode.lanterna.gui2.AbstractListBox",
66 "com.googlecode.lanterna.gui2.Borders$StandardBorder" }) {
67 properties
.put(def
+ ".foreground", fg
);
68 properties
.put(def
+ ".background", bg
);
71 // no bold on borders prelight:
73 .put("com.googlecode.lanterna.gui2.Borders$StandardBorder.sgr[PRELIGHT]",
77 fg
= getForegroundColor(ColorOption
.LINE_MESSAGE_ANS
).toString();
78 bg
= getBackgroundColor(ColorOption
.LINE_MESSAGE_ANS
).toString();
79 String prop
= "com.googlecode.lanterna.gui2.TextBox";
80 properties
.put(prop
+ ".foreground[ACTIVE]", fg
);
81 properties
.put(prop
+ ".background[ACTIVE]", bg
);
83 PropertiesTheme theme
= new PropertiesTheme(properties
);
88 * Create a new {@link Label} with the colours of the given
89 * {@link ColorOption}.
92 * the {@link ColorOption}
94 * the text of the {@link Label}
96 * @return the new {@link Label}
98 static public Label
createLabel(ColorOption el
, String text
) {
102 Label lbl
= new Label(text
);
108 * Theme a {@link Label} with the colours of the given {@link ColorOption}.
111 * the {@link ColorOption}
115 static public void themeLabel(ColorOption el
, Label lbl
) {
116 lbl
.setForegroundColor(getForegroundColor(el
));
117 lbl
.setBackgroundColor(getBackgroundColor(el
));
121 * Return the background colour of the given element.
124 * the {@link ColorOption}
126 * @return its background colour
128 static public TextColor
getBackgroundColor(ColorOption el
) {
129 if (!getInstance().colorMap
.containsKey(el
.name() + "_BG")) {
132 value
= getInstance().bundle
.getStringX(el
, "BG");
133 } catch (MissingResourceException mre
) {
136 getInstance().colorMap
.put(el
.name() + "_BG",
137 convertToColor(value
, TextColor
.ANSI
.BLACK
));
140 return getInstance().colorMap
.get(el
.name() + "_BG");
144 * Return the foreground colour of the given element.
147 * the {@link ColorOption}
149 * @return its foreground colour
151 static public TextColor
getForegroundColor(ColorOption el
) {
152 if (!getInstance().colorMap
.containsKey(el
.name() + "_FG")) {
155 value
= getInstance().bundle
.getStringX(el
, "FG");
156 } catch (MissingResourceException mre
) {
159 getInstance().colorMap
.put(el
.name() + "_FG",
160 convertToColor(value
, TextColor
.ANSI
.WHITE
));
163 return getInstance().colorMap
.get(el
.name() + "_FG");
167 * Get the (unique) instance of this class.
169 * @return the (unique) instance
171 static private UiColors
getInstance() {
172 synchronized (lock
) {
173 if (instance
== null)
174 instance
= new UiColors();
181 * Convert the given {@link String} value to a {@link TextColor}.
184 * the {@link String} to convert
185 * @param defaultColor
186 * the default {@link TextColor} to return if the conversion
189 * @return the converted colour
191 static private TextColor
convertToColor(String value
, TextColor defaultColor
) {
193 if (value
.startsWith("@")) {
194 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
195 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
196 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
197 return TextColor
.Indexed
.fromRGB(r
, g
, b
);
198 } else if (value
.startsWith("#")) {
199 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
200 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
201 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
202 return new TextColor
.RGB(r
, g
, b
);
203 } else if (value
.replaceAll("[0-9]*", "").length() == 0) {
204 return new TextColor
.Indexed(Integer
.parseInt(value
));
206 return TextColor
.ANSI
.valueOf(value
);
208 } catch (Exception e
) {
209 new Exception("Cannot convert value to colour: " + value
, e
)