forked from eclipse-archived/smarthome
/
HSBTypeTest.java
129 lines (110 loc) · 4.76 KB
/
HSBTypeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.smarthome.core.library.types;
import static org.junit.Assert.*;
import java.math.BigDecimal;
import org.junit.Test;
/**
*
* @author Chris Jackson - added fromRGB() test
* @author Stefan Triller - more tests for type conversions
*
*/
public class HSBTypeTest {
@Test
public void testEquals() {
HSBType hsb1 = new HSBType("53,86,1");
HSBType hsb2 = new HSBType("53,86,1");
assertTrue(hsb1.equals(hsb2));
hsb1 = new HSBType("0,0,0");
hsb2 = new HSBType("0,0,0");
assertTrue(hsb1.equals(hsb2));
}
@Test
public void testHsbToRgbConversion() {
compareHsbToRgbValues("0,100,100", 255, 0, 0); // red
compareHsbToRgbValues("360,100,100", 255, 0, 0); // red
compareHsbToRgbValues("0,0,0", 0, 0, 0); // black
compareHsbToRgbValues("0,0,100", 255, 255, 255); // white
compareHsbToRgbValues("120,100,100", 0, 255, 0); // green
compareHsbToRgbValues("240,100,100", 0, 0, 255); // blue
compareHsbToRgbValues("229,37,62", 99, 110, 158); // blueish
compareHsbToRgbValues("316,69,47", 119, 37, 97); // purple
compareHsbToRgbValues("60,60,60", 153, 153, 61); // green
compareHsbToRgbValues("300,100,40", 102, 0, 102);
}
private int convertPercentToByte(PercentType percent) {
return percent.value.multiply(BigDecimal.valueOf(255))
.divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).intValue();
}
private void compareHsbToRgbValues(String hsbValues, int red, int green, int blue) {
HSBType hsb = new HSBType(hsbValues);
assertEquals(red, convertPercentToByte(hsb.getRed()));
assertEquals(green, convertPercentToByte(hsb.getGreen()));
assertEquals(blue, convertPercentToByte(hsb.getBlue()));
}
@Test
public void testRgbToHsbConversion() {
compareRgbToHsbValues("0,100,100", 255, 0, 0); // red
compareRgbToHsbValues("0,0,0", 0, 0, 0); // black
compareRgbToHsbValues("0,0,100", 255, 255, 255); // white
compareRgbToHsbValues("120,100,100", 0, 255, 0); // green
compareRgbToHsbValues("240,100,100", 0, 0, 255); // blue
compareRgbToHsbValues("60,60,60", 153, 153, 61); // green
compareRgbToHsbValues("300,100,40", 102, 0, 102);
compareRgbToHsbValues("228,37,61", 99, 110, 158); // blueish
compareRgbToHsbValues("316,68,46", 119, 37, 97); // purple
}
private void compareRgbToHsbValues(String hsbValues, int red, int green, int blue) {
HSBType hsb = new HSBType(hsbValues);
HSBType hsbRgb = HSBType.fromRGB(red, green, blue);
assertEquals(hsb.getHue(), hsbRgb.getHue());
assertEquals(hsb.getSaturation(), hsbRgb.getSaturation());
assertEquals(hsb.getBrightness(), hsbRgb.getBrightness());
}
@Test
public void testConversionToOnOffType() {
assertEquals(OnOffType.ON, new HSBType("100,100,100").as(OnOffType.class));
assertEquals(OnOffType.ON, new HSBType("100,100,1").as(OnOffType.class));
assertEquals(OnOffType.OFF, new HSBType("100,100,0").as(OnOffType.class));
}
@Test
public void testConversionToDecimalType() {
assertEquals(new DecimalType("1.0"), new HSBType("100,100,100").as(DecimalType.class));
assertEquals(new DecimalType("0.01"), new HSBType("100,100,1").as(DecimalType.class));
assertEquals(DecimalType.ZERO, new HSBType("100,100,0").as(DecimalType.class));
}
@Test
public void testConversionToPercentType() {
assertEquals(PercentType.HUNDRED, new HSBType("100,100,100").as(PercentType.class));
assertEquals(new PercentType("1"), new HSBType("100,100,1").as(PercentType.class));
assertEquals(PercentType.ZERO, new HSBType("100,100,0").as(PercentType.class));
}
@Test
public void testConversionToPointType() {
// should not be possible => null
assertEquals(null, new HSBType("100,100,100").as(PointType.class));
}
@Test
public void testConversionToXY() {
HSBType hsb = new HSBType("220,90,50");
PercentType[] xy = hsb.toXY();
assertEquals(new PercentType("16.969364"), xy[0]);
assertEquals(new PercentType("12.379659"), xy[1]);
}
@Test
public void testCreateFromXY() {
HSBType hsb = HSBType.fromXY(5f, 3f);
assertEquals(new HSBType("11,100,100"), hsb);
}
}