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.wifiled.internal.handler;
15 import static java.lang.Math.max;
17 import org.openhab.core.library.types.HSBType;
18 import org.openhab.core.library.types.PercentType;
23 * @author Stefan Endrullis - Initial contribution
25 public class InternalLedState {
27 /** Values for the colors red, green, blue from 0 to 1. */
29 /** White values from 0 to 1. */
32 public InternalLedState() {
36 public InternalLedState(double r, double g, double b, double w, double w2) {
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));
48 public InternalLedState withColor(HSBType color) {
50 return new InternalLedState(
51 color.getRed().doubleValue() / 100,
52 color.getGreen().doubleValue() / 100,
53 color.getBlue().doubleValue() / 100,
60 public InternalLedState withBrightness(double brightness) {
61 return new InternalLedState(r * brightness, g * brightness, b * brightness, w, w2);
64 public InternalLedState withWhite(double w) {
65 return new InternalLedState(r, g, b, w, w2);
68 public InternalLedState withWhite2(double w2) {
69 return new InternalLedState(r, g, b, w, w2);
72 public PercentType toHSBType() {
73 return HSBType.fromRGB(conv(r), conv(g), conv(b));
77 * Fades from this color to the that color according to the given progress value from 0 (this color)
80 * @param that that color
81 * @param progress value between 0 (this color) and 1 (that color)
84 public InternalLedState fade(InternalLedState that, double progress) {
85 double invProgress = 1 - progress;
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
99 * Returns the brightness or the RGB color.
101 * @return value between 0 and 1
103 public double getBrightness() {
104 return max(r, max(g, b));
108 * Returns the white value.
110 * @return value between 0 and 1
112 public double getWhite() {
117 * Returns the white2 value.
119 * @return value between 0 and 1
121 public double getWhite2() {
125 private static double conv(int v) {
126 return (double) v / 255;
129 private static int conv(double v) {
130 return (int) (v * 255 + 0.5);
136 * @return value between 0 and 255
143 * Returns green value.
145 * @return value between 0 and 255
152 * Returns blue value.
154 * @return value between 0 and 255
161 * Returns white value.
163 * @return value between 0 and 255
170 * Returns white2 value.
172 * @return value between 0 and 255
179 public boolean equals(Object o) {
183 if (o == null || getClass() != o.getClass()) {
187 InternalLedState that = (InternalLedState) o;
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;
198 public int hashCode() {
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));
213 public String toString() {
214 return "InternalLedState{" + "r=" + r + ", g=" + g + ", b=" + b + ", w=" + w + '}';