JS Script: How use HSBType to do RGB to HSB conversion?

I need to convert a calculated RGB value to HSB in JS Script so I can do a sendCommand(“H, S, B”) to a Hue bulb. I’ve tried this:

var HSB = new HSBType.fromRGB(red, green, blue);

And see an error in the log that HSBType is not defined. Do I need to import a library or something to get access to the HSBType?

Unfortunately the OH types are not included by default. So you will need to bring in the HSBType using

var HSBType = Java.type('org.openhab.core.library.types.HSBType');

Alternatively you can import all the types and everything else using

var runtime = require(@runtime)

And then reference it using ‘runtime.HSBType’.

Many thanks for the info, Rich. You’re so helpful!

The following code works:

var HSBType = Java.type('org.openhab.core.library.types.HSBType');
var HSB = HSBType.fromRGB(Math.round(red), Math.round(green), Math.round(blue));
logger.info("{} kelvin converted to red: {}, green: {}, blue: {}.", triggeringItem.state, red, green, blue);
logger.info("RGB values converted to hsb: {}.", HSB);
items.getItem("LightTableColour1_Color").sendCommand(HSB.toString());

I’d copied an RGB to HSB function from somewhere while I was stuck with the HSBType problem but now I can remove that function to tidy the code up a bit.