Skip to content

Commit 7aef09f

Browse files
authored
Merge pull request #193 from microsoft/token-accent
Fix Design Tokens handling Color values
2 parents 0d41771 + 51d4550 commit 7aef09f

File tree

11 files changed

+239
-101
lines changed

11 files changed

+239
-101
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
140140
await BaseLayerLuminance.SetValueFor(ref1!.Element, (float)0.15);
141141

142142
//Set to Excel color
143-
await AccentBaseColor.SetValueFor(ref2!.Element, "#185ABD".ToColor());
143+
await AccentBaseColor.SetValueFor(ref2!.Element, "#185ABD".ToSwatch());
144144

145145
//Set the font
146146
await BodyFont.SetValueFor(ref3!.Element, "Comic Sans MS");
@@ -163,7 +163,7 @@ public async Task OnClick()
163163
```
164164
As can be seen in the code above (with the `ref4.Element`), it is posible to apply multiple tokens to the same component.
165165

166-
For Design Tokens that work with a color value, it is needed to add the `ToColor()` extension method on the string value. This converts the string into a RGB value that the Design Token can operate with. Internally we are using the `System.Drawing.Color` struct for this and this means you can use all the available methods, operators, etc from that namespace in your code too.
166+
For Design Tokens that work with a color value, it is needed to add the `ToSwatch()` extension method on the string value or use one of the `Swatch` constructors. This to make sure the color is using a format that Design Tokens can handle. A `Swatch` has a lot of commonality with the `System.Drawing.Color` struct. Instead of the values of the components being between 0 and 255, in a `Swatch` they are expressed as a value between 0 and 1.
167167

168168
> :notebook: **Note**
169169
>

examples/FluentUI.Demo.Shared/Pages/CardPage.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
</FluentCard>
4545
</BaseLayerLuminance>
4646

47-
<FillColor Value=@("#D6D6D6".ToColor())>
47+
<FillColor Value=@(new Swatch("#D6D6D6"))>
4848
<FluentCard neutral-palette-source="#CABA8C" BackReference="@context">
4949
<div class="contents">
5050
Tinted neutral, light

examples/FluentUI.Demo.Shared/Pages/Index.razor

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,29 @@
99
<FluentAnchor @ref="ref3" Appearance="Appearance.Filled" Href="componentbindings" >Blazor component bindings</FluentAnchor>
1010
<FluentButton @ref="ref4" Appearance="Appearance.Filled" @onclick=OnClick>FluentButton</FluentButton>
1111

12+
<h2>Color Test</h2>
13+
14+
<div>
15+
<div style="margin-top: 12px; display: flex;">
16+
<div style="height: 32px; width: 124px; margin-right: 5px; padding: 9px 0; font-size: small; text-align: center;">Accent</div>
17+
<div style="height: 32px; width: 95px; margin-right: 5px; font-size: small; display: flex;align-items: center;justify-content: center; color:white; background-color: #D83B01;">#D83B01</div>
18+
<div style="height: 32px; width: 95px; margin-right: 5px; font-size: small; display: flex;align-items: center;justify-content: center; color:white; background-color: #185ABD;">#185ABD</div>
19+
<div style="height: 32px; width: 95px; margin-right: 5px; font-size: small; display: flex;align-items: center;justify-content: center; color:white; background-color: #107C41;">#107C41</div>
20+
<div style="height: 32px; width: 95px; margin-right: 5px; font-size: small; display: flex;align-items: center;justify-content: center; color:white; background-color: #C43E1C;">#C43E1C</div>
21+
<div style="height: 32px; width: 95px; margin-right: 5px; font-size: small; display: flex;align-items: center;justify-content: center; color:white; background-color: #6264A7;">#6264A7</div>
22+
<div style="height: 32px; width: 95px; margin-right: 5px; font-size: small; display: flex;align-items: center;justify-content: center; color:white; background-color: #7719AA;">#7719AA</div>
23+
<div style="height: 32px; width: 95px; margin-right: 5px; font-size: small; display: flex;align-items: center;justify-content: center; color:white; background-color: #03787C;">#03787C</div>
24+
<div style="height: 32px; width: 95px; margin-right: 5px; font-size: small; display: flex;align-items: center;justify-content: center; color:white; background-color: #BC1948;">#BC1948</div>
25+
</div>
26+
<div style="margin-top: 12px; display: flex;">
27+
<div style="height: 32px; width: 124px; margin-right: 5px; padding: 9px 0; font-size: small; text-align: center;">Result</div>
28+
<FluentButton @ref="cref1" id="office" Appearance="Appearance.Accent" style="margin-right: 5px; width: 95px;">Office</FluentButton>
29+
<FluentButton @ref="cref2" Appearance="Appearance.Accent" style="margin-right: 5px; width: 95px;">Word</FluentButton>
30+
<FluentButton @ref="cref3" Appearance="Appearance.Accent" style="margin-right: 5px; width: 95px;">Excel</FluentButton>
31+
<FluentButton @ref="cref4" Appearance="Appearance.Accent" style="margin-right: 5px; width: 95px;">PowerPoint</FluentButton>
32+
<FluentButton @ref="cref5" Appearance="Appearance.Accent" style="margin-right: 5px; width: 95px;">Teams</FluentButton>
33+
<FluentButton @ref="cref6" Appearance="Appearance.Accent" style="margin-right: 5px; width: 95px;">OneNote</FluentButton>
34+
<FluentButton @ref="cref7" Appearance="Appearance.Accent" style="margin-right: 5px; width: 95px;">SharePoint</FluentButton>
35+
<FluentButton @ref="cref8" Appearance="Appearance.Accent" style="margin-right: 5px; width: 95px;">Stream</FluentButton>
36+
</div>
37+
</div>

examples/FluentUI.Demo.Shared/Pages/Index.razor.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,23 @@ public partial class Index
2626
private FluentAnchor? ref3;
2727
private FluentButton? ref4;
2828

29+
private FluentButton? cref1;
30+
private FluentButton? cref2;
31+
private FluentButton? cref3;
32+
private FluentButton? cref4;
33+
private FluentButton? cref5;
34+
private FluentButton? cref6;
35+
private FluentButton? cref7;
36+
private FluentButton? cref8;
37+
2938
protected override async Task OnAfterRenderAsync(bool firstRender)
3039
{
3140
if (firstRender)
3241
{
3342
//Set to dark mode
3443
await BaseLayerLuminance.SetValueFor(ref1!.Element, (float)0.15);
3544

36-
await AccentBaseColor.SetValueFor(ref2!.Element, "#185ABD".ToColor());
45+
await AccentBaseColor.SetValueFor(ref2!.Element, "#107C41".ToSwatch());
3746

3847
await BodyFont.SetValueFor(ref3!.Element, "Comic Sans MS");
3948

@@ -42,6 +51,16 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
4251
//And change conrner radius as well
4352
await ControlCornerRadius.SetValueFor(ref4!.Element, 15);
4453

54+
55+
await AccentBaseColor.SetValueFor(cref1!.Element, "#D83B01".ToSwatch());
56+
await AccentBaseColor.SetValueFor(cref2!.Element, "#185ABD".ToSwatch());
57+
await AccentBaseColor.SetValueFor(cref3!.Element, "#107C41".ToSwatch());
58+
await AccentBaseColor.SetValueFor(cref4!.Element, "#C43E1C".ToSwatch());
59+
await AccentBaseColor.SetValueFor(cref5!.Element, "#6264A7".ToSwatch());
60+
await AccentBaseColor.SetValueFor(cref6!.Element, "#7719AA".ToSwatch());
61+
await AccentBaseColor.SetValueFor(cref7!.Element, "#03787C".ToSwatch());
62+
await AccentBaseColor.SetValueFor(cref8!.Element, "#BC1948".ToSwatch());
63+
4564
StateHasChanged();
4665
}
4766

examples/FluentUI.Demo.Shared/Pages/ToolbarPage.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@page "/ToolbarPage"
2-
@using System.Drawing
2+
33
@inject FillColor FillColor
44
@inject BaseLayerLuminance BaseLayerLuminance
55
<svg xmlns="http://www.w3.org/2000/svg" style="display:none">
@@ -184,8 +184,8 @@
184184
{
185185
if (firstRender)
186186
{
187-
await FillColor.SetValueFor(Toolbar1!.Element, "#333".ToColor());
188-
await FillColor.SetValueFor(Toolbar2!.Element, "#333".ToColor());
187+
await FillColor.SetValueFor(Toolbar1!.Element, "#333".ToSwatch());
188+
await FillColor.SetValueFor(Toolbar2!.Element, "#333".ToSwatch());
189189
await BaseLayerLuminance.SetValueFor(Toolbar1!.Element, (float)0.15);
190190
await BaseLayerLuminance.SetValueFor(Toolbar2!.Element, (float)0.15);
191191

src/Microsoft.Fast.Components.FluentUI.Generators/DesignTokenConstants.cs

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -114,151 +114,151 @@ public class DesignTokenConstants
114114
public const string neutralStrokeStrongFocusDelta = "int?";
115115

116116
// Color recipes
117-
public const string neutralBaseColor = "System.Drawing.Color";
118-
public const string accentBaseColor = "System.Drawing.Color";
117+
public const string neutralBaseColor = "Swatch";
118+
public const string accentBaseColor = "Swatch";
119119

120120
// Neutral Layer Card Container
121-
public const string neutralLayerCardContainer = "System.Drawing.Color";
121+
public const string neutralLayerCardContainer = "Swatch";
122122

123123
// Neutral Layer Floating
124-
public const string neutralLayerFloating = "System.Drawing.Color";
124+
public const string neutralLayerFloating = "Swatch";
125125

126126
// Neutral Layer 1
127-
public const string neutralLayer1 = "System.Drawing.Color";
127+
public const string neutralLayer1 = "Swatch";
128128

129129
// Neutral Layer 2
130-
public const string neutralLayer2 = "System.Drawing.Color";
130+
public const string neutralLayer2 = "Swatch";
131131

132132
// Neutral Layer 3
133-
public const string neutralLayer3 = "System.Drawing.Color";
133+
public const string neutralLayer3 = "Swatch";
134134

135135
// Neutral Layer 4
136-
public const string neutralLayer4 = "System.Drawing.Color";
136+
public const string neutralLayer4 = "Swatch";
137137

138-
public const string fillColor = "System.Drawing.Color";
138+
public const string fillColor = "Swatch";
139139

140140
// Accent Fill
141-
public const string accentFillRest = "System.Drawing.Color";
142-
public const string accentFillHover = "System.Drawing.Color";
143-
public const string accentFillActive = "System.Drawing.Color";
144-
public const string accentFillFocus = "System.Drawing.Color";
141+
public const string accentFillRest = "Swatch";
142+
public const string accentFillHover = "Swatch";
143+
public const string accentFillActive = "Swatch";
144+
public const string accentFillFocus = "Swatch";
145145

146146
// Foreground On Accent
147-
public const string foregroundOnAccentRest = "System.Drawing.Color";
148-
public const string foregroundOnAccentHover = "System.Drawing.Color";
149-
public const string foregroundOnAccentActive = "System.Drawing.Color";
150-
public const string foregroundOnAccentFocus = "System.Drawing.Color";
147+
public const string foregroundOnAccentRest = "Swatch";
148+
public const string foregroundOnAccentHover = "Swatch";
149+
public const string foregroundOnAccentActive = "Swatch";
150+
public const string foregroundOnAccentFocus = "Swatch";
151151

152152
// Accent Foreground
153-
public const string accentForegroundRest = "System.Drawing.Color";
154-
public const string accentForegroundHover = "System.Drawing.Color";
155-
public const string accentForegroundActive = "System.Drawing.Color";
156-
public const string accentForegroundFocus = "System.Drawing.Color";
153+
public const string accentForegroundRest = "Swatch";
154+
public const string accentForegroundHover = "Swatch";
155+
public const string accentForegroundActive = "Swatch";
156+
public const string accentForegroundFocus = "Swatch";
157157

158158
// Accent Stroke Control
159-
public const string accentStrokeControlRest = "System.Drawing.Color";
160-
public const string accentStrokeControlHover = "System.Drawing.Color";
161-
public const string accentStrokeControlActive = "System.Drawing.Color";
162-
public const string accentStrokeControlFocus = "System.Drawing.Color";
159+
public const string accentStrokeControlRest = "Swatch";
160+
public const string accentStrokeControlHover = "Swatch";
161+
public const string accentStrokeControlActive = "Swatch";
162+
public const string accentStrokeControlFocus = "Swatch";
163163

164164
// Neutral Fill
165-
public const string neutralFillRest = "System.Drawing.Color";
166-
public const string neutralFillHover = "System.Drawing.Color";
167-
public const string neutralFillActive = "System.Drawing.Color";
168-
public const string neutralFillFocus = "System.Drawing.Color";
165+
public const string neutralFillRest = "Swatch";
166+
public const string neutralFillHover = "Swatch";
167+
public const string neutralFillActive = "Swatch";
168+
public const string neutralFillFocus = "Swatch";
169169

170170
// Neutral Fill Input
171-
public const string neutralFillInputRest = "System.Drawing.Color";
172-
public const string neutralFillInputHover = "System.Drawing.Color";
173-
public const string neutralFillInputActive = "System.Drawing.Color";
174-
public const string neutralFillInputFocus = "System.Drawing.Color";
171+
public const string neutralFillInputRest = "Swatch";
172+
public const string neutralFillInputHover = "Swatch";
173+
public const string neutralFillInputActive = "Swatch";
174+
public const string neutralFillInputFocus = "Swatch";
175175

176176
// Neutral Fill Input Alt
177-
public const string neutralFillInputAltRest = "System.Drawing.Color";
178-
public const string neutralFillInputAltHover = "System.Drawing.Color";
179-
public const string neutralFillInputAltActive = "System.Drawing.Color";
180-
public const string neutralFillInputAltFocus = "System.Drawing.Color";
177+
public const string neutralFillInputAltRest = "Swatch";
178+
public const string neutralFillInputAltHover = "Swatch";
179+
public const string neutralFillInputAltActive = "Swatch";
180+
public const string neutralFillInputAltFocus = "Swatch";
181181

182182
// Neutral Fill Layer
183-
public const string neutralFillLayerRest = "System.Drawing.Color";
184-
public const string neutralFillLayerHover = "System.Drawing.Color";
185-
public const string neutralFillLayerActive = "System.Drawing.Color";
183+
public const string neutralFillLayerRest = "Swatch";
184+
public const string neutralFillLayerHover = "Swatch";
185+
public const string neutralFillLayerActive = "Swatch";
186186

187187
// Neutral Fill Layer Alt
188-
public const string neutralFillLayerAltRest = "System.Drawing.Color";
188+
public const string neutralFillLayerAltRest = "Swatch";
189189

190190
// Neutral Fill Secondary
191-
public const string neutralFillSecondaryRest = "System.Drawing.Color";
192-
public const string neutralFillSecondaryHover = "System.Drawing.Color";
193-
public const string neutralFillSecondaryActive = "System.Drawing.Color";
194-
public const string neutralFillSecondaryFocus = "System.Drawing.Color";
191+
public const string neutralFillSecondaryRest = "Swatch";
192+
public const string neutralFillSecondaryHover = "Swatch";
193+
public const string neutralFillSecondaryActive = "Swatch";
194+
public const string neutralFillSecondaryFocus = "Swatch";
195195

196196
// Neutral Fill Stealth
197-
public const string neutralFillStealthRest = "System.Drawing.Color";
198-
public const string neutralFillStealthHover = "System.Drawing.Color";
199-
public const string neutralFillStealthActive = "System.Drawing.Color";
200-
public const string neutralFillStealthFocus = "System.Drawing.Color";
197+
public const string neutralFillStealthRest = "Swatch";
198+
public const string neutralFillStealthHover = "Swatch";
199+
public const string neutralFillStealthActive = "Swatch";
200+
public const string neutralFillStealthFocus = "Swatch";
201201

202202
// Neutral Fill Strong
203-
public const string neutralFillStrongRest = "System.Drawing.Color";
203+
public const string neutralFillStrongRest = "Swatch";
204204

205-
public const string neutralFillStrongHover = "System.Drawing.Color";
205+
public const string neutralFillStrongHover = "Swatch";
206206

207-
public const string neutralFillStrongActive = "System.Drawing.Color";
207+
public const string neutralFillStrongActive = "Swatch";
208208

209-
public const string neutralFillStrongFocus = "System.Drawing.Color";
209+
public const string neutralFillStrongFocus = "Swatch";
210210

211211

212212
// Neutral Foreground
213-
public const string neutralForegroundRest = "System.Drawing.Color";
213+
public const string neutralForegroundRest = "Swatch";
214214

215-
public const string neutralForegroundHover = "System.Drawing.Color";
215+
public const string neutralForegroundHover = "Swatch";
216216

217-
public const string neutralForegroundActive = "System.Drawing.Color";
217+
public const string neutralForegroundActive = "Swatch";
218218

219-
public const string neutralForegroundFocus = "System.Drawing.Color";
219+
public const string neutralForegroundFocus = "Swatch";
220220

221221

222222
// Neutral Foreground Hint
223-
public const string neutralForegroundHint = "System.Drawing.Color";
223+
public const string neutralForegroundHint = "Swatch";
224224

225225

226226
// Neutral Stroke
227-
public const string neutralStrokeRest = "System.Drawing.Color";
228-
public const string neutralStrokeHover = "System.Drawing.Color";
229-
public const string neutralStrokeActive = "System.Drawing.Color";
230-
public const string neutralStrokeFocus = "System.Drawing.Color";
227+
public const string neutralStrokeRest = "Swatch";
228+
public const string neutralStrokeHover = "Swatch";
229+
public const string neutralStrokeActive = "Swatch";
230+
public const string neutralStrokeFocus = "Swatch";
231231

232232
// Neutral Stroke Control
233-
public const string neutralStrokeControlRest = "System.Drawing.Color";
234-
public const string neutralStrokeControlHover = "System.Drawing.Color";
235-
public const string neutralStrokeControlActive = "System.Drawing.Color";
236-
public const string neutralStrokeControlFocus = "System.Drawing.Color";
233+
public const string neutralStrokeControlRest = "Swatch";
234+
public const string neutralStrokeControlHover = "Swatch";
235+
public const string neutralStrokeControlActive = "Swatch";
236+
public const string neutralStrokeControlFocus = "Swatch";
237237

238238

239239
// Neutral Stroke Divider
240-
public const string neutralStrokeDividerRest = "System.Drawing.Color";
240+
public const string neutralStrokeDividerRest = "Swatch";
241241

242242
// Neutral Stroke Input
243-
public const string neutralStrokeInputRest = "System.Drawing.Color";
244-
public const string neutralStrokeInputHover = "System.Drawing.Color";
245-
public const string neutralStrokeInputActive = "System.Drawing.Color";
246-
public const string neutralStrokeInputFocus = "System.Drawing.Color";
243+
public const string neutralStrokeInputRest = "Swatch";
244+
public const string neutralStrokeInputHover = "Swatch";
245+
public const string neutralStrokeInputActive = "Swatch";
246+
public const string neutralStrokeInputFocus = "Swatch";
247247

248248
// Neutral Stroke Layer
249-
public const string neutralStrokeLayerRest = "System.Drawing.Color";
250-
public const string neutralStrokeLayerHover = "System.Drawing.Color";
251-
public const string neutralStrokeLayerActive = "System.Drawing.Color";
249+
public const string neutralStrokeLayerRest = "Swatch";
250+
public const string neutralStrokeLayerHover = "Swatch";
251+
public const string neutralStrokeLayerActive = "Swatch";
252252

253253
// Neutral Stroke Strong
254-
public const string neutralStrokeStrongRest = "System.Drawing.Color";
255-
public const string neutralStrokeStrongHover = "System.Drawing.Color";
256-
public const string neutralStrokeStrongActive = "System.Drawing.Color";
257-
public const string neutralStrokeStrongFocus = "System.Drawing.Color";
254+
public const string neutralStrokeStrongRest = "Swatch";
255+
public const string neutralStrokeStrongHover = "Swatch";
256+
public const string neutralStrokeStrongActive = "Swatch";
257+
public const string neutralStrokeStrongFocus = "Swatch";
258258

259259
// Focus Stroke Outer
260-
public const string focusStrokeOuter = "System.Drawing.Color";
260+
public const string focusStrokeOuter = "Swatch";
261261

262262
// Focus Stroke Inner
263-
public const string focusStrokeInner = "System.Drawing.Color";
263+
public const string focusStrokeInner = "Swatch";
264264
}

src/Microsoft.Fast.Components.FluentUI/ColorHelpers.cs

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

src/Microsoft.Fast.Components.FluentUI/DesignTokens/DesignToken.razor.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ public async ValueTask<T> GetValueFor(ElementReference element)
129129
return await module.InvokeAsync<T>(Name + ".getValueFor", element);
130130
}
131131

132+
/// <summary>
133+
/// Convert a hex color string to a value the DesignToken can work with
134+
/// </summary>
135+
/// <returns>the value</returns>
136+
public async ValueTask<object> ParseColorHex(string color)
137+
{
138+
IJSObjectReference module = await moduleTask!.Value;
139+
return await module.InvokeAsync<object>("parseColorHexRGB", color);
140+
}
141+
142+
132143
[SuppressMessage("Usage", "CA1816:Dispose methods should call SuppressFinalize", Justification = "Not needed")]
133144
public async ValueTask DisposeAsync()
134145
{

0 commit comments

Comments
 (0)