Skip to content

Commit

Permalink
fix: ensure color wheel in not opinionated about saturation and light…
Browse files Browse the repository at this point in the history
…ness
  • Loading branch information
Westbrook committed Apr 7, 2021
1 parent 4c7a124 commit 8e0fd9c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/color-slider/test/color-slider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,11 @@ describe('ColorSlider', () => {
expect(el.sliderHandlePosition).to.equal(0);
});
it('accepts pointer events', async () => {
const color = new TinyColor({ h: '0', s: '20%', l: '70%' });
const el = await fixture<ColorSlider>(
html`
<sp-color-slider
.color=${color}
style="--spectrum-colorslider-default-length: 192px; --spectrum-colorslider-default-height: 24px; --spectrum-colorslider-default-height: 24px;"
></sp-color-slider>
`
Expand All @@ -231,6 +233,8 @@ describe('ColorSlider', () => {
};

expect(el.sliderHandlePosition).to.equal(0);
expect((el.color as HSLA).s).to.be.within(0.19, 0.21);
expect((el.color as HSLA).l).to.be.within(0.69, 0.71);

handle.dispatchEvent(
new PointerEvent('pointerdown', {
Expand All @@ -246,6 +250,8 @@ describe('ColorSlider', () => {

await elementUpdated(el);
expect(el.sliderHandlePosition).to.equal(0);
expect((el.color as HSLA).s).to.be.within(0.19, 0.21);
expect((el.color as HSLA).l).to.be.within(0.69, 0.71);

const root = el.shadowRoot ? el.shadowRoot : el;
const gradient = root.querySelector('.gradient') as HTMLElement;
Expand All @@ -263,6 +269,8 @@ describe('ColorSlider', () => {

await elementUpdated(el);
expect(el.sliderHandlePosition).to.equal(0);
expect((el.color as HSLA).s).to.be.within(0.19, 0.21);
expect((el.color as HSLA).l).to.be.within(0.69, 0.71);

gradient.dispatchEvent(
new PointerEvent('pointerdown', {
Expand All @@ -278,6 +286,8 @@ describe('ColorSlider', () => {
await elementUpdated(el);

expect(el.sliderHandlePosition).to.equal(47.91666666666667);
expect((el.color as HSLA).s).to.be.within(0.19, 0.21);
expect((el.color as HSLA).l).to.be.within(0.69, 0.71);

handle.dispatchEvent(
new PointerEvent('pointermove', {
Expand All @@ -303,6 +313,8 @@ describe('ColorSlider', () => {
await elementUpdated(el);

expect(el.sliderHandlePosition).to.equal(53.125);
expect((el.color as HSLA).s).to.be.within(0.19, 0.21);
expect((el.color as HSLA).l).to.be.within(0.69, 0.71);
});
it('accepts pointer events while [vertical]', async () => {
const el = await fixture<ColorSlider>(
Expand Down
3 changes: 2 additions & 1 deletion packages/color-wheel/src/ColorWheel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export class ColorWheel extends Focusable {
break;
}
this.value = (360 + this.value + delta) % 360;
this._color = new TinyColor({ ...this._color.toHsl(), h: this.value });
}

private handleKeyup(event: KeyboardEvent): void {
Expand Down Expand Up @@ -235,7 +236,7 @@ export class ColorWheel extends Focusable {

private handlePointermove(event: PointerEvent): void {
this.value = this.calculateHandlePosition(event);
this._color = new TinyColor({ h: this.value, s: '100%', l: '50%' });
this._color = new TinyColor({ ...this._color.toHsl(), h: this.value });

this.dispatchEvent(
new Event('input', {
Expand Down
12 changes: 12 additions & 0 deletions packages/color-wheel/test/color-wheel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,11 @@ describe('ColorWheel', () => {
expect(el.value).to.equal(0);
});
it('accepts pointer events', async () => {
const color = new TinyColor({ h: '0', s: '20%', l: '70%' });
const el = await fixture<ColorWheel>(
html`
<sp-color-wheel
.color=${color}
style="--spectrum-global-dimension-size-125: 10px;"
></sp-color-wheel>
`
Expand All @@ -229,6 +231,8 @@ describe('ColorWheel', () => {
};

expect(el.value).to.equal(0);
expect((el.color as HSLA).s).to.be.within(0.19, 0.21);
expect((el.color as HSLA).l).to.be.within(0.69, 0.71);

handle.dispatchEvent(
new PointerEvent('pointerdown', {
Expand All @@ -245,6 +249,8 @@ describe('ColorWheel', () => {
await elementUpdated(el);

expect(el.value).to.equal(0);
expect((el.color as HSLA).s).to.be.within(0.19, 0.21);
expect((el.color as HSLA).l).to.be.within(0.69, 0.71);

const root = el.shadowRoot ? el.shadowRoot : el;
const gradient = root.querySelector('[name="gradient"]') as HTMLElement;
Expand All @@ -263,6 +269,8 @@ describe('ColorWheel', () => {
await elementUpdated(el);

expect(el.value).to.equal(0);
expect((el.color as HSLA).s).to.be.within(0.19, 0.21);
expect((el.color as HSLA).l).to.be.within(0.69, 0.71);

gradient.dispatchEvent(
new PointerEvent('pointerdown', {
Expand All @@ -278,6 +286,8 @@ describe('ColorWheel', () => {
await elementUpdated(el);

expect(el.value).to.equal(263.74596725608353);
expect((el.color as HSLA).s).to.be.within(0.19, 0.21);
expect((el.color as HSLA).l).to.be.within(0.69, 0.71);

handle.dispatchEvent(
new PointerEvent('pointermove', {
Expand All @@ -303,6 +313,8 @@ describe('ColorWheel', () => {
await elementUpdated(el);

expect(el.value).to.equal(96.34019174590992);
expect((el.color as HSLA).s).to.be.within(0.19, 0.21);
expect((el.color as HSLA).l).to.be.within(0.69, 0.71);
});
const colorFormats: {
name: string;
Expand Down

0 comments on commit 8e0fd9c

Please sign in to comment.