]> git.basschouten.com Git - openhab-addons.git/commitdiff
[feican] Use new core class ColorUtil for RGB conversion (#14771)
authorHolger Friedrich <holgerfriedrich@users.noreply.github.com>
Mon, 1 May 2023 18:46:05 +0000 (20:46 +0200)
committerGitHub <noreply@github.com>
Mon, 1 May 2023 18:46:05 +0000 (20:46 +0200)
Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
bundles/org.openhab.binding.feican/src/main/java/org/openhab/binding/feican/internal/Commands.java

index 52c070d7358c1f6aa8ad896a2054bb2950bb500a..3770d2f9d4631442ee9f3bde24f097bd8a2511fc 100644 (file)
  */
 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.
      *