colorpicker: keep internal HSV

fixes #372
This commit is contained in:
tophf 2018-04-17 22:35:23 +03:00
parent 58f4fbec41
commit f7f679bbf9

View File

@ -16,6 +16,7 @@
let HSV = {}; let HSV = {};
let currentFormat; let currentFormat;
const prevHSV = {};
let initialized = false; let initialized = false;
let shown = false; let shown = false;
@ -175,6 +176,9 @@
Object.defineProperty($inputs.rgb, 'color', {get: inputsToRGB}); Object.defineProperty($inputs.rgb, 'color', {get: inputsToRGB});
Object.defineProperty($inputs.hsl, 'color', {get: inputsToHSL}); Object.defineProperty($inputs.hsl, 'color', {get: inputsToHSL});
Object.defineProperty($inputs, 'color', {get: () => $inputs[currentFormat].color}); Object.defineProperty($inputs, 'color', {get: () => $inputs[currentFormat].color});
Object.defineProperty($inputs, 'colorString', {
get: () => currentFormat && colorConverter.format($inputs[currentFormat].color)
});
HUE_COLORS.forEach(color => Object.assign(color, colorConverter.parse(color.hex))); HUE_COLORS.forEach(color => Object.assign(color, colorConverter.parse(color.hex)));
@ -328,6 +332,9 @@
const formats = ['hex', 'rgb', 'hsl']; const formats = ['hex', 'rgb', 'hsl'];
const dir = shiftKey ? -1 : 1; const dir = shiftKey ? -1 : 1;
const total = formats.length; const total = formats.length;
if ($inputs.colorString === $inputs.prevColorString) {
Object.assign(HSV, prevHSV);
}
switchInputGroup(formats[(formats.indexOf(currentFormat) + dir + total) % total]); switchInputGroup(formats[(formats.indexOf(currentFormat) + dir + total) % total]);
renderInputs(); renderInputs();
} }
@ -516,7 +523,18 @@
$opacityBar.style.background = 'linear-gradient(to right,' + $opacityBar.style.background = 'linear-gradient(to right,' +
colorToString(Object.assign(rgb, {a: 0}), 'rgb') + ',' + colorToString(Object.assign(rgb, {a: 0}), 'rgb') + ',' +
colorToString(Object.assign(rgb, {a: 1}), 'rgb') + ')'; colorToString(Object.assign(rgb, {a: 1}), 'rgb') + ')';
colorpickerCallback(); colorpickerCallback();
const colorString = $inputs.colorString;
if ($inputs.prevColorString === colorString) {
// keep the internal HSV calculated initially for this color format
Object.assign(HSV, prevHSV);
} else {
// remember the internal HSV
$inputs.prevColorString = colorString;
Object.assign(prevHSV, HSV);
}
} }
//endregion //endregion