Skip to content

Commit d83b8ba

Browse files
bjarnefiOvergaard
andauthored
fix: Don't update saturation based on parsed color (#536)
* Don't update saturation based on parsed color * Workaround to update color area when hue slider change * Fix typo * Don't change opacity of gradient in opacity-slider as value change and value itself may contain alpha * Make handle opacity based on alpha * Clear console * Clear console * Clear console * Add comment * Remove output of hue value * Update saturation * Don't update saturation * Remove commented line * Remove unused parameter * Run linter --------- Co-authored-by: Jacob Overgaard <[email protected]>
1 parent b06aebb commit d83b8ba

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

packages/uui-color-area/lib/uui-color-area.element.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,16 @@ export class UUIColorAreaElement extends LitElement {
9797
const parsed = colord(newVal);
9898

9999
if (parsed.isValid()) {
100-
const { h, s, l } = parsed.toHsl();
100+
const { h, l, a } = parsed.toHsl();
101+
102+
// Update hue from parsed color, but when color is black, value from hue slider may be different from zero.
103+
if (h !== 0) {
104+
this.hue = h;
105+
}
101106

102-
this.hue = h;
103-
this.saturation = s;
104107
this.lightness = l;
105108
this.brightness = this.getBrightness(l);
109+
this.alpha = a * 100;
106110
}
107111
} catch (e) {
108112
// TODO: Should we log this?

packages/uui-color-picker/lib/uui-color-picker.element.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,24 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
360360
}
361361

362362
setColor(colorString: string | HslaColor) {
363-
this._colord = new Colord(colorString);
363+
const colord = new Colord(colorString);
364364

365-
const { h, l, s, a } = this._colord.toHsl();
365+
const { h, s, l, a } = colord.toHsl();
366366

367367
this.hue = h;
368368
this.saturation = s;
369369
this.lightness = l;
370370
this.alpha = this.opacity ? a * 100 : 100;
371371

372+
const hslaColor = colorString as HslaColor;
373+
374+
// Workaround as hue isn't correct after changing hue slider, but Colord parse hue value as zero when color is black.
375+
if (hslaColor && hslaColor.h) {
376+
this.hue = hslaColor.h;
377+
}
378+
379+
this._colord = colord;
380+
372381
this._syncValues();
373382

374383
this.dispatchEvent(
@@ -384,6 +393,23 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
384393
return this.uppercase ? string.toUpperCase() : string.toLowerCase();
385394
}
386395

396+
/** Generates a hex string from HSL values. Hue must be 0-360. All other arguments must be 0-100. */
397+
private getHexString(
398+
hue: number,
399+
saturation: number,
400+
lightness: number,
401+
alpha = 100
402+
) {
403+
const color = colord(
404+
`hsla(${hue}, ${saturation}%, ${lightness}%, ${alpha / 100})`
405+
);
406+
if (!color.isValid()) {
407+
return '';
408+
}
409+
410+
return color.toHex();
411+
}
412+
387413
private _syncValues() {
388414
this.inputValue = this.getFormattedValue(this.format);
389415
this.value = this.inputValue;
@@ -400,6 +426,7 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
400426
aria-disabled=${this.disabled ? 'true' : 'false'}>
401427
<uui-color-area
402428
.value="${this.value}"
429+
.hue="${Math.round(this.hue)}"
403430
?disabled=${this.disabled}
404431
@change=${this.handleGridChange}>
405432
</uui-color-area>
@@ -419,7 +446,11 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
419446
class="opacity-slider"
420447
.value=${Math.round(this.alpha)}
421448
type="opacity"
422-
.color=${this.value}
449+
.color=${this.getHexString(
450+
this.hue,
451+
this.saturation,
452+
this.lightness
453+
)}
423454
?disabled=${this.disabled}
424455
@change=${this.handleAlphaChange}>
425456
</uui-color-slider>

packages/uui-color-slider/lib/uui-color-slider.element.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class UUIColorSliderElement extends LabelMixin('label', LitElement) {
217217
this.dispatchEvent(new UUIColorSliderEvent(UUIColorSliderEvent.CHANGE));
218218
}
219219

220-
get ccsPropCurrentValue() {
220+
get cssPropCurrentValue() {
221221
return this.value === 0
222222
? this.vertical
223223
? 100
@@ -257,7 +257,7 @@ export class UUIColorSliderElement extends LabelMixin('label', LitElement) {
257257
<!-- <slot name="detail"> </slot> -->
258258
<span
259259
id="color-slider__handle"
260-
style="--current-value: ${this.ccsPropCurrentValue}%;"
260+
style="--current-value: ${this.cssPropCurrentValue}%;"
261261
tabindex=${ifDefined(this.disabled ? undefined : '0')}></span>
262262
</div>
263263
${Math.round(this.value)}`;

0 commit comments

Comments
 (0)