-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
script.js
187 lines (167 loc) · 5.46 KB
/
script.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
let config = {
url: 'http://192.168.1.225',
};
let currentColors = {};
let rgbBrightnessChange = false;
$(document).ready(function() {
// Cache buster added because caching was a big problem on mobile
let cacheBuster = new Date().getTime();
// btnStatus();
getLEDStatus('rgb');
getLEDStatus('white');
// RGB Slider
let slider = document.getElementById('slider');
// White Slider
let wSlider = document.getElementById('whiteSlider');
const pickr = Pickr.create({
el: '.color-picker',
theme: 'classic', // or 'monolith', or 'nano'
lockOpacity: true,
padding: 15,
inline: true,
swatches: [
'rgba(255, 0, 0, 1)',
'rgba(255, 82, 0, 1)',
'rgba(0, 255, 0, 1)',
'rgba(0, 0, 255, 1)',
'rgba(27, 161, 17, 1)',
'rgba(255, 255, 0, 1)',
'rgba(255, 0, 255, 1)',
'rgba(108, 16, 157, 1)',
'rgba(0, 255, 255, 1)',
'rgba(24, 139, 167, 1)',
'rgba(255, 255, 255, 1)',
'rgba(0, 0, 0, 1)',
],
components: {
// Main components
preview: true,
opacity: false,
hue: true,
// Input / output Options
interaction: {
hex: true,
rgba: true,
// hsla: true,
// hsva: true,
// cmyk: true,
input: true,
// clear: true,
save: true
}
}
});
pickr.off().on('swatchselect', e => {
// sendData(e); // Swatchselect apparently triggers save so it triggers sendData() automatically
pickr.setColor(e.toRGBA().toString(0));
});
pickr.on('save', e => {
// If 'save' is being triggered by brightness changes instead
if(rgbBrightnessChange == false) {
let tempColors = pickr.getColor().toRGBA();
currentColors.red = Math.floor(tempColors[0]);
currentColors.green = Math.floor(tempColors[1]);
currentColors.blue = Math.floor(tempColors[2]);
slider.noUiSlider.set(100); // sets slider value to 100 if color is changed manually
$('#slider .noUi-connect').css('background', `rgb(${currentColors.red}, ${currentColors.green}, ${currentColors.blue}`);
} else {
rgbBrightnessChange = false;
}
sendData(e);
});
noUiSlider.create(slider, {
behavior: "tap",
start: [100],
connect: [true, false],
// direction: 'rtl',
step: 5,
range: {
'min': [0],
'max': [100]
},
pips: {
mode: 'values',
values: [0, 25, 50, 75, 100],
density: 5,
format: wNumb({
decimals: 0,
postfix: "%"
})
}
});
slider.noUiSlider.on('set', function(e) {
let sliderVal = (slider.noUiSlider.get()/100);
let newRed = Math.floor(currentColors.red * sliderVal);
let newGreen = Math.floor(currentColors.green * sliderVal);
let newBlue = Math.floor(currentColors.blue * sliderVal);
rgbBrightnessChange = true;
pickr.setColor(`rgb(${newRed}, ${newGreen}, ${newBlue})`);
});
function sendData(e){
let obj = e.toRGBA();
let red = Math.floor(obj[0]);
let green = Math.floor(obj[1]);
let blue = Math.floor(obj[2]);
let queryBuilder = `red=${red}&green=${green}&blue=${blue}`;
$.ajax({
url: `${config.url}/api/lr/?${queryBuilder}&${cacheBuster}`,
method: 'GET',
dataType: 'json',
cache: false,
success: function (result) {
// console.log(result);
// console.log(currentColors);
}
});
}
function changeWhiteLed(frequency){
$.ajax({
url: `${config.url}/api/lr/white?white=${frequency}&${cacheBuster}`,
method: 'GET',
success: function(result) {
console.log(result);
}
});
}
noUiSlider.create(wSlider, {
behavior: "tap",
start: [100],
connect: [false, true],
step: 5,
range: {
'min': [0],
'max': [100]
},
pips: {
mode: 'values',
values: [0, 25, 50, 75, 100],
density: 5,
format: wNumb({
decimals: 0,
postfix: "%"
})
}
});
wSlider.noUiSlider.on('change', function(e) {
let sliderVal = (wSlider.noUiSlider.get()/100);
changeWhiteLed(Math.floor(sliderVal * 255));
});
// Get RGB Status so Color Picker in UI is set to that color on page load
function getLEDStatus(color) {
$.ajax({
url: `${config.url}/api/lr/getStatus?colors=${color}&${cacheBuster}`,
method: 'GET',
success: function(result) {
if(color == 'rgb') {
let colors = `rgb(${result.red}, ${result.green}, ${result.blue})`;
currentColors.red = result.red;
currentColors.green = result.green;
currentColors.blue = result.blue;
pickr.setColor(colors);
} else {
wSlider.noUiSlider.set(Math.floor((result.white / 255) * 100));
}
},
});
}
});