Last active
January 27, 2021 16:57
-
-
Save dfkeenan/d5ce7ba64f796e41cd9202b416c994bd to your computer and use it in GitHub Desktop.
Transform VS Image Library icons for themes
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
using System; | |
using System.Xml.Linq; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System.IO; | |
using SkiaSharp; | |
namespace VSIMageLib | |
{ | |
class Program | |
{ | |
private static readonly XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml"; | |
static void Main(string[] args) | |
{ | |
string inputFile = args[0]; | |
var backgroundColor = SKColor.Parse(args[1]); //ThemeBackgroundColor | |
var outputFile = args.Length == 3 ? args[2] : Path.Combine(Path.GetDirectoryName(inputFile), Path.GetFileNameWithoutExtension(inputFile) + ".Dark.xaml"); | |
var doc = XDocument.Load(inputFile); | |
var currentBrushes = from element in doc.Descendants() | |
where element.Attribute(x+"Key")?.Value?.StartsWith("VSImageLib", StringComparison.OrdinalIgnoreCase) == true | |
from descendent in element.Descendants() | |
let brush = descendent.Attribute("Brush") | |
where brush is object | |
select brush; | |
backgroundColor.ToHsl(out var _, out var _, out var backgroundLuminosity); | |
foreach (var brush in currentBrushes) | |
{ | |
var color = SKColor.Parse(brush.Value); | |
color.ToHsl(out var h, out var s, out var l); | |
l = (float)SKTransformLuminosity(h,s,l,backgroundLuminosity); | |
brush.Value = SKColor.FromHsl(h, s, l, color.Alpha).ToString(); | |
} | |
doc.Save(outputFile); | |
} | |
private static double SKTransformLuminosity(double hue, double saturation, double luminosity, double backgroundLuminosity) | |
=> TransformLuminosity(hue, saturation / 100.0, luminosity / 100.0, backgroundLuminosity / 100.0) * 100.0; | |
private static double TransformLuminosity(double hue, double saturation, double luminosity, double backgroundLuminosity) | |
{ | |
if (backgroundLuminosity < 0.5) | |
{ | |
if (luminosity >= 82.0 / 85.0) | |
return backgroundLuminosity * (luminosity - 1.0) / (-3.0 / 85.0); | |
double val2 = saturation >= 0.2 ? (saturation <= 0.3 ? 1.0 - (saturation - 0.2) / (1.0 / 10.0) : 0.0) : 1.0; | |
double num1 = Math.Max(Math.Min(1.0, Math.Abs(hue - 37.0) / 20.0), val2); | |
double num2 = ((backgroundLuminosity - 1.0) * 0.66 / (82.0 / 85.0) + 1.0) * num1 + 0.66 * (1.0 - num1); | |
if (luminosity < 0.66) | |
return (num2 - 1.0) / 0.66 * luminosity + 1.0; | |
return (num2 - backgroundLuminosity) / (-259.0 / 850.0) * (luminosity - 82.0 / 85.0) + backgroundLuminosity; | |
} | |
if (luminosity < 82.0 / 85.0) | |
return luminosity * backgroundLuminosity / (82.0 / 85.0); | |
return (1.0 - backgroundLuminosity) * (luminosity - 1.0) / (3.0 / 85.0) + 1.0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment