mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-25 14:55:55 +01:00
[hue] Improve color setting (#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 <software@whitebear.ch> Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
parent
bce662b6da
commit
d5f0e317a7
@ -123,7 +123,7 @@ public class Resource {
|
|||||||
/**
|
/**
|
||||||
* Check if <code>light</code> or <code>grouped_light</code> resource contains any
|
* Check if <code>light</code> or <code>grouped_light</code> resource contains any
|
||||||
* relevant fields to process according to its type.
|
* relevant fields to process according to its type.
|
||||||
*
|
*
|
||||||
* As an example, {@link #colorTemperature} is relevant for a <code>light</code>
|
* As an example, {@link #colorTemperature} is relevant for a <code>light</code>
|
||||||
* resource because it's needed for updating the color-temperature channels.
|
* resource because it's needed for updating the color-temperature channels.
|
||||||
*
|
*
|
||||||
@ -285,9 +285,7 @@ public class Resource {
|
|||||||
ColorXy color = this.color;
|
ColorXy color = this.color;
|
||||||
if (Objects.nonNull(color)) {
|
if (Objects.nonNull(color)) {
|
||||||
try {
|
try {
|
||||||
Gamut gamut = color.getGamut();
|
HSBType hsb = ColorUtil.xyToHsb(color.getXY());
|
||||||
gamut = Objects.nonNull(gamut) ? gamut : ColorUtil.DEFAULT_GAMUT;
|
|
||||||
HSBType hsb = ColorUtil.xyToHsb(color.getXY(), gamut);
|
|
||||||
OnState on = this.on;
|
OnState on = this.on;
|
||||||
Dimming dimming = this.dimming;
|
Dimming dimming = this.dimming;
|
||||||
double brightness = Objects.nonNull(on) && !on.isOn() ? 0
|
double brightness = Objects.nonNull(on) && !on.isOn() ? 0
|
||||||
@ -354,9 +352,7 @@ public class Resource {
|
|||||||
ColorXy color = this.color;
|
ColorXy color = this.color;
|
||||||
if (Objects.nonNull(color)) {
|
if (Objects.nonNull(color)) {
|
||||||
try {
|
try {
|
||||||
Gamut gamut = color.getGamut();
|
HSBType hsb = ColorUtil.xyToHsb(color.getXY());
|
||||||
gamut = Objects.nonNull(gamut) ? gamut : ColorUtil.DEFAULT_GAMUT;
|
|
||||||
HSBType hsb = ColorUtil.xyToHsb(color.getXY(), gamut);
|
|
||||||
return new HSBType(hsb.getHue(), hsb.getSaturation(), PercentType.HUNDRED);
|
return new HSBType(hsb.getHue(), hsb.getSaturation(), PercentType.HUNDRED);
|
||||||
} catch (DTOPresentButEmptyException e) {
|
} catch (DTOPresentButEmptyException e) {
|
||||||
return UnDefType.UNDEF; // indicates the DTO is present but its inner fields are missing
|
return UnDefType.UNDEF; // indicates the DTO is present but its inner fields are missing
|
||||||
|
@ -72,10 +72,10 @@ public class Setters {
|
|||||||
* @return the target resource.
|
* @return the target resource.
|
||||||
*/
|
*/
|
||||||
public static Resource setAlert(Resource target, Command command, @Nullable Resource source) {
|
public static Resource setAlert(Resource target, Command command, @Nullable Resource source) {
|
||||||
if ((command instanceof StringType) && Objects.nonNull(source)) {
|
if ((command instanceof StringType alert) && Objects.nonNull(source)) {
|
||||||
Alerts otherAlert = source.getAlerts();
|
Alerts otherAlert = source.getAlerts();
|
||||||
if (Objects.nonNull(otherAlert)) {
|
if (Objects.nonNull(otherAlert)) {
|
||||||
ActionType actionType = ActionType.of(((StringType) command).toString());
|
ActionType actionType = ActionType.of(alert.toString());
|
||||||
if (otherAlert.getActionValues().contains(actionType)) {
|
if (otherAlert.getActionValues().contains(actionType)) {
|
||||||
target.setAlerts(new Alerts().setAction(actionType));
|
target.setAlerts(new Alerts().setAction(actionType));
|
||||||
}
|
}
|
||||||
@ -98,8 +98,7 @@ public class Setters {
|
|||||||
*/
|
*/
|
||||||
public static Resource setColorTemperatureAbsolute(Resource target, Command command, @Nullable Resource source) {
|
public static Resource setColorTemperatureAbsolute(Resource target, Command command, @Nullable Resource source) {
|
||||||
QuantityType<?> mirek;
|
QuantityType<?> mirek;
|
||||||
if (command instanceof QuantityType<?>) {
|
if (command instanceof QuantityType<?> quantity) {
|
||||||
QuantityType<?> quantity = (QuantityType<?>) command;
|
|
||||||
Unit<?> unit = quantity.getUnit();
|
Unit<?> unit = quantity.getUnit();
|
||||||
if (Units.KELVIN.equals(unit)) {
|
if (Units.KELVIN.equals(unit)) {
|
||||||
mirek = quantity.toInvertibleUnit(Units.MIRED);
|
mirek = quantity.toInvertibleUnit(Units.MIRED);
|
||||||
@ -109,9 +108,8 @@ public class Setters {
|
|||||||
QuantityType<?> kelvin = quantity.toInvertibleUnit(Units.KELVIN);
|
QuantityType<?> kelvin = quantity.toInvertibleUnit(Units.KELVIN);
|
||||||
mirek = Objects.nonNull(kelvin) ? kelvin.toInvertibleUnit(Units.MIRED) : null;
|
mirek = Objects.nonNull(kelvin) ? kelvin.toInvertibleUnit(Units.MIRED) : null;
|
||||||
}
|
}
|
||||||
} else if (command instanceof DecimalType) {
|
} else if (command instanceof DecimalType decimal) {
|
||||||
mirek = QuantityType.valueOf(((DecimalType) command).doubleValue(), Units.KELVIN)
|
mirek = QuantityType.valueOf(decimal.doubleValue(), Units.KELVIN).toInvertibleUnit(Units.MIRED);
|
||||||
.toInvertibleUnit(Units.MIRED);
|
|
||||||
} else {
|
} else {
|
||||||
mirek = null;
|
mirek = null;
|
||||||
}
|
}
|
||||||
@ -141,7 +139,7 @@ public class Setters {
|
|||||||
* @return the target resource.
|
* @return the target resource.
|
||||||
*/
|
*/
|
||||||
public static Resource setColorTemperaturePercent(Resource target, Command command, @Nullable Resource source) {
|
public static Resource setColorTemperaturePercent(Resource target, Command command, @Nullable Resource source) {
|
||||||
if (command instanceof PercentType) {
|
if (command instanceof PercentType mirek) {
|
||||||
MirekSchema schema = target.getMirekSchema();
|
MirekSchema schema = target.getMirekSchema();
|
||||||
schema = Objects.nonNull(schema) ? schema : Objects.nonNull(source) ? source.getMirekSchema() : null;
|
schema = Objects.nonNull(schema) ? schema : Objects.nonNull(source) ? source.getMirekSchema() : null;
|
||||||
schema = Objects.nonNull(schema) ? schema : MirekSchema.DEFAULT_SCHEMA;
|
schema = Objects.nonNull(schema) ? schema : MirekSchema.DEFAULT_SCHEMA;
|
||||||
@ -149,7 +147,7 @@ public class Setters {
|
|||||||
colorTemperature = Objects.nonNull(colorTemperature) ? colorTemperature : new ColorTemperature();
|
colorTemperature = Objects.nonNull(colorTemperature) ? colorTemperature : new ColorTemperature();
|
||||||
double min = schema.getMirekMinimum();
|
double min = schema.getMirekMinimum();
|
||||||
double max = schema.getMirekMaximum();
|
double max = schema.getMirekMaximum();
|
||||||
double val = min + ((max - min) * ((PercentType) command).doubleValue() / 100f);
|
double val = min + ((max - min) * mirek.doubleValue() / 100f);
|
||||||
target.setColorTemperature(colorTemperature.setMirek(val));
|
target.setColorTemperature(colorTemperature.setMirek(val));
|
||||||
}
|
}
|
||||||
return target;
|
return target;
|
||||||
@ -168,13 +166,10 @@ public class Setters {
|
|||||||
* @return the target resource.
|
* @return the target resource.
|
||||||
*/
|
*/
|
||||||
public static Resource setColorXy(Resource target, Command command, @Nullable Resource source) {
|
public static Resource setColorXy(Resource target, Command command, @Nullable Resource source) {
|
||||||
if (command instanceof HSBType) {
|
if (command instanceof HSBType hsb) {
|
||||||
Gamut gamut = target.getGamut();
|
hsb = new HSBType(hsb.getHue(), hsb.getSaturation(), PercentType.HUNDRED);
|
||||||
gamut = Objects.nonNull(gamut) ? gamut : Objects.nonNull(source) ? source.getGamut() : null;
|
|
||||||
gamut = Objects.nonNull(gamut) ? gamut : ColorUtil.DEFAULT_GAMUT;
|
|
||||||
HSBType hsb = (HSBType) command;
|
|
||||||
ColorXy color = target.getColorXy();
|
ColorXy color = target.getColorXy();
|
||||||
target.setColorXy((Objects.nonNull(color) ? color : new ColorXy()).setXY(ColorUtil.hsbToXY(hsb, gamut)));
|
target.setColorXy((Objects.nonNull(color) ? color : new ColorXy()).setXY(ColorUtil.hsbToXY(hsb)));
|
||||||
}
|
}
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
@ -191,11 +186,10 @@ public class Setters {
|
|||||||
* @return the target resource.
|
* @return the target resource.
|
||||||
*/
|
*/
|
||||||
public static Resource setDimming(Resource target, Command command, @Nullable Resource source) {
|
public static Resource setDimming(Resource target, Command command, @Nullable Resource source) {
|
||||||
if (command instanceof PercentType) {
|
if (command instanceof PercentType brightness) {
|
||||||
Double min = target.getMinimumDimmingLevel();
|
Double min = target.getMinimumDimmingLevel();
|
||||||
min = Objects.nonNull(min) ? min : Objects.nonNull(source) ? source.getMinimumDimmingLevel() : null;
|
min = Objects.nonNull(min) ? min : Objects.nonNull(source) ? source.getMinimumDimmingLevel() : null;
|
||||||
min = Objects.nonNull(min) ? min : Dimming.DEFAULT_MINIMUM_DIMMIMG_LEVEL;
|
min = Objects.nonNull(min) ? min : Dimming.DEFAULT_MINIMUM_DIMMIMG_LEVEL;
|
||||||
PercentType brightness = (PercentType) command;
|
|
||||||
if (brightness.doubleValue() < min.doubleValue()) {
|
if (brightness.doubleValue() < min.doubleValue()) {
|
||||||
brightness = new PercentType(new BigDecimal(min, Resource.PERCENT_MATH_CONTEXT));
|
brightness = new PercentType(new BigDecimal(min, Resource.PERCENT_MATH_CONTEXT));
|
||||||
}
|
}
|
||||||
@ -219,8 +213,8 @@ public class Setters {
|
|||||||
* @return the target resource.
|
* @return the target resource.
|
||||||
*/
|
*/
|
||||||
public static Resource setEffect(Resource target, Command command, @Nullable Resource source) {
|
public static Resource setEffect(Resource target, Command command, @Nullable Resource source) {
|
||||||
if ((command instanceof StringType) && Objects.nonNull(source)) {
|
if ((command instanceof StringType effect) && Objects.nonNull(source)) {
|
||||||
EffectType commandEffectType = EffectType.of(((StringType) command).toString());
|
EffectType commandEffectType = EffectType.of(effect.toString());
|
||||||
Effects sourceFixedEffects = source.getFixedEffects();
|
Effects sourceFixedEffects = source.getFixedEffects();
|
||||||
if (Objects.nonNull(sourceFixedEffects) && sourceFixedEffects.allows(commandEffectType)) {
|
if (Objects.nonNull(sourceFixedEffects) && sourceFixedEffects.allows(commandEffectType)) {
|
||||||
target.setFixedEffects(new Effects().setEffect(commandEffectType));
|
target.setFixedEffects(new Effects().setEffect(commandEffectType));
|
||||||
|
Loading…
Reference in New Issue
Block a user