1 package be
.nikiroo
.jvcard
.tui
;
3 import java
.util
.HashMap
;
5 import java
.util
.MissingResourceException
;
7 import be
.nikiroo
.jvcard
.resources
.bundles
.ColorBundle
;
8 import be
.nikiroo
.jvcard
.resources
.enums
.ColorOption
;
10 import com
.googlecode
.lanterna
.TextColor
;
11 import com
.googlecode
.lanterna
.gui2
.Label
;
14 * All colour information must come from here.
19 public class UiColors
extends ColorBundle
{
20 static private Object lock
= new Object();
21 static private UiColors instance
= null;
23 private Map
<String
, TextColor
> colorMap
= null;
27 colorMap
= new HashMap
<String
, TextColor
>();
31 * Create a new {@link Label} with the colours of the given {@link ColorOption}.
34 * the {@link ColorOption}
36 * the text of the {@link Label}
38 * @return the new {@link Label}
40 static public Label
createLabel(ColorOption el
, String text
) {
44 Label lbl
= new Label(text
);
50 * Theme a {@link Label} with the colours of the given {@link ColorOption}.
53 * the {@link ColorOption}
57 static public void themeLabel(ColorOption el
, Label lbl
) {
58 lbl
.setForegroundColor(getForegroundColor(el
));
59 lbl
.setBackgroundColor(getBackgroundColor(el
));
63 * Return the background colour of the given element.
66 * the {@link ColorOption}
68 * @return its background colour
70 static public TextColor
getBackgroundColor(ColorOption el
) {
71 if (!getInstance().colorMap
.containsKey(el
.name() + "_BG")) {
74 value
= getInstance().map
.getString(el
.name() + "_BG");
75 } catch (MissingResourceException mre
) {
78 getInstance().colorMap
.put(el
.name() + "_BG",
79 convertToColor(value
, TextColor
.ANSI
.BLACK
));
82 return getInstance().colorMap
.get(el
.name() + "_BG");
86 * Return the foreground colour of the given element.
89 * the {@link ColorOption}
91 * @return its foreground colour
93 static public TextColor
getForegroundColor(ColorOption el
) {
94 if (!getInstance().colorMap
.containsKey(el
.name() + "_FG")) {
97 value
= getInstance().map
.getString(el
.name() + "_FG");
98 } catch (MissingResourceException mre
) {
101 getInstance().colorMap
.put(el
.name() + "_FG",
102 convertToColor(value
, TextColor
.ANSI
.WHITE
));
105 return getInstance().colorMap
.get(el
.name() + "_FG");
109 * Get the (unique) instance of this class.
111 * @return the (unique) instance
113 static private UiColors
getInstance() {
114 synchronized (lock
) {
115 if (instance
== null)
116 instance
= new UiColors();
123 * Convert the given {@link String} value to a {@link TextColor}.
126 * the {@link String} to convert
127 * @param defaultColor
128 * the default {@link TextColor} to return if the conversion
131 * @return the converted colour
133 static private TextColor
convertToColor(String value
, TextColor defaultColor
) {
135 if (value
.startsWith("@")) {
136 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
137 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
138 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
139 return TextColor
.Indexed
.fromRGB(r
, g
, b
);
140 } else if (value
.startsWith("#")) {
141 int r
= Integer
.parseInt(value
.substring(1, 3), 16);
142 int g
= Integer
.parseInt(value
.substring(3, 5), 16);
143 int b
= Integer
.parseInt(value
.substring(5, 7), 16);
144 return new TextColor
.RGB(r
, g
, b
);
145 } else if (value
.replaceAll("[0-9]*", "").length() == 0) {
146 return new TextColor
.Indexed(Integer
.parseInt(value
));
148 return TextColor
.ANSI
.valueOf(value
);
150 } catch (Exception e
) {
151 new Exception("Cannot convert value to colour: " + value
, e
)