]> git.basschouten.com Git - openhab-addons.git/blob
4eb0d67d6f15b2b6b38c66bb5bc5fbe2607c83c5
[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.binding.wifiled.internal.handler;
14
15 import static java.lang.Math.max;
16
17 import org.openhab.core.library.types.HSBType;
18 import org.openhab.core.library.types.PercentType;
19
20 /**
21  * Internal LED state.
22  *
23  * @author Stefan Endrullis - Initial contribution
24  */
25 public class InternalLedState {
26
27     /** Values for the colors red, green, blue from 0 to 1. */
28     double r, g, b;
29     /** White values from 0 to 1. */
30     double w, w2;
31
32     public InternalLedState() {
33         this(0, 0, 0, 0, 0);
34     }
35
36     public InternalLedState(double r, double g, double b, double w, double w2) {
37         this.r = r;
38         this.g = g;
39         this.b = b;
40         this.w = w;
41         this.w2 = w2;
42     }
43
44     public static InternalLedState fromRGBW(int r, int g, int b, int w, int w2) {
45         return new InternalLedState(conv(r), conv(g), conv(b), conv(w), conv(w2));
46     }
47
48     public InternalLedState withColor(HSBType color) {
49         //@formatter:off
50         return new InternalLedState(
51             color.getRed().doubleValue()   / 100,
52             color.getGreen().doubleValue() / 100,
53             color.getBlue().doubleValue()  / 100,
54             w,
55             w2
56         );
57         //@formatter:on
58     }
59
60     public InternalLedState withBrightness(double brightness) {
61         return new InternalLedState(r * brightness, g * brightness, b * brightness, w, w2);
62     }
63
64     public InternalLedState withWhite(double w) {
65         return new InternalLedState(r, g, b, w, w2);
66     }
67
68     public InternalLedState withWhite2(double w2) {
69         return new InternalLedState(r, g, b, w, w2);
70     }
71
72     public PercentType toHSBType() {
73         return HSBType.fromRGB(conv(r), conv(g), conv(b));
74     }
75
76     /**
77      * Fades from this color to the that color according to the given progress value from 0 (this color)
78      * to 1 (that color).
79      *
80      * @param that that color
81      * @param progress value between 0 (this color) and 1 (that color)
82      * @return faded color
83      */
84     public InternalLedState fade(InternalLedState that, double progress) {
85         double invProgress = 1 - progress;
86
87         //@formatter:off
88         return new InternalLedState(
89             this.r * invProgress + that.r * progress,
90             this.g * invProgress + that.g * progress,
91             this.b * invProgress + that.b * progress,
92             this.w * invProgress + that.w * progress,
93             this.w2 * invProgress + that.w2 * progress
94         );
95         //@formatter:on
96     }
97
98     /**
99      * Returns the brightness or the RGB color.
100      *
101      * @return value between 0 and 1
102      */
103     public double getBrightness() {
104         return max(r, max(g, b));
105     }
106
107     /**
108      * Returns the white value.
109      *
110      * @return value between 0 and 1
111      */
112     public double getWhite() {
113         return w;
114     }
115
116     /**
117      * Returns the white2 value.
118      *
119      * @return value between 0 and 1
120      */
121     public double getWhite2() {
122         return w2;
123     }
124
125     private static double conv(int v) {
126         return (double) v / 255;
127     }
128
129     private static int conv(double v) {
130         return (int) (v * 255 + 0.5);
131     }
132
133     /**
134      * Returns red value.
135      *
136      * @return value between 0 and 255
137      */
138     public int getR() {
139         return conv(r);
140     }
141
142     /**
143      * Returns green value.
144      *
145      * @return value between 0 and 255
146      */
147     public int getG() {
148         return conv(g);
149     }
150
151     /**
152      * Returns blue value.
153      *
154      * @return value between 0 and 255
155      */
156     public int getB() {
157         return conv(b);
158     }
159
160     /**
161      * Returns white value.
162      *
163      * @return value between 0 and 255
164      */
165     public int getW() {
166         return conv(w);
167     }
168
169     /**
170      * Returns white2 value.
171      *
172      * @return value between 0 and 255
173      */
174     public int getW2() {
175         return conv(w2);
176     }
177
178     @Override
179     public boolean equals(Object o) {
180         if (this == o) {
181             return true;
182         }
183         if (o == null || getClass() != o.getClass()) {
184             return false;
185         }
186
187         InternalLedState that = (InternalLedState) o;
188
189         //@formatter:off
190         return Double.compare(that.r, r) == 0
191             && Double.compare(that.g, g) == 0
192             && Double.compare(that.b, b) == 0
193             && Double.compare(that.w, w) == 0;
194         //@formatter:on
195     }
196
197     @Override
198     public int hashCode() {
199         int result;
200         long temp;
201         temp = Double.doubleToLongBits(r);
202         result = (int) (temp ^ (temp >>> 32));
203         temp = Double.doubleToLongBits(g);
204         result = 31 * result + (int) (temp ^ (temp >>> 32));
205         temp = Double.doubleToLongBits(b);
206         result = 31 * result + (int) (temp ^ (temp >>> 32));
207         temp = Double.doubleToLongBits(w);
208         result = 31 * result + (int) (temp ^ (temp >>> 32));
209         return result;
210     }
211
212     @Override
213     public String toString() {
214         return "InternalLedState{" + "r=" + r + ", g=" + g + ", b=" + b + ", w=" + w + '}';
215     }
216 }