-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGplPaletteParser.cs
More file actions
67 lines (52 loc) · 2.21 KB
/
GplPaletteParser.cs
File metadata and controls
67 lines (52 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using Stride.Core.Mathematics;
namespace Stride.Core.Assets.Editor.ViewModel
{
public static class GplPaletteParser
{
public static Dictionary<string, Color3>? TryParse(string filePath)
{
if (!File.Exists(filePath))
return null;
var colors = new Dictionary<string, Color3>();
try
{
var lines = File.ReadAllLines(filePath);
if (lines.Length == 0 || !lines[0].Trim().Equals("GIMP Palette", StringComparison.OrdinalIgnoreCase))
return null;
int index = 1;
int unnamedIndex = 1;
for (; index < lines.Length; index++)
{
var line = lines[index].Trim();
if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#") || line.StartsWith("Name:") || line.StartsWith("Columns:"))
continue;
var parts = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 3)
continue;
if (!byte.TryParse(parts[0], out byte r) ||
!byte.TryParse(parts[1], out byte g) ||
!byte.TryParse(parts[2], out byte b))
continue;
var name = parts.Length >= 4
? string.Join(" ", parts, 3, parts.Length - 3).Trim()
: $"Color {unnamedIndex++}";
var key = name;
int suffix = 2;
while (colors.ContainsKey(key))
key = $"{name} {suffix++}";
colors[key] = new Color3(r / 255f, g / 255f, b / 255f);
}
return colors.Count > 0 ? colors : null;
}
catch (Exception)
{
return null;
}
}
}
}