2 class SettingAppColorPicker {
6 this.colorInput = elem.querySelector('input[type=color]');
7 this.lightColorInput = elem.querySelector('input[name="setting-app-color-light"]');
8 this.resetButton = elem.querySelector('[setting-app-color-picker-reset]');
9 this.defaultButton = elem.querySelector('[setting-app-color-picker-default]');
11 this.colorInput.addEventListener('change', this.updateColor.bind(this));
12 this.colorInput.addEventListener('input', this.updateColor.bind(this));
13 this.resetButton.addEventListener('click', event => {
14 this.colorInput.value = this.colorInput.dataset.current;
17 this.defaultButton.addEventListener('click', event => {
18 this.colorInput.value = this.colorInput.dataset.default;
24 * Update the app colors as a preview, and create a light version of the color.
27 const hexVal = this.colorInput.value;
28 const rgb = this.hexToRgb(hexVal);
29 const rgbLightVal = 'rgba('+ [rgb.r, rgb.g, rgb.b, '0.15'].join(',') +')';
31 this.lightColorInput.value = rgbLightVal;
33 const customStyles = document.getElementById('custom-styles');
34 const oldColor = customStyles.getAttribute('data-color');
35 const oldColorLight = customStyles.getAttribute('data-color-light');
37 customStyles.innerHTML = customStyles.innerHTML.split(oldColor).join(hexVal);
38 customStyles.innerHTML = customStyles.innerHTML.split(oldColorLight).join(rgbLightVal);
40 customStyles.setAttribute('data-color', hexVal);
41 customStyles.setAttribute('data-color-light', rgbLightVal);
45 * Covert a hex color code to rgb components.
46 * @attribution https://stackoverflow.com/a/5624139
51 const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
53 r: result ? parseInt(result[1], 16) : 0,
54 g: result ? parseInt(result[2], 16) : 0,
55 b: result ? parseInt(result[3], 16) : 0
61 export default SettingAppColorPicker;