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.io.hueemulation.internal.dto;
15 import org.openhab.core.library.types.DecimalType;
16 import org.openhab.core.library.types.HSBType;
17 import org.openhab.core.library.types.PercentType;
20 * Hue API state object
22 * @author Dan Cunningham - Initial contribution
23 * @author David Graeff - "extended color light bulbs" support
24 * @author Florian Lentz - added xy support
27 public class HueStateColorBulb extends HueStateBulb {
28 public static final int MAX_HUE = 65535; // For extended color light bulbs
30 public static final int MAX_SAT = 254;
33 // color as array of xy-coordinates
34 public double[] xy = { 0, 0 };
36 public String effect = "none";
38 /** time for transition in centiseconds. */
39 public int transitiontime;
41 public static enum ColorMode {
47 public ColorMode colormode = ColorMode.ct;
49 protected HueStateColorBulb() {
52 public HueStateColorBulb(boolean on) {
54 colormode = ColorMode.ct;
58 * Create a hue state with the given brightness percentage
60 * @param brightness Brightness percentage
63 public HueStateColorBulb(PercentType brightness, boolean on) {
64 super(brightness, on);
65 colormode = ColorMode.ct;
69 * Creates a hue state with the given color information
71 * @param hsb Color information. Sets the hue state to "on" if brightness is > 0.
73 public HueStateColorBulb(HSBType hsb) {
74 super(hsb.getBrightness(), hsb.getBrightness().intValue() > 0);
75 this.hue = (int) (hsb.getHue().intValue() * MAX_HUE / 360.0 + 0.5);
76 this.sat = (int) (hsb.getSaturation().intValue() * MAX_SAT / 100.0 + 0.5);
77 colormode = this.sat > 0 ? ColorMode.hs : ColorMode.ct;
81 * Converts this HueState to a HSBType
83 public HSBType toHSBType() {
84 if (colormode == ColorMode.xy) {
86 double y = (this.bri) / 100.0d;
87 double x = (y / this.xy[1]) * this.xy[0];
88 double z = (y / this.xy[1]) * ((1.0d - this.xy[0]) - this.xy[1]);
89 int r = (int) (Math.abs(
90 ((1.4628067016601562d * x) - (0.18406230211257935d * y)) - (0.2743605971336365d * z)) * 255.0d);
91 int g = (int) (Math.abs(
92 (((-x) * 0.5217933058738708d) + (1.4472380876541138d * y)) + (0.06772270053625107d * z)) * 255.0d);
93 int b = (int) (Math.abs(
94 ((0.03493420034646988d * x) - (0.09689299762248993d * y)) + (1.288409948348999d * z)) * 255.0d);
104 double maxValue = (r > g ? r : g);
108 double delta = maxValue - minValue;
109 if (maxValue <= 0.0d) {
110 return new HSBType(new DecimalType(0), new PercentType(100),
111 new PercentType((this.bri * 100) / MAX_BRI));
114 if ((r) >= maxValue) {
116 } else if ((g) >= maxValue) {
117 h = 2.0d + ((b - r) / delta);
119 h = 4.0d + ((r - g) / delta);
125 double hueSat = Math.floor((delta / maxValue) * 254.0d);
126 int percentSat = (int) ((100.0d * hueSat) / (MAX_SAT));
128 int bri = this.bri * 100 / MAX_BRI;
132 return new HSBType(new DecimalType((Math.floor(182.04d * h) * 360.0d) / (MAX_HUE)),
133 new PercentType(percentSat), new PercentType(bri));
136 int bri = this.bri * 100 / MAX_BRI;
137 int sat = this.sat * 100 / MAX_SAT;
138 int hue = this.hue * 360 / MAX_HUE;
143 if (colormode == ColorMode.ct) {
146 return new HSBType(new DecimalType(hue), new PercentType(sat), new PercentType(bri));
151 public String toString() {
152 String xyString = "{";
153 for (double d : xy) {
157 return "on: " + on + ", brightness: " + bri + ", hue: " + hue + ", sat: " + sat + ", xy: " + xyString + ", ct: "
158 + ct + ", alert: " + alert + ", effect: " + effect + ", colormode: " + colormode + ", reachable: "