Skip to content

Commit 1c3e934

Browse files
authored
AUI: Fixed an issue with overlay handling of transparent colors (#281)
1 parent cf620bb commit 1c3e934

File tree

8 files changed

+51
-8
lines changed

8 files changed

+51
-8
lines changed

.changeset/neat-plants-spend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@adaptive-web/adaptive-ui": patch
3+
---
4+
5+
Fixed an issue with overlay handling of transparent colors

packages/adaptive-ui-explorer/src/app.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
layerFillFixedMinus2,
1515
layerFillFixedMinus3,
1616
layerFillFixedPlus1,
17+
neutralAsOverlay,
1718
neutralBaseColor,
1819
neutralPalette,
1920
wcagContrastLevel
@@ -243,6 +244,9 @@ export class App extends FASTElement {
243244

244245
app.highlightPalette = highlightPalette.getValueFor(app.canvas);
245246
break;
247+
case "neutralAsOverlay":
248+
neutralAsOverlay.setValueFor(app.canvas, source.neutralAsOverlay);
249+
break;
246250
case "showOnlyLayerBackgrounds":
247251
app.updateBackgrounds();
248252
break;

packages/adaptive-ui-explorer/src/components/control-pane/control-pane.template.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export function controlPaneTemplate<T extends ControlPane>(): ElementViewTemplat
4040
/>
4141
</div>
4242
43+
<adaptive-switch
44+
id="neutralAsOverlay"
45+
:checked=${twoWay((x) => x.state.neutralAsOverlay)}
46+
>Neutral as overlay</adaptive-switch>
47+
4348
<adaptive-switch
4449
id="showOnlyLayerBackgrounds"
4550
:checked=${twoWay((x) => x.state.showOnlyLayerBackgrounds)}

packages/adaptive-ui-explorer/src/state.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const State = Context.create<State>("State");
88
export interface State {
99
componentType: ComponentType;
1010
neutralColor: Color;
11+
neutralAsOverlay: boolean;
1112
accentColor: Color;
1213
highlightColor: Color;
1314
showOnlyLayerBackgrounds: boolean;
@@ -31,6 +32,9 @@ export class DefaultState implements State {
3132
@observable
3233
public neutralColor: Color = PLACEHOLDER_COLOR;
3334

35+
@observable
36+
public neutralAsOverlay: boolean = false;
37+
3438
@observable
3539
public accentColor: Color = PLACEHOLDER_COLOR;
3640

packages/adaptive-ui/docs/api-report.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ export class Color extends PaintBase {
7575
}): Color;
7676
static fromRgb(r: number, g: number, b: number, alpha?: number): Color;
7777
protected readonly _intendedColor?: Color;
78+
static isTransparent(color: Color_2): boolean;
7879
static parse(color: string): Color | undefined;
7980
// @deprecated
8081
toColorString: () => string;
8182
toString(): string;
83+
static transparent: Color;
8284
static unsafeOpacity(color: Color, alpha: number): Color;
8385
}
8486

packages/adaptive-ui/src/core/color/color.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { cssDirective } from "@microsoft/fast-element";
22
import { type Color as CuloriColor, formatHex, formatRgb, modeLrgb, modeRgb, parse, type Rgb, useMode, wcagLuminance } from "culori/fn";
3+
import { parseTransparent } from "culori";
34
import { PaintBase } from "./paint.js";
45
import { calculateOverlayColor } from "./utilities/opacity.js";
56

67
useMode(modeRgb);
78
// For luminance
89
useMode(modeLrgb);
910

11+
const _transparent: Rgb = parseTransparent("transparent");
12+
1013
/**
1114
* Represents a color.
1215
*
@@ -44,6 +47,9 @@ export class Color extends PaintBase {
4447
* @returns The color value in string format
4548
*/
4649
public toString(): string {
50+
if (Color.isTransparent(this.color)) {
51+
return "transparent";
52+
}
4753
return this.color.alpha !== undefined && this.color.alpha < 1 ? formatRgb(this.color) : formatHex(this.color);
4854
}
4955

@@ -99,6 +105,21 @@ export class Color extends PaintBase {
99105
}
100106
}
101107

108+
/**
109+
* A Color representing the full transparent.
110+
*/
111+
public static transparent: Color = new Color(_transparent);
112+
113+
/**
114+
* Checks if a color is transparent.
115+
*
116+
* @param color - The color to check.
117+
* @returns True if the color is transparent, false otherwise.
118+
*/
119+
public static isTransparent(color: CuloriColor): boolean {
120+
return color.alpha === 0;
121+
}
122+
102123
/**
103124
* Creates a new Color as an overlay representation of the `intendedColor` over `reference`.
104125
*

packages/adaptive-ui/src/core/color/utilities/conditional.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { Color } from "../color.js";
22
import { InteractiveColorSet } from "../recipe.js";
3-
import { _white } from "./color-constants.js";
4-
5-
const _transparentWhite = Color.unsafeOpacity(_white, 0);
63

74
/**
85
* Return an interactive set of the provided tokens or a no-op "transparent" set of tokens.
@@ -20,10 +17,10 @@ export function conditionalSwatchSet(
2017
}
2118

2219
return {
23-
rest: _transparentWhite,
24-
hover: _transparentWhite,
25-
active: _transparentWhite,
26-
focus: _transparentWhite,
27-
disabled: _transparentWhite,
20+
rest: Color.transparent,
21+
hover: Color.transparent,
22+
active: Color.transparent,
23+
focus: Color.transparent,
24+
disabled: Color.transparent,
2825
};
2926
}

packages/adaptive-ui/src/core/color/utilities/opacity.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ function calcRgbOverlay(rgbMatch: Rgb, rgbBackground: Rgb, rgbOverlay: Rgb): num
3434
*/
3535
export function calculateOverlayColor(match: CuloriColor, background: CuloriColor): Rgb {
3636
const rgbMatch = rgb(match);
37+
38+
if (Color.isTransparent(rgbMatch) || Color.isTransparent(background)) {
39+
return rgbMatch;
40+
}
41+
3742
const rgbBackground = rgb(background);
3843
let overlay = rgbBlack;
3944
let alpha = calcRgbOverlay(rgbMatch, rgbBackground, overlay);

0 commit comments

Comments
 (0)