chart = {
const wrapper = d3.create('div')
.style('height', '353px')
.style('width', '355px')
.style('padding', '5px')
.style('display', 'flex')
.style('flex-wrap', 'wrap')
.style('justify-content', 'left')
.style('background-color', 'lightgrey')
.style('border', '1px solid darkgrey')
let colorSelected = null,
colorPrev = null,
hexSelected = null;
const updateSelection = () => {
updateColor();
colorSelected.style('background-color', selected.hex());
hexSelected.property('value', selected.hex().toString());
}
const updateFromHex = function() {
const maybeHex = this.value;
if (/^#([0-9A-F]{3}){1,2}$/i.test(maybeHex)) {
const newColor = d3.hsl(maybeHex);
selected.h = newColor.h, selected.s = newColor.s, selected.l = newColor.l;
channels.h = selected.h
colorSelected.style('background-color', selected.hex());
picker.call(renderPicker);
}
}
const colorDrag = function() {
const newCoords = d3.mouse(this);
coords[0] = Math.max(Math.min(newCoords[0], len), 0),
coords[1] = Math.max(Math.min(newCoords[1], len), 0);
updateSelection();
}
const spectrumDrag = function() {
const y = d3.mouse(this)[1];
channels.h = scales.h(y);
picker.call(renderPicker);
updateSelection();
}
const revert = () => {
const c = colorPrev.style('background-color');
const newColor = d3.hsl(c);
selected.h = newColor.h, selected.s = newColor.s, selected.l = newColor.l;
channels.h = selected.h
colorSelected.style('background-color', selected.hex());
hexSelected.property('value', selected.hex().toString());
picker.call(renderPicker);
}
const commit = () => {
previous.h = selected.h, previous.s = selected.s, previous.l = selected.l;
colorPrev.style('background-color', previous.hex());
}
const picker = wrapper
.append('div')
.style('width', `${len}px`)
.style('height', `${len}px`)
.style('margin', '5px')
.style('cursor', 'crosshair')
.style('border', '1px solid darkgrey')
.style('padding', '1px')
.append('canvas')
.attr('width', len)
.attr('height', len)
.call(renderPicker)
.call(d3.drag()
.on('start', colorDrag)
.on('drag', colorDrag));
const spectrum = wrapper
.append('div')
.style('width', '25px')
.style('height', `${len}px`)
.style('margin', '5px')
.style('cursor', 'crosshair')
.style('border', '1px solid darkgrey')
.style('padding', '1px')
.append('canvas')
.attr('width', 1)
.attr('height', `${len}`)
.style('width', '25px')
.style('height', `${len}px`)
.call(renderSpectrum)
.call(d3.drag()
.on('start', spectrumDrag)
.on('drag', spectrumDrag));
const swatches = wrapper
.append('div')
.style('margin', '5px')
.style('padding', '1px')
.style('display', 'flex')
.style('border', '1px solid darkgrey');
colorSelected = swatches
.append('div')
.style('width', '50px')
.style('height', '25px')
.style('background-color', selected.hex());
colorPrev = swatches
.append('div')
.style('width', '50px')
.style('height', '25px')
.style('background-color', previous.hex())
.style('cursor', 'pointer')
.on('click', revert);
hexSelected = wrapper
.append('input')
.style('width', '55px')
.style('height', '22px')
.style('margin', '5px')
.property('value', selected.hex().toString())
.on('input', updateFromHex);
const okInput = wrapper
.append('button')
.text('OK')
.style('width', '70px')
.style('height', '28px')
.style('margin', '5px')
.style('flex-grow', 1)
.on('click', commit);
const cancelInput = wrapper
.append('button')
.text('Cancel')
.style('width', '70px')
.style('height', '28px')
.style('margin', '5px')
.style('flex-grow', 1)
.on('click', revert);
return Object.assign(wrapper.node());
}