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.mqtt.generic.values;
15 import java.math.BigDecimal;
16 import java.util.List;
17 import java.util.Locale;
19 import javax.ws.rs.NotSupportedException;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
24 import org.openhab.core.library.CoreItemFactory;
25 import org.openhab.core.library.types.HSBType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.UnDefType;
31 import org.openhab.core.util.ColorUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Implements a color value.
39 * Accepts user updates from a HSBType, OnOffType and StringType.
41 * Accepts MQTT state updates as OnOffType and a
42 * StringType with comma separated HSB ("h,s,b"), RGB ("r,g,b"), CIE xyY ("x,y,Y") and on, off strings.
43 * On, Off strings can be customized.
45 * @author David Graeff - Initial contribution
46 * @author Aitor Iturrioz - Add CIE xyY colors support
49 public class ColorValue extends Value {
50 private static BigDecimal factor = new BigDecimal("2.55"); // string to not lose precision
52 private final Logger logger = LoggerFactory.getLogger(ColorValue.class);
54 private final ColorMode colorMode;
55 private final String onValue;
56 private final String offValue;
57 private final int onBrightness;
60 * Creates a non initialized color value.
62 * @param colorMode The color mode: HSB, RGB or XYY.
63 * @param onValue The ON value string. This will be compared to MQTT messages.
64 * @param offValue The OFF value string. This will be compared to MQTT messages.
65 * @param onBrightness When receiving an ON command, the brightness percentage is set to this value
67 public ColorValue(ColorMode colorMode, @Nullable String onValue, @Nullable String offValue, int onBrightness) {
68 super(CoreItemFactory.COLOR, List.of(OnOffType.class, PercentType.class, StringType.class));
70 if (onBrightness > 100) {
71 throw new IllegalArgumentException("Brightness parameter must be <= 100");
74 this.colorMode = colorMode;
75 this.onValue = onValue == null ? "ON" : onValue;
76 this.offValue = offValue == null ? "OFF" : offValue;
77 this.onBrightness = onBrightness;
81 * Updates the color state.
84 public HSBType parseCommand(Command command) throws IllegalArgumentException {
85 HSBType oldvalue = (state == UnDefType.UNDEF) ? new HSBType() : (HSBType) state;
86 if (command instanceof HSBType) {
87 return (HSBType) command;
88 } else if (command instanceof OnOffType) {
89 OnOffType boolValue = ((OnOffType) command);
90 PercentType minOn = new PercentType(Math.max(oldvalue.getBrightness().intValue(), onBrightness));
91 return new HSBType(oldvalue.getHue(), oldvalue.getSaturation(),
92 boolValue == OnOffType.ON ? minOn : new PercentType(0));
93 } else if (command instanceof PercentType) {
94 return new HSBType(oldvalue.getHue(), oldvalue.getSaturation(), (PercentType) command);
96 final String updatedValue = command.toString();
97 if (onValue.equals(updatedValue)) {
98 PercentType minOn = new PercentType(Math.max(oldvalue.getBrightness().intValue(), onBrightness));
99 return new HSBType(oldvalue.getHue(), oldvalue.getSaturation(), minOn);
100 } else if (offValue.equals(updatedValue)) {
101 return new HSBType(oldvalue.getHue(), oldvalue.getSaturation(), new PercentType(0));
103 String[] split = updatedValue.split(",");
104 if (split.length != 3) {
105 throw new IllegalArgumentException(updatedValue + " is not a valid string syntax");
107 switch (this.colorMode) {
109 return new HSBType(updatedValue);
111 return HSBType.fromRGB(Integer.parseInt(split[0]), Integer.parseInt(split[1]),
112 Integer.parseInt(split[2]));
114 HSBType tempState = HSBType.fromXY(Float.parseFloat(split[0]), Float.parseFloat(split[1]));
115 return new HSBType(tempState.getHue(), tempState.getSaturation(), new PercentType(split[2]));
117 throw new IllegalArgumentException("Non supported color mode");
124 * Converts the color state to a string.
126 * @return Returns the color value depending on the color mode: as a HSB/HSV string (hue,saturation,brightness ->
127 * "60,100,100"), as an RGB string (red,green,blue -> "255,255,0") or as a xyY string
128 * ("0.419321,0.505255,100.00").
131 public String getMQTTpublishValue(Command command, @Nullable String pattern) {
132 String formatPattern = pattern;
133 if (formatPattern == null || "%s".equals(formatPattern)) {
134 if (this.colorMode == ColorMode.XYY) {
135 formatPattern = "%1$f,%2$f,%3$.2f";
137 formatPattern = "%1$d,%2$d,%3$d";
141 HSBType hsbState = (HSBType) command;
143 switch (this.colorMode) {
145 return String.format(formatPattern, hsbState.getHue().intValue(), hsbState.getSaturation().intValue(),
146 hsbState.getBrightness().intValue());
148 int[] rgb = ColorUtil.hsbToRgb(hsbState);
149 return String.format(formatPattern, rgb[0], rgb[1], rgb[2]);
151 double[] xyY = ColorUtil.hsbToXY(hsbState);
152 return String.format(Locale.ROOT, formatPattern, xyY[0], xyY[1],
153 hsbState.getBrightness().doubleValue());
155 throw new NotSupportedException(String.format("Non supported color mode: {}", this.colorMode));