]> git.basschouten.com Git - openhab-addons.git/blob
b20d30b85989472a70d219dd519f2d491745beff
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.wlanthermo.internal.api.nano;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 /**
19  * The {@link UtilNano} class provides conversion functions for the WlanThermo Nano
20  *
21  * @author Christian Schlipp - Initial contribution
22  */
23 public class UtilNano {
24
25     private static final Map<String, String> COLOR_MAPPINGS = createColorMap();
26     private static final String DEFAULT_HEX = "#ffffff";
27     private static final String DEFAULT_COLORNAME = "niagara";
28
29     private UtilNano() {
30         // hidden
31     }
32
33     private static Map<String, String> createColorMap() {
34         HashMap<String, String> map = new HashMap<>();
35         map.put("niagara", "#5587A2");
36         map.put("rosa", "#FFAEC9");
37         map.put("lapis blue", "#0C4C88");
38         map.put("orange", "#EF562D");
39         map.put("lila", "#A349A4");
40         map.put("red", "#ED1C24");
41         map.put("green", "#22B14C");
42         map.put("gold", "#FFC100");
43         map.put("kale", "#5C7148");
44         map.put("brown", "#804000");
45         return map;
46     }
47
48     /**
49      * Convert WlanThermo Color Name to Hex
50      * 
51      * @param colorName the WlanThermo color name
52      * @return The color as Hex String
53      */
54     public static String toHex(String colorName) {
55         return COLOR_MAPPINGS.getOrDefault(colorName, DEFAULT_HEX);
56     }
57
58     public static String toColorName(String colorHex) {
59         String colorName = null;
60         if (!colorHex.startsWith("#")) {
61             colorHex = "#" + colorHex;
62         }
63         for (Map.Entry<String, String> entry : COLOR_MAPPINGS.entrySet()) {
64             if (entry.getValue().equalsIgnoreCase(colorHex)) {
65                 colorName = entry.getKey();
66             }
67         }
68         if (colorName == null) {
69             colorName = DEFAULT_COLORNAME;
70         }
71         return colorName;
72     }
73 }