Skip to content

Commit 6864116

Browse files
yulunzdevtools-frontend-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
Better handle near hueless colors.
Bug:489704048 Change-Id: I61443c2bf3a39149d8336f922a70d7039e396abc Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/7750005 Reviewed-by: Philip Pfaffe <pfaffe@chromium.org> Commit-Queue: Yulun Zeng <yulunz@chromium.org>
1 parent 8fe6081 commit 6864116

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

front_end/core/common/Color.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ export interface Color {
684684

685685
const EPSILON = 0.01;
686686
const WIDE_RANGE_EPSILON = 1; // For comparisons on channels with a wider range than [0,1]
687+
const STRICT_EPSILON = 1e-4;
687688
function equals(a: number[], b: number[], accuracy?: number): boolean;
688689
function equals(a: number|null, b: number|null, accuracy?: number): boolean;
689690
function equals(a: number|null|number[], b: number|null|number[], accuracy = EPSILON): boolean {
@@ -994,7 +995,7 @@ export class LCH implements Color {
994995
// See "powerless" component definitions in
995996
// https://www.w3.org/TR/css-color-4/#specifying-lab-lch
996997
isHuePowerless(): boolean {
997-
return equals(this.c, 0);
998+
return equals(this.c, 0, STRICT_EPSILON);
998999
}
9991000
static fromSpec(spec: ColorParameterSpec, text: string): LCH|null {
10001001
const L = parsePercentage(spec[0], [0, 100]) ?? parseNumber(spec[0]);
@@ -1592,7 +1593,7 @@ export class HSL implements Color {
15921593
this.l = clamp(l, {min: 0, max: 1});
15931594
s = equals(this.l, 0) || equals(this.l, 1) ? 0 : s;
15941595
this.s = clamp(s, {min: 0, max: 1});
1595-
h = equals(this.s, 0) ? 0 : h;
1596+
h = equals(this.s, 0, STRICT_EPSILON) ? 0 : h;
15961597
this.h = normalizeHue(h * 360) / 360;
15971598
this.alpha = clamp(alpha ?? null, {min: 0, max: 1});
15981599
this.#authoredText = authoredText;

front_end/ui/legacy/components/color_picker/Spectrum.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,30 @@ describeWithEnvironment('ColorPicker aka Spectrum', () => {
7373
textInputs.forEach(decrement);
7474
assert.strictEqual(spectrum.color.asString(), 'rgb(127 253 0 / 29%)');
7575
});
76+
77+
it('updates hue correctly for near-grayscale colors', () => {
78+
const spectrum = new ColorPicker.Spectrum.Spectrum();
79+
const colorElement = spectrum.contentElement.querySelector('.spectrum-color') as HTMLElement;
80+
assert.exists(colorElement);
81+
82+
// Scenario 1: Input #3c3d3d only
83+
spectrum.setColor(Common.Color.parse('#3c3d3d') as Common.Color.Color);
84+
// Hue should be 0.5 (Cyan). getColorFromHsva returns rgb(0, 255, 255) for h=0.5, s=1, v=1.
85+
assert.strictEqual(colorElement.style.backgroundColor, 'rgb(0, 255, 255)');
86+
87+
// Scenario 2: Input #3d3d3d only
88+
spectrum.setColor(Common.Color.parse('#3d3d3d') as Common.Color.Color);
89+
// #3d3d3d should not be powerless, so it updates to its own calculated hue (0).
90+
// Hue 0 corresponds to Red.
91+
assert.strictEqual(colorElement.style.backgroundColor, 'rgb(255, 0, 0)');
92+
93+
// Scenario 3: First #3c3d3d and then #3d3d3d
94+
spectrum.setColor(Common.Color.parse('#3c3d3d') as Common.Color.Color);
95+
assert.strictEqual(colorElement.style.backgroundColor, 'rgb(0, 255, 255)');
96+
spectrum.setColor(Common.Color.parse('#3d3d3d') as Common.Color.Color);
97+
// Since it is not powerless, it should update and be Red.
98+
assert.strictEqual(colorElement.style.backgroundColor, 'rgb(255, 0, 0)');
99+
});
76100
});
77101

78102
describeWithMockConnection('PaletteGenerator', () => {

0 commit comments

Comments
 (0)