nanoleaf-cove
Version:
Simple NanoLeaf Api for Node.js
143 lines (142 loc) • 4.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LightState = void 0;
var tslib_1 = require("tslib");
var color_transformer_1 = require("./color-transformer");
var isFunction = function (object) {
var getClass = {}.toString;
return object && getClass.call(object) === '[object Function]';
};
var SuperState = (function () {
function SuperState() {
var _this = this;
this._values = {};
this.transition = function (value) {
_this._values['duration'] = value / 1000;
return _this;
};
this.duration = this.transition;
this.fast = function (value) {
_this._values['fast'] = value;
return _this;
};
this.getValues = function () {
return _this._values;
};
}
return SuperState;
}());
var LightState = (function (_super) {
tslib_1.__extends(LightState, _super);
function LightState(values) {
var _this = _super.call(this) || this;
_this.create = function (values) {
if (values) {
Object.keys(values).forEach(function (value) {
var fn;
if (_this.hasOwnProperty(value)) {
fn = _this[value];
if (isFunction(fn)) {
fn.apply(_this, [values[value]]);
}
}
});
}
return _this;
};
_this.on = function (value) {
if (value === void 0) { value = true; }
_this._values['on'] = { value: !!value };
return _this;
};
_this.turnOn = _this.on;
_this.off = function () {
_this.on(false);
return _this;
};
_this.turnOff = _this.off;
_this.hue = function (value) {
_this._values['hue'] = { value: value };
return _this;
};
_this.hueIncrement = function (value) {
_this._values['hue'] = { increment: value };
return _this;
};
_this.bri = function (value, duration) {
if (duration === void 0) { duration = 1; }
_this._values['brightness'] = { value: value, duration: duration };
return _this;
};
_this.brightness = _this.bri;
_this.briIncrement = function (value) {
_this._values['brightness'] = { increment: value };
return _this;
};
_this.brightnessIncrement = _this.briIncrement;
_this.sat = function (value) {
_this._values['sat'] = { value: value };
return _this;
};
_this.saturation = _this.sat;
_this.satIncrement = function (value) {
_this._values['sat'] = { increment: value };
return _this;
};
_this.saturationIncrement = _this.satIncrement;
_this.hsv = function (value) {
var convertedHsv = [];
if (Array.isArray(value)) {
convertedHsv = value;
}
else {
convertedHsv = [value.h, value.s, value.v];
}
_this.hue(convertedHsv[0]);
_this.sat(convertedHsv[1]);
_this.bri(convertedHsv[2]);
return _this;
};
_this.temp = function (value) {
if (value > 6500)
value = 6500;
else if (value < 1200)
value = 1200;
_this._values['ct'] = { value: value };
return _this;
};
_this.temperature = _this.temp;
_this.tempIncrement = function (value) {
_this._values['ct'] = { increment: value };
return _this;
};
_this.temperatureIncrement = _this.tempIncrement;
_this.rgb = function (value) {
var convertedRgb;
if (Array.isArray(value)) {
convertedRgb = { r: value[0], g: value[1], b: value[2] };
}
else {
convertedRgb = value;
}
var convertedHsv = color_transformer_1.rgb2hsv([convertedRgb.r, convertedRgb.g, convertedRgb.b]);
return _this.hsv([convertedHsv.h, convertedHsv.s, convertedHsv.v]);
};
_this.color = function (value) {
if (Array.isArray(value)) {
value = { r: value[0], g: value[1], b: value[2] };
}
if (value.ct) {
return _this.temp(value.ct);
}
else {
return _this.rgb({ r: value.r, g: value.g, b: value.b });
}
};
if (values)
_this.create(values);
return _this;
}
return LightState;
}(SuperState));
exports.LightState = LightState;