-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
85 lines (76 loc) · 1.81 KB
/
server.js
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
76
77
78
79
80
81
82
83
84
85
const express = require('express')
const app = express()
const cssColor = require('color-functions/lib/css-color');
const isPi = require('detect-rpi')();
const Blinkt = isPi ? require('node-blinkt') : undefined;
function chip(color) {
return `<a href='/color/${encodeURIComponent(color)}' class="chip" style='background-color:${color}'></a>`
}
app.get('/', (req, res) => {
const hi = 360 / 10;
const si = 20;
const li = 5;
let str = '';
for (let h = 0; h <= 360; h += hi) {
for (let s = 0; s <= 100; s += si) {
str += `<div class='row'>`;
for (let l = 0; l <= 100; l += li) {
const color = `hsl(${h},${s}%,${l}%)`;
str += chip(color);
}
str += `</div>`;
}
}
const page = `
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.chip {
width: 40px;
height: 40px;
display: block;
flex: 1;
}
.row {
display: flex;
width: 100%;
}
</style>
</head>
<body>
<div class='container'>
${str}
</div>
</body>
</html>
`;
// req.send('<html></html>')
res.send(page);
})
function setColor(r, g, b, a) {
if (Blinkt) {
const leds = new Blinkt();
leds.setup();
leds.setAllPixels(r, g, b, a);
leds.sendUpdate();
leds.sendUpdate();
}
}
app.get('/color/:color', (req, res) => {
const {params:{color}} = req;
const rgba = cssColor(color);
if (rgba) {
const {r, g, b, a} = rgba;
setColor(r, g, b, a);
console.log(`Set color to ${color}`);
res.redirect('/');
} else {
res.send(`Error parsing color`)
}
})
const port = process.env.PORT || 3000;
app.listen(port, function () {
console.log('Example app listening on port 3000!')
})