[feican] Use new core class ColorUtil for RGB conversion (#14771)

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich 2023-05-01 20:46:05 +02:00 committed by GitHub
parent 2c8f639060
commit feee01d65c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,13 +12,11 @@
*/
package org.openhab.binding.feican.internal;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.util.ColorUtil;
/**
* Creates commands to send to Feican devices.
@ -67,25 +65,13 @@ public class Commands {
*/
public byte[] color(HSBType color) {
byte[] command = RGB_COMMAND.clone();
PercentType[] rgb = color.toRGB();
command[4] = convertColorPercentToByte(rgb[0]);
command[5] = convertColorPercentToByte(rgb[1]);
command[6] = convertColorPercentToByte(rgb[2]);
int[] rgb = ColorUtil.hsbToRgb(color);
command[4] = (byte) rgb[0];
command[5] = (byte) rgb[1];
command[6] = (byte) rgb[2];
return command;
}
/**
* Converts a percentage (0-100) to a color range value (0-255). This is needed because {@link HSBType} returns
* color values in the 100-range while a 255 range is needed.
*
* @param percent value to be converted.
* @return converted value as a byte value
*/
private byte convertColorPercentToByte(PercentType percent) {
return percent.toBigDecimal().multiply(BigDecimal.valueOf(255))
.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP).byteValue();
}
/**
* Returns the command to set the color temperature to a value between 0 and 100.
*