2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tradfri.internal;
15 import static org.junit.jupiter.api.Assertions.*;
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;
23 * Tests for {@link TradfriColor}.
25 * @author Holger Reichert - Initial contribution
28 public class TradfriColorTest {
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(312, hsbType.getHue().intValue());
39 assertEquals(92, hsbType.getSaturation().intValue());
40 assertEquals(100, hsbType.getBrightness().intValue());
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(93, hsbType.getHue().intValue());
52 assertEquals(65, hsbType.getSaturation().intValue());
53 assertEquals(34, hsbType.getBrightness().intValue());
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(93, hsbType.getHue().intValue());
65 assertEquals(65, hsbType.getSaturation().intValue());
66 assertEquals(1, hsbType.getBrightness().intValue());
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(156, hsbType.getHue().intValue());
78 assertEquals(77, hsbType.getSaturation().intValue());
79 assertEquals(72, hsbType.getBrightness().intValue());
83 public void testFromHSBTypeKnownGood1() {
84 TradfriColor color = new TradfriColor(HSBType.RED);
85 assertEquals(45789, (int) color.xyX);
86 assertEquals(19490, (int) color.xyY);
87 assertEquals(254, (int) color.brightness);
88 HSBType hsbType = color.getHSB();
89 assertNotNull(hsbType);
90 assertEquals(356, hsbType.getHue().intValue());
91 assertEquals(100, hsbType.getSaturation().intValue());
92 assertEquals(100, hsbType.getBrightness().intValue());
96 public void testFromHSBTypeKnownGood2() {
97 TradfriColor color = new TradfriColor(new HSBType("0,100,1"));
98 assertEquals(45789, (int) color.xyX);
99 assertEquals(19490, (int) color.xyY);
100 assertEquals(2, (int) color.brightness);
101 HSBType hsbType = color.getHSB();
102 assertNotNull(hsbType);
103 assertEquals(356, hsbType.getHue().intValue());
104 assertEquals(100, hsbType.getSaturation().intValue());
105 assertEquals(1, hsbType.getBrightness().intValue());
109 public void testConversionReverse() {
110 // convert from HSBType
111 TradfriColor color = new TradfriColor(HSBType.GREEN);
112 assertEquals(11298, (int) color.xyX);
113 assertEquals(48935, (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(11298, (int) reverse.xyX);
123 assertEquals(48935, (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());
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);
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);
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());
183 PercentType mixed1 = new TradfriColor(0, 1000000, null).getColorTemperature();
184 assertEquals(0, mixed1.intValue());
186 PercentType mixed2 = new TradfriColor(1000000, 0, null).getColorTemperature();
187 assertEquals(100, mixed2.intValue());