-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor-detector.html
79 lines (71 loc) · 2.58 KB
/
color-detector.html
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
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Example 2: color detector</title>
<link rel="stylesheet" href="common.css" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.3/require.min.js"></script>
<script>var process = false;</script>
<script src="https://unpkg.com/[email protected]"></script>
</head>
<body>
<div class="box">
<button>start experiment</button>
</div>
<div style="display:none">
<video width="640" height="480" autoplay></video>
<canvas width="640" height="480"></canvas>
</div>
<script>
require(['../dist/jsgatt.js'], function(Device) {
var SET_LIGHT_SERVICE = '33160fb9-5b27-4e70-b0f8-ff411e3ae078';
var SET_LIGHT_CHARACTERISTIC = '217887f8-0af2-4002-9c05-24c9ecf71600';
var SET_LIGHT_COLOR_SERVICE = 'b882e31b-5096-43d1-90a9-edbf95073337';
var SET_LIGHT_COLOR_CHARACTERISTIC = '74532143-fff1-460d-8e8a-370f934d40be';
var awos = new Device.default(window);
var toggle = awos.register(
SET_LIGHT_SERVICE,
SET_LIGHT_CHARACTERISTIC,
'SML-c4-GU10'
);
var color = awos.register(
SET_LIGHT_COLOR_SERVICE,
SET_LIGHT_COLOR_CHARACTERISTIC,
'SML-c4-GU10'
);
var colorbox = document.querySelector('.box');
var button = document.querySelector('button');
var video = document.querySelector('video');
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');
button.addEventListener('click', function() {
toggle(new Uint8Array([1])).then(function() {
button.style.display = 'none';
window.navigator
.mediaDevices
.getUserMedia({audio: false, video: true})
.then(function(stream) {
video.srcObject = stream;
camColorPicker();
});
});
});
function camColorPicker() {
context.drawImage(video, 0, 0);
var pixels = context.getImageData(0, 0, 640, 480).data;
var rgbpixel =[];
for (var i = 0; i < pixels.length/4; i += 4) {
rgbpixel.push([pixels[i], pixels[i+1], pixels[i+2]]);
}
var cmap = MMCQ.quantize(rgbpixel, 2);
var dominant = cmap? cmap.palette()[0] : null;
if (dominant) {
colorbox.style.backgroundColor = "rgb("+ dominant.join(',') +")";
color(new Uint8Array([dominant[0],dominant[1],dominant[2]]));
}
window.requestAnimationFrame(camColorPicker);
}
});
</script>
</body>
</html>