Skip to content
This repository was archived by the owner on Mar 17, 2020. It is now read-only.

Commit 2339601

Browse files
committed
Use color picker
1 parent 3bd63ce commit 2339601

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

static/bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"iron-list": "polymerelements/iron-list#~1.2.7",
2424
"paper-spinner": "polymerelements/paper-spinner#~1.1.1",
2525
"platinum-sw": "polymerelements/platinum-sw#~1.2.3",
26-
"paper-toast": "polymerelements/paper-toast#~1.2.1"
26+
"paper-toast": "polymerelements/paper-toast#~1.2.1",
27+
"paper-color-picker": "~0.9.2"
2728
}
2829
}

static/index.html

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<script src="bower_components/webcomponentsjs/webcomponents-lite.js"></script>
66
<link rel="import" href="bower_components/polymer/polymer.html">
77
<link rel="import" href="bower_components/paper-button/paper-button.html">
8-
<link rel="import" href="bower_components/paper-input/paper-input.html">
8+
<link rel="import" href="bower_components/paper-color-picker/paper-color-input.html">
99
<link rel="import" href="bower_components/paper-spinner/paper-spinner.html">
1010
<link rel="import" href="bower_components/paper-toast/paper-toast.html">
1111
<link rel="import" href="bower_components/iron-list/iron-list.html">
@@ -57,10 +57,15 @@
5757
flex: auto;
5858
font-family: monospace;
5959
font-size: 8pt;
60+
@apply(--layout-horizontal);
6061
}
6162
.consoleLine:first-of-type {
6263
border-top: none !important;
6364
}
65+
.consoleLine .consoleColor {
66+
width: 20px;
67+
height: 20px;
68+
}
6469

6570
.error:last-child {
6671
border-bottom: 1px solid #ffd7d7;
@@ -144,17 +149,18 @@
144149
</div>
145150
<div id="update">(not updated yet)</div>
146151
<br></br>
147-
<paper-input label="LED color" placeholder="Type color name here" id="color" class="flex"></paper-input>
148-
<div class="horizontal layout end-justified">
149-
<paper-button id="submit" on-click="writeLedColor">Submit</paper-button>
150-
</div>
152+
<paper-color-input shape="circle" type="hsv" label="LED light color"
153+
id="color" class="flex"></paper-color-input>
151154
</div>
152155
<br></br>
153156
<div id="console" class="flex">
154157
<iron-list class="messages" items="[[consoleLines]]" as="item">
155158
<template>
156159
<div class$="consoleLine [[item.type]]">
157-
[[item.value]]
160+
<div class="flex">[[item.value]]</div>
161+
<template is="dom-if" if="[[item.color]]">
162+
<div class="consoleColor" style="[[item.color]]"></div>
163+
</template>
158164
</div>
159165
</template>
160166
</iron-list>
@@ -187,6 +193,7 @@
187193

188194
let style = window.getComputedStyle(document.body.appendChild(a));
189195
let colors = style.color.match(/\d+/g).map(a => parseInt(a, 10));
196+
console.log("Translated to hex color " + JSON.stringify(colors));
190197

191198
document.body.removeChild(a);
192199

@@ -253,8 +260,10 @@
253260
computeButtonTitle: function(server) {
254261
return server ? "Disconnect" : "Connect";
255262
},
256-
log: function(text) {
257-
this.push('consoleLines', { value: text, type: "log" });
263+
log: function(text, hexColor) {
264+
if (hexColor)
265+
hexColor = "background: " + hexColor + ";";
266+
this.push('consoleLines', { value: text, type: "log", color: hexColor });
258267
},
259268
error: function(text) {
260269
this.push('consoleLines', { value: text, type: "error" });
@@ -263,31 +272,43 @@
263272
this.consoleLines = [];
264273
},
265274
readLedColor: function() {
266-
return this.colorLedCharacteristic.readValue()
267-
.then(function(data) {
275+
if (!this.colorLedCharacteristic)
276+
return Promise.resolve();
277+
278+
return this.colorLedCharacteristic.readValue().then(data => {
268279
data = data.buffer ? data : new DataView(data);
269280
let r = data.getUint8(0).toString(16).padStart(2, '0');
270281
let g = data.getUint8(1).toString(16).padStart(2, '0');
271282
let b = data.getUint8(2).toString(16).padStart(2, '0');
272-
document.querySelector('#color').value = "#" + r + g + b;
283+
284+
// Ignore events from changing the color.
285+
this.reading = true;
286+
let value = "#" + r + g + b;
287+
this.log("Received LED color: " + value, value);
288+
document.querySelector('#color').valueAsHex = value;
289+
this.reading = false;
273290
});
274291
},
275-
writeLedColor: function() {
276-
this.$.submit.style.backgroundColor = "#646FB7";
292+
writeLedColor: function(hexValue) {
293+
if (this.writing || this.reading)
294+
return;
295+
296+
this.writing = true;
277297

278298
let done = () => {
279-
this.$.submit.style.backgroundColor = "#303F9F";
299+
this.writing = false;
280300
this.readLedColor();
281301
}
282302

283303
if (!this.colorLedCharacteristic) {
284-
this.error("Error: No Color Characteristic found.")
304+
this.error("Error: No Color Characteristic found.");
285305
return done();
286306
}
287307

288-
let value = document.querySelector('#color').value;
308+
let value = toHexColor(hexValue);
309+
this.log("Sending LED color: " + hexValue, hexValue);
289310

290-
return this.colorLedCharacteristic.writeValue(toHexColor(value))
311+
return this.colorLedCharacteristic.writeValue(value)
291312
.catch(err => { this.error(err); })
292313
.then(done);
293314
},
@@ -388,9 +409,11 @@
388409
});
389410
},
390411
ready: function() {
391-
document.querySelector('#color').addEventListener('keydown', ev => {
392-
if (ev.keyCode === 13)
393-
this.writeLedColor();
412+
let picker = document.querySelector('#color');
413+
picker.addEventListener('value-changed', ev => {
414+
let hexValue = picker.valueAsHex;
415+
if (hexValue)
416+
this.writeLedColor(hexValue);
394417
});
395418
}
396419
});

0 commit comments

Comments
 (0)