-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorSelector.js
More file actions
70 lines (58 loc) · 2.14 KB
/
colorSelector.js
File metadata and controls
70 lines (58 loc) · 2.14 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
// This file is required by the color-selector.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
// TODO: Refactor common code with paletteView.js
const {ipcRenderer} = require('electron');
const cmn = require('./common.js');
const domu = require('./domUtils.js');
let _palette = []; // Current palette colors.
let _palIndex = 0; // Index of currently selected palette color.
let _palElems; // Array of DOM elements the palette.
function paletteIndex(id) {
return parseInt(id.substr(-1));
}
function setPalette(palette) {
_palette = palette;
_palette.forEach((p,i) => _palElems[i].style.backgroundColor = cmn.toCSSColorStr(p));
}
function selectPalette(palIndex) {
// Clear the old selected style.
_palElems[_palIndex].classList.remove('selected');
// And set the new selected style.
_palIndex = palIndex;
_palElems[_palIndex].classList.add('selected');
}
/**
* Sets the color of a palette.
* @param backgroundColor CSS color string in rgb() form.
*/
function setPaletteColor(palIndex, backgroundColor) {
_palette[palIndex] = cmn.toRGBA(backgroundColor);
_palElems[palIndex].style.backgroundColor = backgroundColor;
}
function onColorTableClicked(event) {
setPaletteColor(_palIndex, event.srcElement.style.backgroundColor);
ipcRenderer.send('palette-update', _palette);
}
function onPaletteColorClicked(event) {
const palIndex = paletteIndex(event.srcElement.id);
selectPalette(palIndex);
}
function init() {
// colorTable
document.getElementById('colorTable').addEventListener('click', onColorTableClicked);
// palette
// Must be reversed since they float right they are declared right-to-left.
_palElems = domu.getChildElements(document.getElementById('palette')).reverse();
_palElems.forEach(p => p.addEventListener('click', onPaletteColorClicked));
}
init();
/**
* Updates the palette being edited.
* @param palette Array of palette colors each color is an [r,g,b,a]
* @param palIndex Index of selected palette color.
*/
ipcRenderer.on('palette-update', function (event, palette, palIndex) {
setPalette(palette);
selectPalette(palIndex);
});