Description
I've no expertise in color conversions and maybe I'm just too stupid to handle this corrrectly.
Rationale
I want to introduce the DIRIGERA binding which provides support of color bulbs and color LED strips. I want to use HSBType closeTo
comparison in order to check if a command
is close to the current state
.
- If it's close - don't change device settings
- If not - change device settings
Observation
After manual testing I stumbled over the following values and wrote it into a test
HSBType colorState = HSBType.valueOf("63,1,33");
HSBType compareState = HSBType.valueOf("63,100,0");
System.out.println("Color " + colorState + " Compare " + compareState + " close? "
+ colorState.closeTo(compareState, 0.01));
Output:
Color 63,1,33 Compare 63,100,0 close? true
Hue - fine
Saturation - far away
Brightness - far away
Comparison done with 1% max allowed difference and result is true?
Made some more tests checking each H, S and B value
HSBType colorState = HSBType.valueOf("100,50,100");
for (int i = 0; i < 101; i++) {
HSBType compareState = HSBType.valueOf("100,50," + i);
boolean close = compareState.closeTo(colorState, 0.01);
System.out.println("Color " + colorState + " Compare " + compareState + " close? " + close);
}
Brightness is changed from 0 to 100 with 1% deviation expected.
Result:
Color 100,50,100 Compare 100,50,0 close? false
...
Color 100,50,100 Compare 100,50,53 close? false
Color 100,50,100 Compare 100,50,54 close? true
...
Color 100,50,100 Compare 100,50,100 close? true
Nearly half of the values are close with 1% deviation?
I think you see the pattern. Just play with the values in the loop and see the results:
Changing the Saturation value with allowed difference of 18% nearly all values are closeTo true
Code
HSBType colorState = HSBType.valueOf("100,50,100");
for (int i = 0; i < 101; i++) {
HSBType compareState = HSBType.valueOf("100," + i + ",100");
boolean close = compareState.closeTo(colorState, 0.18);
System.out.println("Color " + colorState + " Compare " + compareState + " close? " + close);
}
Log
Color 100,50,100 Compare 100,0,100 close? false
...
Color 100,50,100 Compare 100,3,100 close? true
...
Color 100,50,100 Compare 100,95,100 close? true
Color 100,50,100 Compare 100,96,100 close? false
...
Color 100,50,100 Compare 100,100,100 close? false
Sidenote
Sorry for the german screenshot. But you can see searching all java files from addons the closeTo
function is only used in test code and not in production code which I can use as reference.