Files
@ d16a2d816aa5
Branch filter:
Location: ATITD-Tools/Desert-Paint-Codex/Services/PaletteService.cs - annotation
d16a2d816aa5
2.0 KiB
text/x-csharp
Don't open the recipe generator log file on view creation - only open it if configured when starting recipe generation. Fixes a crash when importing reaction data / profile data.
40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 a5faa82faf6a a5faa82faf6a 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 | using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using DesertPaintCodex.Models;
using DesertPaintCodex.Util;
namespace DesertPaintCodex.Services
{
internal static class PaletteService
{
private static readonly Regex ColorEntry = new(@"\#(?<red>\w\w)(?<green>\w\w)(?<blue>\w\w)\s*(?<name>\w+)");
public static List<PaintColor> Colors { get; } = new();
private static bool _initialized = false;
public static void Initialize()
{
if (_initialized) return;
string? colorsPath = FileUtils.FindApplicationResourceFile("colors.txt");
Debug.Assert(colorsPath != null);
Load(colorsPath);
_initialized = true;
}
public static void Load(string file)
{
using StreamReader reader = new StreamReader(file);
string? line;
while ((line = reader.ReadLine()) != null)
{
Match match = ColorEntry.Match(line);
if (match.Success)
{
Colors.Add(new PaintColor(match.Groups["name"].Value,
match.Groups["red"].Value,
match.Groups["green"].Value,
match.Groups["blue"].Value));
}
}
}
public static int Count => Colors.Count;
public static string FindNearest(PaintColor color)
{
int bestDistSq = int.MaxValue;
PaintColor? bestColor = null;
foreach (PaintColor paintColor in Colors)
{
int distSq = paintColor.GetDistanceSquared(color);
if (distSq >= bestDistSq) continue;
bestDistSq = distSq;
bestColor = paintColor;
}
Debug.Assert(bestColor != null);
return bestColor.Name;
}
}
}
|