Skip to content

Commit 044caaa

Browse files
authored
feat: track accentBaseColor in the design system (#1878)
* Added accentBaseColor to the design system for future color updates * Set accentBaseColor setting in Explorer and Color explorer apps
1 parent 2488879 commit 044caaa

File tree

11 files changed

+37
-73
lines changed

11 files changed

+37
-73
lines changed

packages/fast-color-explorer/app/design-system.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ import {
22
DesignSystem,
33
DesignSystemDefaults,
44
} from "@microsoft/fast-components-styles-msft";
5-
import {
6-
accentPaletteConfig,
7-
createColorPalette,
8-
neutralPaletteConfig,
9-
} from "@microsoft/fast-components-styles-msft";
5+
import { createColorPalette } from "@microsoft/fast-components-styles-msft";
106
import { ColorRGBA64, parseColorHexRGB } from "@microsoft/fast-colors";
117
import { AccentColors } from "./colors";
128

@@ -19,5 +15,6 @@ export const colorsDesignSystem: ColorsDesignSystem = Object.assign(
1915
accentPalette: createColorPalette(parseColorHexRGB(
2016
AccentColors.blue
2117
) as ColorRGBA64),
18+
accentBaseColor: AccentColors.blue,
2219
}
2320
);

packages/fast-color-explorer/app/state.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ function setPalette(
4545
...state.designSystem,
4646
[palette]: createColorPalette(value),
4747
};
48+
if (palette === "accentPalette") {
49+
designSystem.accentBaseColor = value.toStringHexRGB();
50+
}
4851

