Files
@ 7117d2e703c8
Branch filter:
Location: ATITD-Tools/Desert-Paint-Codex/Services/PaletteService.cs - annotation
7117d2e703c8
2.0 KiB
text/x-csharp
Updated scanner for Tale 10, and fixed several bugs.
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 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);
}
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;
}
}
}
|