]> git.basschouten.com Git - openhab-addons.git/blob
97e7e1559decba8a8062f844ac671f526cf84cff
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.tradfri.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.junit.jupiter.api.Test;
19 import org.openhab.core.library.types.HSBType;
20 import org.openhab.core.library.types.PercentType;
21
22 /**
23  * Tests for {@link TradfriColor}.
24  *
25  * @author Holger Reichert - Initial contribution
26  */
27 @NonNullByDefault
28 public class TradfriColorTest {
29
30     @Test
31     public void testFromCieKnownGood1() {
32         TradfriColor color = new TradfriColor(29577, 12294, 354);
33         assertEquals(29577, (int) color.xyX);
34         assertEquals(12294, (int) color.xyY);
35         assertEquals(254, (int) color.brightness);
36         HSBType hsbType = color.getHSB();
37         assertNotNull(hsbType);
38         assertEquals(321, hsbType.getHue().intValue());
39         assertEquals(100, hsbType.getSaturation().intValue());
40         assertEquals(100, hsbType.getBrightness().intValue());
41     }
42
43     @Test
44     public void testFromCieKnownGood2() {
45         TradfriColor color = new TradfriColor(19983, 37417, 84);
46         assertEquals(19983, (int) color.xyX);
47         assertEquals(37417, (int) color.xyY);
48         assertEquals(84, (int) color.brightness);
49         HSBType hsbType = color.getHSB();
50         assertNotNull(hsbType);
51         assertEquals(115, hsbType.getHue().intValue());
52         assertEquals(77, hsbType.getSaturation().intValue());
53         assertEquals(34, hsbType.getBrightness().intValue());
54     }
55
56     @Test
57     public void testFromCieKnownGood3() {
58         TradfriColor color = new TradfriColor(19983, 37417, 1);
59         assertEquals(19983, (int) color.xyX);
60         assertEquals(37417, (int) color.xyY);
61         assertEquals(1, (int) color.brightness);
62         HSBType hsbType = color.getHSB();
63         assertNotNull(hsbType);
64         assertEquals(115, hsbType.getHue().intValue());
65         assertEquals(77, hsbType.getSaturation().intValue());
66         assertEquals(1, hsbType.getBrightness().intValue());
67     }
68
69     @Test
70     public void testFromCieKnownGood4() {
71         TradfriColor color = new TradfriColor(11413, 31334, 181);
72         assertEquals(11413, (int) color.xyX);
73         assertEquals(31334, (int) color.xyY);
74         assertEquals(181, (int) color.brightness);
75         HSBType hsbType = color.getHSB();
76         assertNotNull(hsbType);
77         assertEquals(158, hsbType.getHue().intValue());
78         assertEquals(100, hsbType.getSaturation().intValue());
79         assertEquals(72, hsbType.getBrightness().intValue());
80     }
81
82     @Test
83     public void testFromHSBTypeKnownGood1() {
84         TradfriColor color = new TradfriColor(HSBType.RED);
85         assertEquals(41947, (int) color.xyX);
86         assertEquals(21625, (int) color.xyY);
87         assertEquals(254, (int) color.brightness);
88         HSBType hsbType = color.getHSB();
89         assertNotNull(hsbType);
90         assertEquals(0, hsbType.getHue().intValue());
91         assertEquals(100, hsbType.getSaturation().intValue());
92         assertEquals(100, hsbType.getBrightness().intValue());
93     }
94
95     @Test
96     public void testFromHSBTypeKnownGood2() {
97         TradfriColor color = new TradfriColor(new HSBType("0,100,1"));
98         assertEquals(41947, (int) color.xyX);
99         assertEquals(21625, (int) color.xyY);
100         assertEquals(2, (int) color.brightness);
101         HSBType hsbType = color.getHSB();
102         assertNotNull(hsbType);
103         assertEquals(0, hsbType.getHue().intValue());
104         assertEquals(100, hsbType.getSaturation().intValue());
105         assertEquals(1, hsbType.getBrightness().intValue());
106     }
107
108     @Test
109     public void testConversionReverse() {
110         // convert from HSBType
111         TradfriColor color = new TradfriColor(HSBType.GREEN);
112         assertEquals(19660, (int) color.xyX);
113         assertEquals(39321, (int) color.xyY);
114         assertEquals(254, (int) color.brightness);
115         HSBType hsbType = color.getHSB();
116         assertNotNull(hsbType);
117         assertEquals(120, hsbType.getHue().intValue());
118         assertEquals(100, hsbType.getSaturation().intValue());
119         assertEquals(100, hsbType.getBrightness().intValue());
120         // convert the result again based on the XY values
121         TradfriColor reverse = new TradfriColor(color.xyX, color.xyY, color.brightness);
122         assertEquals(19660, (int) reverse.xyX);
123         assertEquals(39321, (int) reverse.xyY);
124         assertEquals(254, (int) reverse.brightness);
125         HSBType hsbTypeReverse = color.getHSB();
126         assertNotNull(hsbTypeReverse);
127         assertEquals(120, hsbTypeReverse.getHue().intValue());
128         assertEquals(100, hsbTypeReverse.getSaturation().intValue());
129         assertEquals(100, hsbTypeReverse.getBrightness().intValue());
130     }
131
132     @Test
133     public void testFromColorTemperatureMinMiddleMax() {
134         // coldest color temperature -> preset 1
135         TradfriColor colorMin = new TradfriColor(PercentType.ZERO);
136         assertNotNull(colorMin);
137         assertEquals(24933, (int) colorMin.xyX);
138         assertEquals(24691, (int) colorMin.xyY);
139         // middle color temperature -> preset 2
140         TradfriColor colorMiddle = new TradfriColor(new PercentType(50));
141         assertNotNull(colorMiddle);
142         assertEquals(30138, (int) colorMiddle.xyX);
143         assertEquals(26909, (int) colorMiddle.xyY);
144         // warmest color temperature -> preset 3
145         TradfriColor colorMax = new TradfriColor(PercentType.HUNDRED);
146         assertNotNull(colorMax);
147         assertEquals(33137, (int) colorMax.xyX);
148         assertEquals(27211, (int) colorMax.xyY);
149     }
150
151     @Test
152     public void testFromColorTemperatureInbetween() {
153         // 30 percent must be between preset 1 and 2
154         TradfriColor color2 = new TradfriColor(new PercentType(30));
155         assertNotNull(color2);
156         assertEquals(28056, (int) color2.xyX);
157         assertEquals(26022, (int) color2.xyY);
158         // 70 percent must be between preset 2 and 3
159         TradfriColor color3 = new TradfriColor(new PercentType(70));
160         assertNotNull(color3);
161         assertEquals(31338, (int) color3.xyX);
162         assertEquals(27030, (int) color3.xyY);
163     }
164
165     @Test
166     public void testCalculateColorTemperature() {
167         // preset 1 -> coldest -> 0 percent
168         PercentType preset1 = new TradfriColor(24933, 24691, null).getColorTemperature();
169         assertEquals(0, preset1.intValue());
170         // preset 2 -> middle -> 50 percent
171         PercentType preset2 = new TradfriColor(30138, 26909, null).getColorTemperature();
172         assertEquals(50, preset2.intValue());
173         // preset 3 -> warmest -> 100 percent
174         PercentType preset3 = new TradfriColor(33137, 27211, null).getColorTemperature();
175         assertEquals(100, preset3.intValue());
176         // preset 3 -> warmest -> 100 percent
177         PercentType colder = new TradfriColor(22222, 23333, null).getColorTemperature();
178         assertEquals(0, colder.intValue());
179         // preset 3 -> warmest -> 100 percent
180         PercentType temp3 = new TradfriColor(34000, 34000, null).getColorTemperature();
181         assertEquals(100, temp3.intValue());
182         // mixed case 1
183         PercentType mixed1 = new TradfriColor(0, 1000000, null).getColorTemperature();
184         assertEquals(0, mixed1.intValue());
185         // mixed case 1
186         PercentType mixed2 = new TradfriColor(1000000, 0, null).getColorTemperature();
187         assertEquals(100, mixed2.intValue());
188     }
189 }