From 92e71ae2f4196d3018cef687d176b4c11db847cb Mon Sep 17 00:00:00 2001 From: RaidMax Date: Sat, 27 Jul 2019 15:23:45 -0500 Subject: [PATCH] finish custom accent color feature --- .../Configuration/ApplicationConfiguration.cs | 7 +- SharedLibraryCore/Utilities.cs | 1 + WebfrontCore/Controllers/BaseController.cs | 1 + .../CustomCssAccentMiddlewareAction.cs | 104 ++++++++++-------- WebfrontCore/Views/Shared/_Layout.cshtml | 2 +- WebfrontCore/WebfrontCore.csproj | 1 + WebfrontCore/wwwroot/css/main.scss | 2 +- WebfrontCore/wwwroot/css/profile.scss | 7 +- WebfrontCore/wwwroot/js/server.js | 2 +- WebfrontCore/wwwroot/js/stats.js | 2 +- version.txt | 1 + 11 files changed, 78 insertions(+), 52 deletions(-) diff --git a/SharedLibraryCore/Configuration/ApplicationConfiguration.cs b/SharedLibraryCore/Configuration/ApplicationConfiguration.cs index 0d318b7f4..e8e1b9fa9 100644 --- a/SharedLibraryCore/Configuration/ApplicationConfiguration.cs +++ b/SharedLibraryCore/Configuration/ApplicationConfiguration.cs @@ -10,7 +10,7 @@ namespace SharedLibraryCore.Configuration { [LocalizedDisplayName("SETUP_ENABLE_WEBFRONT")] - [ConfigurationLinked("WebfrontBindUrl", "ManualWebfrontUrl", "WebfrontPrimaryColor", "WebfrontSecondaryColor")] + [ConfigurationLinked("WebfrontBindUrl", "ManualWebfrontUrl", "WebfrontPrimaryColor", "WebfrontSecondaryColor", "WebfrontCustomBranding")] public bool EnableWebFront { get; set; } [LocalizedDisplayName("WEBFRONT_CONFIGURATION_BIND_URL")] public string WebfrontBindUrl { get; set; } @@ -18,9 +18,14 @@ namespace SharedLibraryCore.Configuration [LocalizedDisplayName("WEBFRONT_CONFIGURATION_MANUAL_URL")] public string ManualWebfrontUrl { get; set; } [ConfigurationOptional] + [LocalizedDisplayName("WEBFRONT_CONFIGURATION_PRIMARY_COLOR")] public string WebfrontPrimaryColor { get; set; } [ConfigurationOptional] + [LocalizedDisplayName("WEBFRONT_CONFIGURATION_SECONDARY_COLOR")] public string WebfrontSecondaryColor { get; set; } + [ConfigurationOptional] + [LocalizedDisplayName("WEBFRONT_CONFIGURATION_CUSTOM_BRANDING")] + public string WebfrontCustomBranding { get; set; } [LocalizedDisplayName("SETUP_ENABLE_MULTIOWN")] public bool EnableMultipleOwners { get; set; } diff --git a/SharedLibraryCore/Utilities.cs b/SharedLibraryCore/Utilities.cs index 7b2fc0d64..8b23df4af 100644 --- a/SharedLibraryCore/Utilities.cs +++ b/SharedLibraryCore/Utilities.cs @@ -672,6 +672,7 @@ namespace SharedLibraryCore cmdProcess.WaitForExit(); string[] cmdLine = cmdProcess.StandardOutput.ReadToEnd().Split("\r\n", StringSplitOptions.RemoveEmptyEntries); + cmdProcess.Dispose(); return cmdLine.Length > 1 ? cmdLine[1] : cmdLine[0]; } diff --git a/WebfrontCore/Controllers/BaseController.cs b/WebfrontCore/Controllers/BaseController.cs index 820480bb4..398cbbd24 100644 --- a/WebfrontCore/Controllers/BaseController.cs +++ b/WebfrontCore/Controllers/BaseController.cs @@ -107,6 +107,7 @@ namespace WebfrontCore.Controllers ViewBag.SocialTitle = SocialTitle; ViewBag.Pages = Pages; ViewBag.Localization = Utilities.CurrentLocalization.LocalizationIndex; + ViewBag.CustomBranding = Manager.GetApplicationSettings().Configuration().WebfrontCustomBranding ?? "IW4MAdmin"; base.OnActionExecuting(context); } diff --git a/WebfrontCore/Middleware/CustomCssAccentMiddlewareAction.cs b/WebfrontCore/Middleware/CustomCssAccentMiddlewareAction.cs index 4f6e79da1..3d6542116 100644 --- a/WebfrontCore/Middleware/CustomCssAccentMiddlewareAction.cs +++ b/WebfrontCore/Middleware/CustomCssAccentMiddlewareAction.cs @@ -10,76 +10,90 @@ namespace WebfrontCore.Middleware { public class CustomCssAccentMiddlewareAction : IMiddlewareAction { - private readonly Color _primaryColor; - private readonly Color _secondaryColor; - private readonly Color _originalPrimaryColor; - private readonly Color _originalSecondaryColor; + private readonly List ColorReplacements = new List(); + + private class ColorMap + { + public Color Original { get; set; } + public Color Replacement { get; set; } + } public CustomCssAccentMiddlewareAction(string originalPrimaryColor, string originalSecondaryColor, string primaryColor, string secondaryColor) { - _originalPrimaryColor = Color.FromArgb(Convert.ToInt32(originalPrimaryColor.Substring(1).ToString(), 16)); - _originalSecondaryColor = Color.FromArgb(Convert.ToInt32(originalSecondaryColor.Substring(1).ToString(), 16)); - _primaryColor = string.IsNullOrEmpty(primaryColor) ? _originalPrimaryColor : Color.FromArgb(Convert.ToInt32(primaryColor.Substring(1).ToString(), 16)); - _secondaryColor = string.IsNullOrEmpty(secondaryColor) ? _originalSecondaryColor : Color.FromArgb(Convert.ToInt32(secondaryColor.Substring(1).ToString(), 16)); + primaryColor = string.IsNullOrWhiteSpace(primaryColor) ? originalPrimaryColor : primaryColor; + secondaryColor = string.IsNullOrWhiteSpace(secondaryColor) ? originalSecondaryColor : secondaryColor; + try + { + ColorReplacements.AddRange(new[] + { + new ColorMap() + { + Original = Color.FromArgb(Convert.ToInt32(originalPrimaryColor.Substring(1).ToString(), 16)), + Replacement = Color.FromArgb(Convert.ToInt32(primaryColor.Substring(1).ToString(), 16)) + }, + new ColorMap() + { + Original = Color.FromArgb(Convert.ToInt32(originalSecondaryColor.Substring(1).ToString(), 16)), + Replacement = Color.FromArgb(Convert.ToInt32(secondaryColor.Substring(1).ToString(), 16)) + } + }); + } + + catch (FormatException) + { + + } } public async Task Invoke(string original) { - string originalPrimaryHex = ColorToHex(_originalPrimaryColor); - string originalPrimaryDec = ColorToDec(_originalPrimaryColor); - string originalSecondaryHex = ColorToHex(_originalSecondaryColor); - string originalSecondaryDec = ColorToDec(_originalSecondaryColor); + foreach (var color in ColorReplacements) + { + foreach (var shade in new[] { 0, -19, -25 }) + { + original = original + .Replace(ColorToHex(LightenDarkenColor(color.Original, shade)), ColorToHex(LightenDarkenColor(color.Replacement, shade)), StringComparison.OrdinalIgnoreCase) + .Replace(ColorToDec(LightenDarkenColor(color.Original, shade)), ColorToDec(LightenDarkenColor(color.Replacement, shade)), StringComparison.OrdinalIgnoreCase); + } + } - string primaryHex = ColorToHex(_primaryColor); - string primaryDec = ColorToDec(_primaryColor); - string secondaryHex = ColorToHex(_secondaryColor); - string secondaryDec = ColorToDec(_secondaryColor); - - string originalPrimaryDarkenHex = ColorToHex(LightenDarkenColor(_originalPrimaryColor, -10)); - string originalPrimaryDarkenDec = ColorToDec(LightenDarkenColor(_originalPrimaryColor, -10)); - string originalSecondaryDarkenHex = ColorToHex(LightenDarkenColor(_originalSecondaryColor, -10)); - string originalSecondaryDarkenDec = ColorToDec(LightenDarkenColor(_originalSecondaryColor, -10)); - - string primaryDarkenHex = ColorToHex(LightenDarkenColor(_primaryColor, -10)); - string primaryDarkenDec = ColorToDec(LightenDarkenColor(_primaryColor, -10)); - string secondaryDarkenHex = ColorToHex(LightenDarkenColor(_secondaryColor, -10)); - string secondaryDarkenDec = ColorToDec(LightenDarkenColor(_secondaryColor, -10)); - - return original - .Replace(originalPrimaryHex, primaryHex, StringComparison.OrdinalIgnoreCase) - .Replace(originalPrimaryDec, primaryDec, StringComparison.OrdinalIgnoreCase) - .Replace(originalSecondaryHex, secondaryHex, StringComparison.OrdinalIgnoreCase) - .Replace(originalSecondaryDec, secondaryDec, StringComparison.OrdinalIgnoreCase) - .Replace(originalPrimaryDarkenHex, primaryDarkenHex, StringComparison.OrdinalIgnoreCase) - .Replace(originalPrimaryDarkenDec, primaryDarkenDec, StringComparison.OrdinalIgnoreCase) - .Replace(originalSecondaryDarkenHex, secondaryDarkenHex, StringComparison.OrdinalIgnoreCase) - .Replace(originalSecondaryDarkenDec, secondaryDarkenDec, StringComparison.OrdinalIgnoreCase); + return original; } + /// + /// converts color to the hex string representation + /// + /// + /// private string ColorToHex(Color color) => $"#{color.R.ToString("X2")}{color.G.ToString("X2")}{color.B.ToString("X2")}"; + + /// + /// converts color to the rgb tuples representation + /// + /// + /// private string ColorToDec(Color color) => $"{(int)color.R}, {(int)color.G}, {(int)color.B}"; /// - /// Adapted from https://css-tricks.com/snippets/javascript/lighten-darken-color/ + /// lightens or darkens a color on the given amount + /// Based off SASS darken/lighten function /// - /// - /// + /// + /// /// - private Color LightenDarkenColor(Color col, float amt) + private Color LightenDarkenColor(Color color, float amount) { - var num = col.ToArgb(); - - int r = (num >> 16) + (int)(amt * (num >> 16)); + int r = color.R + (int)((amount / 100.0f) * color.R); if (r > 255) r = 255; else if (r < 0) r = 0; - int g = ((num >> 8) & 0x00FF) + (int)(amt * ((num >> 8) & 0x00FF)); + int g = color.G + (int)((amount / 100.0f) * color.G); if (g > 255) g = 255; else if (g < 0) g = 0; - int b = (num & 0x0000FF) + (int)(amt * (num & 0x0000FF)); + int b = color.B + (int)((amount / 100.0f) * color.B); if (b > 255) b = 255; else if (b < 0) b = 0; diff --git a/WebfrontCore/Views/Shared/_Layout.cshtml b/WebfrontCore/Views/Shared/_Layout.cshtml index 90617f184..72e6f8d5a 100644 --- a/WebfrontCore/Views/Shared/_Layout.cshtml +++ b/WebfrontCore/Views/Shared/_Layout.cshtml @@ -28,7 +28,7 @@