-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
266 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using MaterialColorUtilities.Utils; | ||
|
||
namespace MaterialColorUtilities.Tests; | ||
|
||
[TestClass] | ||
public class StringUtilsTests | ||
{ | ||
[TestMethod] | ||
[DataRow(0x00000000U, "#000000")] | ||
[DataRow(0x000000FFU, "#0000FF")] | ||
[DataRow(0xFF0000FFU, "#0000FF")] | ||
[DataRow(0x0000FF00U, "#00FF00")] | ||
[DataRow(0x00FF0000U, "#FF0000")] | ||
[DataRow(0x00FFFFFFU, "#FFFFFF")] | ||
[DataRow(0xFFFFFFFFU, "#FFFFFF")] | ||
public void HexFromArgb(uint argb, string expected) | ||
{ | ||
string actual = StringUtils.HexFromArgb(argb); | ||
Assert.AreEqual(expected, actual); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Microsoft.VisualStudio.TestTools.UnitTesting; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System.Text; | ||
using MaterialColorUtilities.Schemes; | ||
using MaterialColorUtilities.Utils; | ||
|
||
namespace Playground.Wasm; | ||
|
||
public static class CssVariables | ||
{ | ||
public static string Create(Scheme<uint> scheme, Scheme<uint> dark = null, string selector = ":root") | ||
{ | ||
StringBuilder builder = new(); | ||
|
||
builder.Append(selector); | ||
builder.Append(" {\n"); | ||
foreach (var color in scheme.Enumerate()) | ||
{ | ||
builder.Append(" --md-sys-color-"); | ||
builder.Append(PascalToKebab(color.Key)); | ||
builder.Append(": "); | ||
string hex = StringUtils.HexFromArgb(color.Value); | ||
builder.Append(hex); | ||
builder.Append(";\n"); | ||
} | ||
builder.Append('}'); | ||
|
||
if (dark != null) | ||
{ | ||
builder.Append("\n\n@media (prefers-color-scheme: dark) {\n"); | ||
builder.Append(" "); | ||
builder.Append(selector); | ||
builder.Append(" {\n"); | ||
foreach (var color in dark.Enumerate()) | ||
{ | ||
builder.Append(" --md-sys-color-"); | ||
builder.Append(PascalToKebab(color.Key)); | ||
builder.Append(": "); | ||
string hex = StringUtils.HexFromArgb(color.Value); | ||
builder.Append(hex); | ||
builder.Append(";\n"); | ||
} | ||
builder.Append(" }\n}"); | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
private static string PascalToKebab(ReadOnlySpan<char> pascalCase) | ||
{ | ||
if (pascalCase.Length == 0) return string.Empty; | ||
|
||
Span<char> kebabCase = stackalloc char[pascalCase.Length + pascalCase.Length / 2]; | ||
int kebabCaseIndex = 0; | ||
kebabCase[kebabCaseIndex++] = char.ToLower(pascalCase[0]); | ||
|
||
for (int i = 1; i < pascalCase.Length; i++) | ||
{ | ||
char currentChar = pascalCase[i]; | ||
|
||
if (char.IsUpper(currentChar)) | ||
{ | ||
kebabCase[kebabCaseIndex++] = '-'; | ||
kebabCase[kebabCaseIndex++] = char.ToLower(currentChar); | ||
} | ||
else | ||
{ | ||
kebabCase[kebabCaseIndex++] = currentChar; | ||
} | ||
} | ||
|
||
return new(kebabCase[..kebabCaseIndex]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
@page "/theme-builder" | ||
@using System.Web | ||
@using System.Globalization | ||
@using MaterialColorUtilities.Palettes | ||
@layout EmptyLayout | ||
@inject NavigationManager NavigationManager | ||
@inject IJSRuntime JsRuntime | ||
@inherits SchemeDisplay | ||
|
||
<style> | ||
html { | ||
background-color: var(--mud-palette-surface); | ||
} | ||
body { | ||
background-color: transparent; | ||
} | ||
</style> | ||
|
||
<ThemeBuilder Small="true" /> | ||
|
||
@{ | ||
JsRuntime.InvokeVoidAsync("postScheme", ThemeService.CreateCssVariables()); | ||
} | ||
|
||
@code { | ||
|
||
protected override void OnInitialized() | ||
{ | ||
base.OnInitialized(); | ||
|
||
Uri uri = new(NavigationManager.Uri); | ||
var queries = HttpUtility.ParseQueryString(uri.Query); | ||
|
||
string colorQuery = queries["color"]; | ||
if (colorQuery != null && uint.TryParse(colorQuery, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out uint color)) | ||
{ | ||
ThemeService.Seed = color; | ||
} | ||
|
||
string styleQuery = queries["style"]; | ||
if (styleQuery != null && Enum.TryParse(styleQuery, true, out Style style)) | ||
{ | ||
ThemeService.Style = style; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.