Skip to content

Commit 8687fec

Browse files
authored
[hue] Improve color setting (openhab#16436)
* [hue] fix xy conversion when B is 0 * [hue] remove gamut correction; let Hue bridge do it instead * [hue] fix gamma round trips; modernize instanceof syntax Signed-off-by: Andrew Fiddian-Green <[email protected]>
1 parent 9a178cf commit 8687fec

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/api/dto/clip2/Resource.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public Resource(@Nullable ResourceType resourceType) {
123123
/**
124124
* Check if <code>light</code> or <code>grouped_light</code> resource contains any
125125
* relevant fields to process according to its type.
126-
*
126+
*
127127
* As an example, {@link #colorTemperature} is relevant for a <code>light</code>
128128
* resource because it's needed for updating the color-temperature channels.
129129
*
@@ -285,9 +285,7 @@ public State getColorState() {
285285
ColorXy color = this.color;
286286
if (Objects.nonNull(color)) {
287287
try {
288-
Gamut gamut = color.getGamut();
289-
gamut = Objects.nonNull(gamut) ? gamut : ColorUtil.DEFAULT_GAMUT;
290-
HSBType hsb = ColorUtil.xyToHsb(color.getXY(), gamut);
288+
HSBType hsb = ColorUtil.xyToHsb(color.getXY());
291289
OnState on = this.on;
292290
Dimming dimming = this.dimming;
293291
double brightness = Objects.nonNull(on) && !on.isOn() ? 0
@@ -354,9 +352,7 @@ public State getColorXyState() {
354352
ColorXy color = this.color;
355353
if (Objects.nonNull(color)) {
356354
try {
357-
Gamut gamut = color.getGamut();
358-
gamut = Objects.nonNull(gamut) ? gamut : ColorUtil.DEFAULT_GAMUT;
359-
HSBType hsb = ColorUtil.xyToHsb(color.getXY(), gamut);
355+
HSBType hsb = ColorUtil.xyToHsb(color.getXY());
360356
return new HSBType(hsb.getHue(), hsb.getSaturation(), PercentType.HUNDRED);
361357
} catch (DTOPresentButEmptyException e) {
362358
return UnDefType.UNDEF; // indicates the DTO is present but its inner fields are missing

bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/api/dto/clip2/helper/Setters.java

+13-19
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ public class Setters {
7272
* @return the target resource.
7373
*/
7474
public static Resource setAlert(Resource target, Command command, @Nullable Resource source) {
75-
if ((command instanceof StringType) && Objects.nonNull(source)) {
75+
if ((command instanceof StringType alert) && Objects.nonNull(source)) {
7676
Alerts otherAlert = source.getAlerts();
7777
if (Objects.nonNull(otherAlert)) {
78-
ActionType actionType = ActionType.of(((StringType) command).toString());
78+
ActionType actionType = ActionType.of(alert.toString());
7979
if (otherAlert.getActionValues().contains(actionType)) {
8080
target.setAlerts(new Alerts().setAction(actionType));
8181
}
@@ -98,8 +98,7 @@ public static Resource setAlert(Resource target, Command command, @Nullable Reso
9898
*/
9999
public static Resource setColorTemperatureAbsolute(Resource target, Command command, @Nullable Resource source) {
100100
QuantityType<?> mirek;
101-
if (command instanceof QuantityType<?>) {
102-
QuantityType<?> quantity = (QuantityType<?>) command;
101+
if (command instanceof QuantityType<?> quantity) {
103102
Unit<?> unit = quantity.getUnit();
104103
if (Units.KELVIN.equals(unit)) {
105104
mirek = quantity.toInvertibleUnit(Units.MIRED);
@@ -109,9 +108,8 @@ public static Resource setColorTemperatureAbsolute(Resource target, Command comm
109108
QuantityType<?> kelvin = quantity.toInvertibleUnit(Units.KELVIN);
110109
mirek = Objects.nonNull(kelvin) ? kelvin.toInvertibleUnit(Units.MIRED) : null;
111110
}
112-
} else if (command instanceof DecimalType) {
113-
mirek = QuantityType.valueOf(((DecimalType) command).doubleValue(), Units.KELVIN)
114-
.toInvertibleUnit(Units.MIRED);
111+
} else if (command instanceof DecimalType decimal) {
112+
mirek = QuantityType.valueOf(decimal.doubleValue(), Units.KELVIN).toInvertibleUnit(Units.MIRED);
115113
} else {
116114
mirek = null;
117115
}
@@ -141,15 +139,15 @@ public static Resource setColorTemperatureAbsolute(Resource target, Command comm
141139
* @return the target resource.
142140
*/
143141
public static Resource setColorTemperaturePercent(Resource target, Command command, @Nullable Resource source) {
144-
if (command instanceof PercentType) {
142+
if (command instanceof PercentType mirek) {
145143
MirekSchema schema = target.getMirekSchema();
146144
schema = Objects.nonNull(schema) ? schema : Objects.nonNull(source) ? source.getMirekSchema() : null;
147145
schema = Objects.nonNull(schema) ? schema : MirekSchema.DEFAULT_SCHEMA;
148146
ColorTemperature colorTemperature = target.getColorTemperature();
149147
colorTemperature = Objects.nonNull(colorTemperature) ? colorTemperature : new ColorTemperature();
150148
double min = schema.getMirekMinimum();
151149
double max = schema.getMirekMaximum();
152-
double val = min + ((max - min) * ((PercentType) command).doubleValue() / 100f);
150+
double val = min + ((max - min) * mirek.doubleValue() / 100f);
153151
target.setColorTemperature(colorTemperature.setMirek(val));
154152
}
155153
return target;
@@ -168,13 +166,10 @@ public static Resource setColorTemperaturePercent(Resource target, Command comma
168166
* @return the target resource.
169167
*/
170168
public static Resource setColorXy(Resource target, Command command, @Nullable Resource source) {
171-
if (command instanceof HSBType) {
172-
Gamut gamut = target.getGamut();
173-
gamut = Objects.nonNull(gamut) ? gamut : Objects.nonNull(source) ? source.getGamut() : null;
174-
gamut = Objects.nonNull(gamut) ? gamut : ColorUtil.DEFAULT_GAMUT;
175-
HSBType hsb = (HSBType) command;
169+
if (command instanceof HSBType hsb) {
170+
hsb = new HSBType(hsb.getHue(), hsb.getSaturation(), PercentType.HUNDRED);
176171
ColorXy color = target.getColorXy();
177-
target.setColorXy((Objects.nonNull(color) ? color : new ColorXy()).setXY(ColorUtil.hsbToXY(hsb, gamut)));
172+
target.setColorXy((Objects.nonNull(color) ? color : new ColorXy()).setXY(ColorUtil.hsbToXY(hsb)));
178173
}
179174
return target;
180175
}
@@ -191,11 +186,10 @@ public static Resource setColorXy(Resource target, Command command, @Nullable Re
191186
* @return the target resource.
192187
*/
193188
public static Resource setDimming(Resource target, Command command, @Nullable Resource source) {
194-
if (command instanceof PercentType) {
189+
if (command instanceof PercentType brightness) {
195190
Double min = target.getMinimumDimmingLevel();
196191
min = Objects.nonNull(min) ? min : Objects.nonNull(source) ? source.getMinimumDimmingLevel() : null;
197192
min = Objects.nonNull(min) ? min : Dimming.DEFAULT_MINIMUM_DIMMIMG_LEVEL;
198-
PercentType brightness = (PercentType) command;
199193
if (brightness.doubleValue() < min.doubleValue()) {
200194
brightness = new PercentType(new BigDecimal(min, Resource.PERCENT_MATH_CONTEXT));
201195
}
@@ -219,8 +213,8 @@ public static Resource setDimming(Resource target, Command command, @Nullable Re
219213
* @return the target resource.
220214
*/
221215
public static Resource setEffect(Resource target, Command command, @Nullable Resource source) {
222-
if ((command instanceof StringType) && Objects.nonNull(source)) {
223-
EffectType commandEffectType = EffectType.of(((StringType) command).toString());
216+
if ((command instanceof StringType effect) && Objects.nonNull(source)) {
217+
EffectType commandEffectType = EffectType.of(effect.toString());
224218
Effects sourceFixedEffects = source.getFixedEffects();
225219
if (Objects.nonNull(sourceFixedEffects) && sourceFixedEffects.allows(commandEffectType)) {
226220
target.setFixedEffects(new Effects().setEffect(commandEffectType));

0 commit comments

Comments
 (0)