cap:colorable
- coloring of lights¶
Capability used for lights that can be colored.
if(thing.matches('type:light', 'cap:colorable')) {
console.log('Current color', await thing.color());
// Set the color
await thing.color('red');
}
API¶
-
color
([color[, duration]])¶ Get the current color or change the color of the light.
Arguments: - color – Optional color to set. The color can be
specified in many formats, hex values such as
#00ff00
, color names such asred
andblue
, and color temperatures such as4000K
orovercast
. - duration (Duration) – Optional duration to perform change in brightness over. Supported when the light has the fading-capability.
Returns: Promise that resolves to the current or set color.
Example:
// Get the current color const currentColor = await thing.color(); // Change color const newColor = await thing.color('4000K'); // Change color over 2 seconds await thing.color('#00ffff', '2s');
- color – Optional color to set. The color can be
specified in many formats, hex values such as
Events¶
Protected methods¶
-
updateColor
(color)¶ Update the current color of the light. Should be called whenever a change in color occurs for the light. If the color set has changed this will emit the
color
event.Arguments: - color – The color of the light.
this.updateColor('#ff00aa'); const { color } = require('abstract-things/values'); this.updateColor(color.rgb(255, 0, 170));
-
changeColor
(color, options)¶ Abstract. Change the color of the light. Implementation should support the following:
color
should be converted to something supported by the light.options.duration
should be respected if the light supports fading.
Arguments: - color – The new color of the light. The colorspace of the light can be be anything, but is most commonly temperatures or rgb-values.
- options – Options for changing the color. The only option available is
duration
which indicates amount of time the change should occur over.
Returns: Promise if change is asynchronous.
Implementing capability¶
Implementations should call updateColor
whenever the color of the light
changes. changeColor
needs to be implemented and will be called whenever a
color change is requested. color:temperature and
color:full should be implemented to indicate the type of
color supported.
const { Light, Colorable, ColorFull } = require('abstract-things/lights');
const { color } = require('abstract-things/values');
class Example extends Light.with(Colorable, ColorFull) {
initCallback() {
return super.initCallback()
.then(() => this.updateColor(color.rgb(0, 0, 0));
}
changeColor(color, options) {
// Convert color to RGB colorspace
const rgb = color.rgb;
return setColorSomehow(rgb, options.duration);
}
}