Skip to content

Commit

Permalink
refactor(color-picker): tidy up slider width logic (#9568)
Browse files Browse the repository at this point in the history
**Related Issue:** N/A

## Summary

✨🧹✨
  • Loading branch information
jcfranco committed Jun 14, 2024
1 parent 137d9ae commit 04aff9c
Showing 1 changed file with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ export class ColorPicker
}
}

@Watch("alphaChannel")
@Watch("dimensions")
handleAlphaChannelDimensionsChange(): void {
this.effectiveSliderWidth = getSliderWidth(this.dimensions, this.alphaChannel);
this.drawColorControls();
}

/** When `true`, hides the RGB/HSV channel inputs. */
@Prop() channelsDisabled = false;

Expand Down Expand Up @@ -313,6 +320,8 @@ export class ColorPicker

private colorFieldScopeNode: HTMLDivElement;

private effectiveSliderWidth: number;

private hueSliderRenderingContext: CanvasRenderingContext2D;

private hueScopeNode: HTMLDivElement;
Expand All @@ -331,12 +340,12 @@ export class ColorPicker

private shiftKeyChannelAdjustment = 0;

@State() defaultMessages: ColorPickerMessages;

@State() channelMode: ColorMode = "rgb";

@State() channels: Channels = this.toChannels(DEFAULT_COLOR);

@State() defaultMessages: ColorPickerMessages;

@State() dimensions = DIMENSIONS.m;

@State() effectiveLocale = "";
Expand Down Expand Up @@ -683,6 +692,7 @@ export class ColorPicker
setUpLoadableComponent(this);

this.handleAllowEmptyOrClearableChange();
this.handleAlphaChannelDimensionsChange();

const { isClearable, color, format, value } = this;
const willSetNoColor = isClearable && !value;
Expand Down Expand Up @@ -758,8 +768,8 @@ export class ColorPicker
scale,
scopeOrientation,
} = this;
const sliderWidth = this.getSliderWidth();

const sliderWidth = this.effectiveSliderWidth;
const selectedColorInHex = color ? hexify(color, alphaChannel) : null;
const hueTop = thumbRadius;
const hueLeft = hueScopeLeft ?? (sliderWidth * DEFAULT_COLOR.hue()) / HSV_LIMITS.h;
Expand Down Expand Up @@ -1110,15 +1120,13 @@ export class ColorPicker
}

private captureHueSliderColor(x: number): void {
const width = this.getSliderWidth();
const hue = (HUE_LIMIT_CONSTRAINED / width) * x;
const hue = (HUE_LIMIT_CONSTRAINED / this.effectiveSliderWidth) * x;

this.internalColorSet(this.baseColorFieldColor.hue(hue), false);
}

private captureOpacitySliderValue(x: number): void {
const width = this.getSliderWidth();
const alpha = opacityToAlpha((OPACITY_LIMITS.max / width) * x);
const alpha = opacityToAlpha((OPACITY_LIMITS.max / this.effectiveSliderWidth) * x);

this.internalColorSet(this.baseColorFieldColor.alpha(alpha), false);
}
Expand Down Expand Up @@ -1347,7 +1355,7 @@ export class ColorPicker
}

const adjustedSliderDimensions = {
width: this.getSliderWidth(),
width: this.effectiveSliderWidth,
height:
dimensions.slider.height + (dimensions.thumb.radius - dimensions.slider.height / 2) * 2,
};
Expand Down Expand Up @@ -1446,8 +1454,7 @@ export class ColorPicker
},
} = this;

const width = this.getSliderWidth();

const width = this.effectiveSliderWidth;
const x = hsvColor.hue() / (HUE_LIMIT_CONSTRAINED / width);
const y = radius;
const sliderBoundX = this.getSliderBoundX(x, width, radius);
Expand All @@ -1459,10 +1466,6 @@ export class ColorPicker
this.drawThumb(this.hueSliderRenderingContext, radius, sliderBoundX, y, hsvColor, false);
}

private getSliderWidth(): number {
return getSliderWidth(this.dimensions, this.alphaChannel);
}

private drawHueSlider(): void {
const context = this.hueSliderRenderingContext;
const {
Expand All @@ -1474,7 +1477,7 @@ export class ColorPicker

const x = 0;
const y = thumbRadius - height / 2;
const width = this.getSliderWidth();
const width = this.effectiveSliderWidth;

const gradient = context.createLinearGradient(0, 0, width, 0);

Expand Down Expand Up @@ -1522,7 +1525,7 @@ export class ColorPicker

const x = 0;
const y = thumbRadius - height / 2;
const width = this.getSliderWidth();
const width = this.effectiveSliderWidth;

context.clearRect(0, 0, width, height + this.getSliderCapSpacing() * 2);

Expand Down Expand Up @@ -1608,7 +1611,7 @@ export class ColorPicker
},
} = this;

const width = this.getSliderWidth();
const width = this.effectiveSliderWidth;
const x = alphaToOpacity(hsvColor.alpha()) / (OPACITY_LIMITS.max / width);
const y = radius;
const sliderBoundX = this.getSliderBoundX(x, width, radius);
Expand Down

0 comments on commit 04aff9c

Please sign in to comment.