-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(color-picker): draw slider thumbs within bounds #7398
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,7 @@ import { | |
LocalizedComponent, | ||
NumberingSystem, | ||
} from "../../utils/locale"; | ||
import { clamp } from "../../utils/math"; | ||
import { clamp, remap } from "../../utils/math"; | ||
import { | ||
connectMessages, | ||
disconnectMessages, | ||
|
@@ -619,7 +619,7 @@ export class ColorPicker | |
} else if (clientX < bounds.x) { | ||
samplingX = 0; | ||
} else { | ||
samplingX = bounds.width - 1; | ||
samplingX = bounds.width; | ||
} | ||
|
||
if (clientY < bounds.y + bounds.height && clientY > bounds.y) { | ||
|
@@ -1095,7 +1095,7 @@ export class ColorPicker | |
slider: { width }, | ||
}, | ||
} = this; | ||
const hue = (360 / width) * x; | ||
const hue = ((HSV_LIMITS.h - 1) / width) * x; | ||
|
||
this.internalColorSet(this.baseColorFieldColor.hue(hue), false); | ||
} | ||
|
@@ -1385,7 +1385,6 @@ export class ColorPicker | |
const startAngle = 0; | ||
const endAngle = 2 * Math.PI; | ||
const outlineWidth = 1; | ||
radius = radius - outlineWidth; | ||
|
||
context.beginPath(); | ||
context.arc(x, y, radius, startAngle, endAngle); | ||
|
@@ -1417,14 +1416,15 @@ export class ColorPicker | |
}, | ||
} = this; | ||
|
||
const x = hsvColor.hue() / (360 / width); | ||
const x = hsvColor.hue() / ((HSV_LIMITS.h - 1) / width); | ||
const y = radius - height / 2 + height / 2; | ||
const sliderBoundX = remap(x, 0, width, radius, width - radius); | ||
|
||
requestAnimationFrame(() => { | ||
this.hueScopeLeft = x; | ||
this.hueScopeLeft = sliderBoundX; | ||
}); | ||
|
||
this.drawThumb(this.hueSliderRenderingContext, radius, x, y, hsvColor); | ||
this.drawThumb(this.hueSliderRenderingContext, radius, sliderBoundX, y, hsvColor); | ||
} | ||
|
||
private drawHueSlider(): void { | ||
|
@@ -1441,7 +1441,15 @@ export class ColorPicker | |
|
||
const gradient = context.createLinearGradient(0, 0, width, 0); | ||
|
||
const hueSliderColorStopKeywords = ["red", "yellow", "lime", "cyan", "blue", "magenta", "red"]; | ||
const hueSliderColorStopKeywords = [ | ||
"red", | ||
"yellow", | ||
"lime", | ||
"cyan", | ||
"blue", | ||
"magenta", | ||
"#ff0004" /* 1 unit less than #ff0 to avoid duplicate values within range */, | ||
]; | ||
|
||
const offset = 1 / (hueSliderColorStopKeywords.length - 1); | ||
let currentOffset = 0; | ||
|
@@ -1565,12 +1573,13 @@ export class ColorPicker | |
|
||
const x = alphaToOpacity(hsvColor.alpha()) / (OPACITY_LIMITS.max / width); | ||
const y = radius; | ||
const sliderBoundX = remap(x, 0, width, radius, width - radius); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will cause the thumb to not center on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed w/ @anveshmekala that there's an issue when sampling colors individually vs sliding. Thanks for clarifying! I'll look into only mapping if the x lies within the slider edges. |
||
|
||
requestAnimationFrame(() => { | ||
this.opacityScopeLeft = x; | ||
this.opacityScopeLeft = sliderBoundX; | ||
}); | ||
|
||
this.drawThumb(this.opacitySliderRenderingContext, radius, x, y, hsvColor); | ||
this.drawThumb(this.opacitySliderRenderingContext, radius, sliderBoundX, y, hsvColor); | ||
} | ||
|
||
private storeOpacityScope = (node: HTMLDivElement): void => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: can we assign
1
to a self explanatory constant since its is used in other places or have a property which storesHSV_LIMITS.h -1