# How to convert RGB to HSB and vice versa?

**URL:** <https://community.openhab.org/t/how-to-convert-rgb-to-hsb-and-vice-versa/31289>\
**Category:** Scripts & Rules\
**Created:** [July 9, 2017, 11:03am UTC](https://community.openhab.org/t/how-to-convert-rgb-to-hsb-and-vice-versa/31289 "2017-07-09T11:03:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nguy\_n\_Phuc\_Anh\_D\_ng](https://community.openhab.org/letter_avatar_proxy/v4/letter/n/ccd318/32.png) [@Nguy\_n\_Phuc\_Anh\_D\_ng](https://community.openhab.org/u/Nguy_n_Phuc_Anh_D_ng)\
**Post date:** [July 9, 2017, 11:03am UTC](https://community.openhab.org/t/how-to-convert-rgb-to-hsb-and-vice-versa/31289/1 "2017-07-09T11:03:48Z")

</div>

Hi friends, as the title, my question is how to convert RGB string to HSB string and vice versa in .rule file?  
I created this .rules file to convert RGB to HSB values but didn’t success:

```javascript
import java.awt.Color
var HSBType hsbValue
rule "RED DIMMER"
  when
    Item WIFI_RGB_RED_DIMMER received command
  then
		var int percent = 100
		if(WIFI_RGB_RED_DIMMER.state instanceof DecimalType) percent = (WIFI_RGB_RED_DIMMER.state as DecimalType).intValue 
		if(receivedCommand==INCREASE) percent = percent + 1
		if(receivedCommand==DECREASE) percent = percent - 1
		if(percent<0) percent = 0
		if(percent>100) percent = 100
		redValue=percent
		var Color color=new Color(redValue,greenValue,blueValue)
		hsbValue = new HSBType(color.toString)
		sendCommand(WIFI_RGB,hsbValue)
end

```

my purpose is that I want to whenever I adjust the slider item WIFI\_RGB\_RED\_DIMMER then the color item WIFI\_RGB must change its color respectively. I am now constantly receive this error:

```javascript
 Rule 'RED DIMMER': An error occured during the script execution: Could not invoke constructor: java.awt.Color.Color(int,int,int)

```

Any help? I’d be very appreciated!

---

<div class="post-metadata">

**Author:** ![namraccr](https://community.openhab.org/user_avatar/community.openhab.org/namraccr/32/36484_2.png) [@namraccr](https://community.openhab.org/u/namraccr)\
**Post date:** [July 9, 2017, 1:19pm UTC](https://community.openhab.org/t/how-to-convert-rgb-to-hsb-and-vice-versa/31289/2 "2017-07-09T13:19:32Z")

</div>

I don’t see where you’re assigning/declaring redValue, greenValue, and blueValue. Did you omit that from your posting, or is it missing from your rule too?

Also, color.toString returns something like this: `java.awt.Color[r=100,g=75,b=55]`, which I don’t think is accceptable in the HSBType constructor.

Here’s a quick test and its output (running under OH2 2.0):

```auto
		var int percent = 100
		var redValue = percent
		var greenValue = 75
		var blueValue = 55
		var Color color=new Color(redValue,greenValue,blueValue)

```

```auto
09:15:05.035 [WARN] [clipse.smarthome.model.script.color!] - Color test...
09:15:05.036 [WARN] [clipse.smarthome.model.script.color!] - Color: java.awt.Color[r=100,g=75,b=55]

```

---

<div class="post-metadata">

**Author:** ![sihui](https://community.openhab.org/user_avatar/community.openhab.org/sihui/32/55674_2.png) [@sihui](https://community.openhab.org/u/sihui)\
**Post date:** [July 9, 2017, 2:09pm UTC](https://community.openhab.org/t/how-to-convert-rgb-to-hsb-and-vice-versa/31289/3 "2017-07-09T14:09:53Z")

</div>

This may also help:

> [@Example: Convert Color Item Values To RGB (With Explanation)](https://community.openhab.org/t/example-convert-color-item-values-to-rgb-with-explanation/24014):
>
> Because there are regular questions on how to read the Color Items’ values as 24-bit RGB ranging from 0-255 I just wrote this little explanation how and why it works. (Or at least one of the many ways) Let’s say you have a Color item named aacc\_color. With this rule you can read out the R, G and B values from it: rule "get RGB Colors from ColorItem" when Item aacc\_color received command then val red = (receivedCommand as HSBType).red \* 2.55 val green = (receivedCommand as HSBType).gr…

---

<div class="post-metadata">

**Author:** ![Nguy\_n\_Phuc\_Anh\_D\_ng](https://community.openhab.org/letter_avatar_proxy/v4/letter/n/ccd318/32.png) [@Nguy\_n\_Phuc\_Anh\_D\_ng](https://community.openhab.org/u/Nguy_n_Phuc_Anh_D_ng)\
**Post date:** [July 9, 2017, 2:30pm UTC](https://community.openhab.org/t/how-to-convert-rgb-to-hsb-and-vice-versa/31289/4 "2017-07-09T14:30:55Z")

</div>

Oh I forgot to post those variables from my post.  
They do exist in my rule file  
I declared outside the rule “Red DIMMER”  
Then I moved it inside the rule, like this:

```php
rule "RED DIMMER"
  when
    Item WIFI_RGB_RED_DIMMER received command
  then
		var int percent = 100
		if(WIFI_RGB_RED_DIMMER.state instanceof DecimalType) percent = (WIFI_RGB_RED_DIMMER.state as DecimalType).intValue 
		if(receivedCommand==INCREASE) percent = percent + 1
		if(receivedCommand==DECREASE) percent = percent - 1
		if(percent<0) percent = 0
		if(percent>100) percent = 100
		var redValue=percent
		var greenValue = 75
        var blueValue = 55
		var Color color=new Color(redValue,greenValue,blueValue)
		var HSBType hsbValue = new HSBType(color.toString)
		sendCommand(WIFI_RGB,hsbValue)
		//postUpdate(WIFI_RGB_RED_DIMMER, percent);
end

```

There is still an error in the event log:

```php
21:28:00.596 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'RED DIMMER': null

```

What wrong with this?

---

<div class="post-metadata">

**Author:** ![marcel\_erkel](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@marcel\_erkel](https://community.openhab.org/u/marcel_erkel)\
**Post date:** [July 9, 2017, 2:39pm UTC](https://community.openhab.org/t/how-to-convert-rgb-to-hsb-and-vice-versa/31289/5 "2017-07-09T14:39:31Z")

</div>

> [@Nguy\_n\_Phuc\_Anh\_D\_ng](#):
>
> ```
> var redValue=percent
> var greenValue = 75
> var blueValue = 55
> 
> ```

These should probably include the type:

```
    var int redValue=percent
    var int greenValue = 75
    var int blueValue = 55

```

---

<div class="post-metadata">

**Author:** ![Nguy\_n\_Phuc\_Anh\_D\_ng](https://community.openhab.org/letter_avatar_proxy/v4/letter/n/ccd318/32.png) [@Nguy\_n\_Phuc\_Anh\_D\_ng](https://community.openhab.org/u/Nguy_n_Phuc_Anh_D_ng)\
**Post date:** [July 9, 2017, 2:43pm UTC](https://community.openhab.org/t/how-to-convert-rgb-to-hsb-and-vice-versa/31289/6 "2017-07-09T14:43:44Z")

</div>

@marcel_erkel  
OK, I changed:

```php
rule "RED DIMMER"
  when
    Item WIFI_RGB_RED received command
  then
		var int percent = 0
		if(WIFI_RGB_RED.state instanceof DecimalType) percent = (WIFI_RGB_RED.state as DecimalType).intValue 
		if(receivedCommand==INCREASE) percent = percent + 1
		if(receivedCommand==DECREASE) percent = percent - 1
		if(percent<0) percent = 0
		if(percent>100) percent = 100
		var int red=percent
        var int green= 75
        var int blue= 55
        var Color color=new Color(red,green,blue)
        var HSBType hsbValue = new HSBType(color.toString)
        sendCommand(WIFI_RGB,hsbValue)
        postUpdate(WIFI_RGB_RED, red);
end

```

But the error still persist:

```php
21:42:41.406 [WARN] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn't post update for 'WIFI_RGB_RED'
21:42:41.411 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'RED DIMMER': null
21:43:32.515 [INFO] [marthome.event.ItemStateChangedEvent] - Date changed from 2017-07-09T21:42:32.538+0700 to 2017-07-09T21:43:32.514+0700

```

---

<div class="post-metadata">

**Author:** ![namraccr](https://community.openhab.org/user_avatar/community.openhab.org/namraccr/32/36484_2.png) [@namraccr](https://community.openhab.org/u/namraccr)\
**Post date:** [July 9, 2017, 2:45pm UTC](https://community.openhab.org/t/how-to-convert-rgb-to-hsb-and-vice-versa/31289/7 "2017-07-09T14:45:50Z")

</div>

As previously stated, this is never going to work as an input to the HSBType constructor:

> [@namraccr](#):
>
> java.awt.Color[r=100,g=75,b=55]

It needs a string in the form “val1,val2,val3”, where each is a string representation of an acceptable value for H, S and B.

---

<div class="post-metadata">

**Author:** ![marcel\_erkel](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@marcel\_erkel](https://community.openhab.org/u/marcel_erkel)\
**Post date:** [July 9, 2017, 3:59pm UTC](https://community.openhab.org/t/how-to-convert-rgb-to-hsb-and-vice-versa/31289/8 "2017-07-09T15:59:47Z")

</div>

How about this:

Add the following import:

```php
import java.util.List

```

```php
rule "RED DIMMER"
  when
    Item WIFI_RGB_RED received command
  then
	var int percent = 0
	if(WIFI_RGB_RED.state instanceof DecimalType) percent = (WIFI_RGB_RED.state as DecimalType).intValue 
	if(receivedCommand==INCREASE) percent = percent + 1
	if(receivedCommand==DECREASE) percent = percent - 1
	if(percent<0) percent = 0
	if(percent>100) percent = 100
	var int red=percent
	var int green= 75
	var int blue= 55

	val List<Float> hsb = Color.RGBtoHSB(red,green,blue,null)
	val String hsbString = hsb.get(0) + "," + hsb.get(1) + "," + hsb.get(2)

	var HSBType hsbValue = new HSBType(hsbString)
        sendCommand(WIFI_RGB,hsbValue)
        postUpdate(WIFI_RGB_RED, red);
end

```

The `Color.RGBtoHSB()` method is a static method of the Color class. It returns an array of type float. The problem is that the DSL used by openHAB apparently does not handle arrays. However, it does support to automatically convert them to a List so that’s why this `val List<Float> hsb = Color.RGBtoHSB(red,green,blue,null)` actually works.  
Next a String is created as indicated by @namraccr.  
I don’t have any color lights so I can’t test, but when executing the rule and printing out the hsbString it looked ok to me.

---

<div class="post-metadata">

**Author:** ![marcel\_erkel](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@marcel\_erkel](https://community.openhab.org/u/marcel_erkel)\
**Post date:** [July 9, 2017, 4:19pm UTC](https://community.openhab.org/t/how-to-convert-rgb-to-hsb-and-vice-versa/31289/9 "2017-07-09T16:19:13Z")

</div>

For completeness, here’s my test code which includes the conversion from RGB to HSB and from HSB to RGB:

Required imports

```php
import java.awt.Color
import java.util.List

```

```php
	// from RGB to HSB
	val List<Float> hsb = Color.RGBtoHSB(250,150,50,null)
	val float hue = hsb.get(0)
	val float saturation = hsb.get(1)
	val float brightness = hsb.get(2)
	val String hsbString = hue + "," + saturation + "," + brightness
 
	logInfo("scene.rules", "H: " + hue)
	logInfo("scene.rules", "S: " + saturation)
	logInfo("scene.rules", "B: " + brightness)
	logInfo("scene.rules", "HSB: " + hsbString)

	// from HSB to RGB
	val int rgb = Color.HSBtoRGB(hue, saturation, brightness)
	val color = new Color(rgb)

	logInfo("scene.rules", "R: " + color.getRed())
	logInfo("scene.rules", "G: " + color.getGreen())
	logInfo("scene.rules", "B: " + color.getBlue())

```

---

<div class="post-metadata">

**Author:** ![a.tibi](https://community.openhab.org/user_avatar/community.openhab.org/a.tibi/32/17401_2.png) [@a.tibi](https://community.openhab.org/u/a.tibi)\
**Post date:** [July 14, 2018, 2:57pm UTC](https://community.openhab.org/t/how-to-convert-rgb-to-hsb-and-vice-versa/31289/10 "2018-07-14T14:57:58Z")

</div>

You can use this to convert into HSB:

```php
hsbValue = HSBType.fromRGB(redValue,greenValue,blueValue)

```
