]> git.basschouten.com Git - openhab-addons.git/blob
c1ce0b7d0379076603bfa714c49ff7fe27413476
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.hueemulation.internal.dto;
14
15 import org.openhab.core.library.types.DecimalType;
16 import org.openhab.core.library.types.HSBType;
17 import org.openhab.core.library.types.PercentType;
18
19 /**
20  * Hue API state object
21  *
22  * @author Dan Cunningham - Initial contribution
23  * @author David Graeff - "extended color light bulbs" support
24  * @author Florian Lentz - added xy support
25  *
26  */
27 public class HueStateColorBulb extends HueStateBulb {
28     public static final int MAX_HUE = 65535; // For extended color light bulbs
29     public int hue = 0;
30     public static final int MAX_SAT = 254;
31     public int sat = 0;
32
33     // color as array of xy-coordinates
34     public double[] xy = { 0, 0 };
35
36     public String effect = "none";
37
38     /** time for transition in centiseconds. */
39     public int transitiontime;
40
41     public static enum ColorMode {
42         ct,
43         hs,
44         xy
45     }
46
47     public ColorMode colormode = ColorMode.ct;
48
49     protected HueStateColorBulb() {
50     }
51
52     public HueStateColorBulb(boolean on) {
53         super(on);
54         colormode = ColorMode.ct;
55     }
56
57     /**
58      * Create a hue state with the given brightness percentage
59      *
60      * @param brightness Brightness percentage
61      * @param on On value
62      */
63     public HueStateColorBulb(PercentType brightness, boolean on) {
64         super(brightness, on);
65         colormode = ColorMode.ct;
66     }
67
68     /**
69      * Creates a hue state with the given color information
70      *
71      * @param hsb Color information. Sets the hue state to "on" if brightness is > 0.
72      */
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;
78     }
79
80     /**
81      * Converts this HueState to a HSBType
82      */
83     public HSBType toHSBType() {
84         if (colormode == ColorMode.xy) {
85             int i;
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);
95             if (r < g) {
96                 i = r;
97             } else {
98                 i = g;
99             }
100             double minValue = i;
101             if (minValue >= b) {
102                 minValue = b;
103             }
104             double maxValue = (r > g ? r : g);
105             if (maxValue <= b) {
106                 maxValue = b;
107             }
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));
112             }
113             double h;
114             if ((r) >= maxValue) {
115                 h = (g - b) / delta;
116             } else if ((g) >= maxValue) {
117                 h = 2.0d + ((b - r) / delta);
118             } else {
119                 h = 4.0d + ((r - g) / delta);
120             }
121             h *= 60.0d;
122             if (h < 0.0d) {
123                 h += 360.0d;
124             }
125             double hueSat = Math.floor((delta / maxValue) * 254.0d);
126             int percentSat = (int) ((100.0d * hueSat) / (MAX_SAT));
127
128             int bri = this.bri * 100 / MAX_BRI;
129             if (!this.on) {
130                 bri = 0;
131             }
132             return new HSBType(new DecimalType((Math.floor(182.04d * h) * 360.0d) / (MAX_HUE)),
133                     new PercentType(percentSat), new PercentType(bri));
134
135         } else {
136             int bri = this.bri * 100 / MAX_BRI;
137             int sat = this.sat * 100 / MAX_SAT;
138             int hue = this.hue * 360 / MAX_HUE;
139
140             if (!this.on) {
141                 bri = 0;
142             }
143             if (colormode == ColorMode.ct) {
144                 sat = 0;
145             }
146             return new HSBType(new DecimalType(hue), new PercentType(sat), new PercentType(bri));
147         }
148     }
149
150     @Override
151     public String toString() {
152         String xyString = "{";
153         for (double d : xy) {
154             xyString += d + " ";
155         }
156         xyString += "}";
157         return "on: " + on + ", brightness: " + bri + ", hue: " + hue + ", sat: " + sat + ", xy: " + xyString + ", ct: "
158                 + ct + ", alert: " + alert + ", effect: " + effect + ", colormode: " + colormode + ", reachable: "
159                 + reachable;
160     }
161 }