4952
return {
5053
...state,

packages/fast-components-react-msft/app/app.tsx

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Site, {
1111
Theme,
1212
} from "@microsoft/fast-development-site-react";
1313
import {
14+
createColorPalette,
1415
DensityOffset,
1516
DesignSystem,
1617
DesignSystemDefaults,
@@ -120,10 +121,9 @@ export default class App extends React.Component<{}, AppState> {
120121
this.state = {
121122
accentColor: accent,
122123
designSystem: Object.assign({}, DesignSystemDefaults, {
123-
accentPalette: this.createColorPalette(parseColor(accent)),
124-
neutralPalette: this.createColorPalette(
125-
new ColorRGBA64(0.5, 0.5, 0.5, 1)
126-
),
124+
accentPalette: createColorPalette(parseColor(accent)),
125+
accentBaseColor: accent,
126+
neutralPalette: createColorPalette(new ColorRGBA64(0.5, 0.5, 0.5, 1)),
127127
direction: Direction.ltr,
128128
}),
129129
backgroundColor: DesignSystemDefaults.backgroundColor,
@@ -222,7 +222,7 @@ export default class App extends React.Component<{}, AppState> {
222222
s: hslColor.s,
223223
l: 0.5,
224224
});
225-
return this.createColorPalette(hslToRGB(augmentedHSLColor));
225+
return createColorPalette(hslToRGB(augmentedHSLColor));
226226
}
227227

228228
private handleUpdateTheme = (theme: ThemeName): void => {
@@ -273,22 +273,14 @@ export default class App extends React.Component<{}, AppState> {
273273

274274
if (config.accentColor !== this.state.accentColor) {
275275
updates.designSystem = Object.assign({}, this.state.designSystem, {
276-
accentPalette: this.createColorPalette(parseColor(config.accentColor)),
276+
accentPalette: createColorPalette(parseColor(config.accentColor)),
277+
accentBaseColor: config.accentColor,
277278
});
278279
}
279280

280281
this.setState(updates as AppState);
281282
};
282283

283-
private createColorPalette(baseColor: ColorRGBA64): string[] {
284-
return new ColorPalette({
285-
baseColor,
286-
steps: 63,
287-
clipLight: 0,
288-
clipDark: 0,
289-
}).palette.map((color: ColorRGBA64): string => color.toStringHexRGB());
290-
}
291-
292284
private handleDensityUpdate = (e: React.ChangeEvent<HTMLInputElement>): void => {
293285
this.setState({
294286
designSystem: Object.assign({}, this.state.designSystem, {

packages/fast-components-styles-msft/src/design-system/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export interface DesignSystem {
1616
*/
1717
backgroundColor: string;
1818

19+
/**
20+
* The accent color, which the accentPalette is based on
21+
*/
22+
accentBaseColor: string;
23+
1924
/**
2025
* Configuration object to derive the neutral palette. Expects a ColorPaletteConfig from @microsoft/fast-colors
2126
*/
@@ -169,6 +174,7 @@ const designSystemDefaults: DesignSystem = {
169174
outlineWidth: 1,
170175
neutralPalette: createColorPalette(new ColorRGBA64(0.5, 0.5, 0.5, 1)),
171176
accentPalette: createColorPalette(parseColorHexRGB("#0078D4")),
177+
accentBaseColor: "#0078D4",
172178

173179
/**
174180
* Recipe Deltas

packages/fast-components-styles-msft/src/utilities/color/accent-fill.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { DesignSystem, DesignSystemResolver } from "../../design-system";
22
import {
3+
accentBaseColor,
34
accentFillActiveDelta,
45
accentFillHoverDelta,
56
accentFillRestDelta,
67
accentFillSelectedDelta,
78
} from "../design-system";
8-
import { accentSwatch, findAccessibleAccentSwatchIndexes } from "./accent";
9+
import { findAccessibleAccentSwatchIndexes } from "./accent";
910
import { accentForegroundCut } from "./accent-foreground-cut";
1011
import {
1112
colorRecipeFactory,
@@ -21,7 +22,7 @@ import { getSwatch, isDarkMode, Palette, palette, PaletteType } from "./palette"
2122
function accentFillAlgorithm(contrast: number): DesignSystemResolver<FillSwatchFamily> {
2223
return (designSystem: DesignSystem): FillSwatchFamily => {
2324
const accentPalette: Palette = palette(PaletteType.accent)(designSystem);
24-
const accent: Swatch = accentSwatch(designSystem);
25+
const accent: Swatch = accentBaseColor(designSystem);
2526
const textColor: Swatch = accentForegroundCut(
2627
Object.assign({}, designSystem, {
2728
backgroundColor: accent,

packages/fast-components-styles-msft/src/utilities/color/accent-foreground-cut.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { black, white } from "./color-constants";
22
import { DesignSystem } from "../../design-system";
3-
import { accentSwatch } from "./accent";
4-
import {
5-
ColorRecipe,
6-
colorRecipeFactory,
7-
contrast,
8-
Swatch,
9-
SwatchRecipe,
10-
SwatchResolver,
11-
} from "./common";
3+
import { contrast, Swatch, SwatchRecipe, SwatchResolver } from "./common";
4+
import { accentBaseColor } from "../design-system";
125

136
/**
147
* Function to derive accentForegroundCut from an input background and target contrast ratio
@@ -33,7 +26,7 @@ function accentForegroundCutFactory(targetContrast: number): SwatchRecipe {
3326
? (designSystem: DesignSystem): Swatch => {
3427
return accentForegroundCutAlgorithm(arg(designSystem), targetContrast);
3528
}
36-
: accentForegroundCutAlgorithm(accentSwatch(arg), targetContrast);
29+
: accentForegroundCutAlgorithm(accentBaseColor(arg), targetContrast);
3730
}
3831

3932
return accentForegroundCutInternal;

packages/fast-components-styles-msft/src/utilities/color/accent.spec.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

packages/fast-components-styles-msft/src/utilities/color/accent.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
import { DesignSystem } from "../../design-system";
22
import { Palette, palette, PaletteType } from "./palette";
3-
import { accent } from "./color-constants";
43
import { findClosestSwatchIndex, isDarkMode } from "./palette";
54
import { contrast, Swatch, SwatchResolver } from "./common";
65
import { clamp, inRange } from "lodash-es";
7-
8-
/**
9-
* Returns a swatch from the middle of the accent palette
10-
*/
11-
export const accentSwatch: SwatchResolver = (designSystem: DesignSystem): Swatch => {
12-
const accentPalette: Palette | null = palette(PaletteType.accent)(designSystem);
13-
14-
return accentPalette === null
15-
? accent
16-
: accentPalette[Math.floor(accentPalette.length / 2)];
17-
};
6+
import { accentBaseColor } from "../design-system";
187

198
/**
209
* Returns indexes for accent UI states that are accessible against an input reference color.
@@ -33,7 +22,7 @@ export function findAccessibleAccentSwatchIndexes(
3322
hover: number;
3423
active: number;
3524
} {
36-
const accentColor: Swatch = accentSwatch(designSystem);
25+
const accentColor: Swatch = accentBaseColor(designSystem);
3726
const accentPalette: Palette = palette(PaletteType.accent)(designSystem);
3827
const darkTheme: boolean = isDarkMode(designSystem);
3928
const stateDirection: 1 | -1 = darkTheme ? 1 : -1;

packages/fast-components-styles-msft/src/utilities/color/color-constants.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { ColorPaletteConfig, parseColorHexRGB } from "@microsoft/fast-colors";
22

33
export const white: string = "#FFFFFF";
44
export const black: string = "#000000";
5-
export const accent: string = "#0078D4";
65

76
export const paletteConstants: Partial<ColorPaletteConfig> = {
87
steps: 63,
@@ -19,5 +18,5 @@ export const neutralPaletteConfig: ColorPaletteConfig = {
1918

2019
export const accentPaletteConfig: ColorPaletteConfig = {
2120
...paletteConstants,
22-
baseColor: parseColorHexRGB(accent),
21+
baseColor: parseColorHexRGB("#0078D4"),
2322
};

packages/fast-components-styles-msft/src/utilities/color/palette.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import {
99
swatchByMode,
1010
} from "./palette";
1111
import designSystemDefaults, { DesignSystem } from "../../design-system";
12-
import { accent } from "./color-constants";
1312
import { Swatch } from "./common";
14-
import { neutralForeground } from "./neutral-foreground";
15-
import { neutralPalette } from "../design-system";
13+
import { accentBaseColor, neutralPalette } from "../design-system";
1614

1715
describe("palette", (): void => {
1816
test("should return a function", (): void => {
@@ -59,6 +57,8 @@ describe("palette", (): void => {
5957
});
6058

6159
describe("findSwatchIndex", (): void => {
60+
const accent: string = accentBaseColor(designSystemDefaults);
61+
6262
test("should impelment design-system defaults", (): void => {
6363
expect(findSwatchIndex(PaletteType.neutral, "#FFF")({} as DesignSystem)).toBe(0);
6464
expect(findSwatchIndex(PaletteType.accent, accent)({} as DesignSystem)).toBe(31);

0 commit comments

Comments
 (0)