2019-07-27 09:18:49 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.Middleware
|
|
|
|
|
{
|
|
|
|
|
public class CustomCssAccentMiddlewareAction : IMiddlewareAction<string>
|
|
|
|
|
{
|
2019-07-27 16:23:45 -04:00
|
|
|
|
private readonly List<ColorMap> ColorReplacements = new List<ColorMap>();
|
|
|
|
|
|
|
|
|
|
private class ColorMap
|
|
|
|
|
{
|
|
|
|
|
public Color Original { get; set; }
|
|
|
|
|
public Color Replacement { get; set; }
|
|
|
|
|
}
|
2019-07-27 09:18:49 -04:00
|
|
|
|
|
|
|
|
|
public CustomCssAccentMiddlewareAction(string originalPrimaryColor, string originalSecondaryColor, string primaryColor, string secondaryColor)
|
|
|
|
|
{
|
2019-07-27 16:23:45 -04:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2019-07-27 09:18:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 13:05:12 -05:00
|
|
|
|
public Task<string> Invoke(string original)
|
2019-07-27 09:18:49 -04:00
|
|
|
|
{
|
2019-07-27 16:23:45 -04:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 13:05:12 -05:00
|
|
|
|
return Task.FromResult(original);
|
2019-07-27 09:18:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 16:23:45 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// converts color to the hex string representation
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="color"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-07-27 09:18:49 -04:00
|
|
|
|
private string ColorToHex(Color color) => $"#{color.R.ToString("X2")}{color.G.ToString("X2")}{color.B.ToString("X2")}";
|
2019-07-27 16:23:45 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// converts color to the rgb tuples representation
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="color"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-07-27 09:18:49 -04:00
|
|
|
|
private string ColorToDec(Color color) => $"{(int)color.R}, {(int)color.G}, {(int)color.B}";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-07-27 16:23:45 -04:00
|
|
|
|
/// lightens or darkens a color on the given amount
|
|
|
|
|
/// Based off SASS darken/lighten function
|
2019-07-27 09:18:49 -04:00
|
|
|
|
/// </summary>
|
2019-07-27 16:23:45 -04:00
|
|
|
|
/// <param name="color"></param>
|
|
|
|
|
/// <param name="amount"></param>
|
2019-07-27 09:18:49 -04:00
|
|
|
|
/// <returns></returns>
|
2019-07-27 16:23:45 -04:00
|
|
|
|
private Color LightenDarkenColor(Color color, float amount)
|
2019-07-27 09:18:49 -04:00
|
|
|
|
{
|
2019-07-27 16:23:45 -04:00
|
|
|
|
int r = color.R + (int)((amount / 100.0f) * color.R);
|
2019-07-27 09:18:49 -04:00
|
|
|
|
|
|
|
|
|
if (r > 255) r = 255;
|
|
|
|
|
else if (r < 0) r = 0;
|
|
|
|
|
|
2019-07-27 16:23:45 -04:00
|
|
|
|
int g = color.G + (int)((amount / 100.0f) * color.G);
|
2019-07-27 09:18:49 -04:00
|
|
|
|
|
|
|
|
|
if (g > 255) g = 255;
|
|
|
|
|
else if (g < 0) g = 0;
|
|
|
|
|
|
2019-07-27 16:23:45 -04:00
|
|
|
|
int b = color.B + (int)((amount / 100.0f) * color.B);
|
2019-07-27 09:18:49 -04:00
|
|
|
|
|
|
|
|
|
if (b > 255) b = 255;
|
|
|
|
|
else if (b < 0) b = 0;
|
|
|
|
|
|
|
|
|
|
return Color.FromArgb(r, g, b);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|