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.shelly.internal.handler;
15 import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.*;
17 import java.math.BigDecimal;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.core.library.types.HSBType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.PercentType;
25 * The {@link ShellyColorUtils} provides some utility functions around RGBW handling.
27 * @author Markus Michels - Initial contribution
30 public class ShellyColorUtils {
31 OnOffType power = OnOffType.OFF;
38 PercentType percentRed = new PercentType(0);
39 PercentType percentGreen = new PercentType(0);
40 PercentType percentBlue = new PercentType(0);
41 PercentType percentWhite = new PercentType(0);
48 PercentType percentGain = new PercentType(0);
49 PercentType percentBrightness = new PercentType(0);
50 PercentType percentTemp = new PercentType(0);
53 public ShellyColorUtils() {
56 public ShellyColorUtils(ShellyColorUtils col) {
57 minTemp = col.minTemp;
58 maxTemp = col.maxTemp;
64 setBrightness(col.brightness);
68 public void setMode(String mode) {
72 public void setMinMaxTemp(int min, int max) {
77 public boolean setRGBW(int red, int green, int blue, int white) {
85 public boolean setRed(int value) {
86 boolean changed = red != value;
88 percentRed = toPercent(red);
92 public boolean setGreen(int value) {
93 boolean changed = green != value;
95 percentGreen = toPercent(green);
99 public boolean setBlue(int value) {
100 boolean changed = blue != value;
102 percentBlue = toPercent(blue);
106 public boolean setWhite(int value) {
107 boolean changed = white != value;
109 percentWhite = toPercent(white);
113 public boolean setBrightness(int value) {
114 boolean changed = brightness != value;
116 percentBrightness = toPercent(brightness, SHELLY_MIN_BRIGHTNESS, SHELLY_MAX_BRIGHTNESS);
120 public boolean setGain(int value) {
121 boolean changed = gain != value;
123 percentGain = toPercent(gain, SHELLY_MIN_GAIN, SHELLY_MAX_GAIN);
127 public boolean setTemp(int value) {
128 boolean changed = temp != value;
130 percentTemp = toPercent(temp, minTemp, maxTemp);
134 public boolean setEffect(int value) {
135 boolean changed = effect != value;
140 public HSBType toHSB() {
141 return HSBType.fromRGB(red, green, blue);
144 public Integer[] fromRGBW(String rgbwString) {
145 Integer[] values = new Integer[4];
146 values[0] = values[1] = values[2] = values[3] = -1;
148 String[] rgbw = rgbwString.split(",");
149 for (int i = 0; i < rgbw.length; i++) {
150 values[i] = Integer.parseInt(rgbw[i]);
152 } catch (NullPointerException e) { // might be a format problem
153 throw new IllegalArgumentException(
154 "Unable to convert fullColor value: " + rgbwString + ", " + e.getMessage());
156 if (values[0] != -1) {
159 if (values[1] != -1) {
162 if (values[2] != -1) {
165 if (values[3] != -1) {
171 public boolean isRgbValid() {
172 return (red != -1) && (blue != -1) && (green != -1);
175 public static PercentType toPercent(Integer value) {
176 return toPercent(value, 0, SHELLY_MAX_COLOR);
179 public static PercentType toPercent(Integer _value, Integer min, Integer max) {
180 double range = max.doubleValue() - min.doubleValue();
181 double value = _value.doubleValue();
182 value = value < min ? min.doubleValue() : value;
183 value = value > max ? max.doubleValue() : value;
184 double percent = 0.0;
186 percent = Math.round((value - min) / range * 100);
188 return new PercentType(new BigDecimal(percent));