-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (80 loc) · 1.82 KB
/
script.js
File metadata and controls
88 lines (80 loc) · 1.82 KB
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
86
87
88
BrowserFS.install(window);
BrowserFS.configure({
fs: "LocalStorage"
}, function(e) {
if (e) {
throw e;
}
});
var fs = require('fs');
let defaultData = {
data: [
{
input: { r: 0, g: 0, b: 0 },
output: [1]
},
{
input: { r: 1, g: 1, b: 1 },
output: [0]
}
]
};
let contents = [];
fs.readFile('/learned.json', function(err, learnedData) {
if(!learnedData) {
fs.writeFile('/learned.json', JSON.stringify(defaultData), function(err) {
if(err) {
console.log('ERROR WRITING', err);
}
});
}
});
fs.readFile('/learned.json', function(err, learnedData) {
if(learnedData) {
const learned = JSON.parse(learnedData.toString());
if(learned && learned.data) {
contents = learned.data;
console.log('CONTENTS', contents);
}
}
});
if(!contents) {
console.log('No Data Found.');
}
const network = new brain.NeuralNetwork();
network.train(contents);
const colorElem = document.getElementById('color');
const guessElem = document.getElementById('guess');
let color;
setRandomColor();
function setRandomColor() {
color = {
r: Math.random(),
g: Math.random(),
b: Math.random(),
};
const guess = network.run(color)[0];
const guesColor = guess > 0.5 ? 'white' : 'black';
guessElem.style.color = guesColor
colorElem.style.backgroundColor = `rgba(${color.r * 255}, ${color.g * 255}, ${color.b * 255})`;
guessElem.innerHTML = `Text color should be ${guesColor}.`;
}
function chooseColor(value) {
contents.push({
input: color,
output: [value],
});
setRandomColor();
}
function onSave() {
console.log('DATA', contents);
defaultData = {
data: contents,
};
fs.writeFile('/learned.json', JSON.stringify(defaultData), function(err) {
if(err) {
console.log('ERROR WRITING', err);
}
});
setRandomColor();
}