Files
@ 5e28ba3945f7
Branch filter:
Location: ATITD-Tools/Desert-Paint-Codex/Services/PaletteService.cs - annotation
5e28ba3945f7
2.0 KiB
text/x-csharp
Recipe count is now a ulong instead of an int so it can show values > 2.47 billion. Also, don't allow clearing the recipe list while the recipe generator is running.
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;
}
}
}
|