-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmxRGB.ts
75 lines (64 loc) · 1.99 KB
/
dmxRGB.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { dmxnet as dmx } from "dmxnet";
export interface DMXRGBConfig {
artnetip: string;
channel: number;
offsetRed: number;
offsetGreen: number;
offsetBlue: number;
offsetDimmer?: number;
}
export default class DMXRGB {
private config: DMXRGBConfig;
private artnetClient: any;
private static hexRegexp = new RegExp("^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$");
private color: [number, number, number] = [255, 255, 255];
private brightness: number = 1;
constructor(config: DMXRGBConfig) {
this.config = config;
const dmxnet = new dmx({});
this.artnetClient = dmxnet.newSender({
ip: config.artnetip
});
this.setBrightness(1);
}
setColor(hexColor: string) {
const color = hexColor.toLowerCase();
const matches = DMXRGB.hexRegexp.exec(color);
if (matches === null) {
return;
}
console.log(matches);
let colorValues = matches.slice(1).map((hex: string) => parseInt(hex, 16)) as [
number,
number,
number
];
this.color = colorValues;
if (!this.config.offsetDimmer) {
colorValues = colorValues.map(color => color * this.brightness) as any;
}
this.setColorChannels(...colorValues);
}
setBrightness(brightness: number) {
brightness = Math.max(0, Math.min(1, brightness));
if ("offsetDimmer" in this.config) {
this.artnetClient.setChannel(
this.config.channel + this.config.offsetDimmer,
Math.round(brightness * 255)
);
return;
}
const colors = this.color.map(value => value * brightness) as [
number,
number,
number
];
this.setColorChannels(...colors);
this.brightness = brightness;
}
private setColorChannels(red: number, green: number, blue: number) {
this.artnetClient.setChannel(this.config.channel + this.config.offsetRed, red);
this.artnetClient.setChannel(this.config.channel + this.config.offsetGreen, green);
this.artnetClient.setChannel(this.config.channel + this.config.offsetBlue, blue);
}
}