-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #515 from PixiEditor/new-palette-formats
New palette formats parsers
- Loading branch information
Showing
20 changed files
with
497 additions
and
27 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
namespace PixiEditor.Models.IO; | ||
using System.IO; | ||
|
||
namespace PixiEditor.Models.IO; | ||
|
||
internal abstract class PaletteFileParser | ||
{ | ||
public abstract Task<PaletteFileData> Parse(string path); | ||
public abstract Task Save(string path, PaletteFileData data); | ||
public abstract Task<bool> Save(string path, PaletteFileData data); | ||
public abstract string FileName { get; } | ||
public abstract string[] SupportedFileExtensions { get; } | ||
|
||
public virtual bool CanSave => true; | ||
|
||
protected static async Task<string[]> ReadTextLines(string path) | ||
{ | ||
using var stream = File.OpenText(path); | ||
string fileContent = await stream.ReadToEndAsync(); | ||
string[] lines = fileContent.Split('\n'); | ||
return lines; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System.IO; | ||
using System.Text; | ||
using PixiEditor.DrawingApi.Core.ColorsImpl; | ||
|
||
namespace PixiEditor.Models.IO.PaletteParsers; | ||
|
||
internal class GimpGplParser : PaletteFileParser | ||
{ | ||
public override string FileName { get; } = "GIMP Palette"; | ||
public override string[] SupportedFileExtensions { get; } = { ".gpl" }; | ||
|
||
public override async Task<PaletteFileData> Parse(string path) | ||
{ | ||
try | ||
{ | ||
return await ParseFile(path); | ||
} | ||
catch | ||
{ | ||
return PaletteFileData.Corrupted; | ||
} | ||
} | ||
|
||
private async Task<PaletteFileData> ParseFile(string path) | ||
{ | ||
var lines = await ReadTextLines(path); | ||
string name = Path.GetFileNameWithoutExtension(path); | ||
|
||
lines = lines.Where(x => !x.StartsWith("#") && !String.Equals(x.Trim(), "GIMP Palette", StringComparison.CurrentCultureIgnoreCase)).ToArray(); | ||
|
||
if(lines.Length == 0) return PaletteFileData.Corrupted; | ||
|
||
List<Color> colors = new(); | ||
char[] separators = new[] { '\t', ' ' }; | ||
foreach (var colorLine in lines) | ||
{ | ||
var colorParts = colorLine.Split(separators, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
if (colorParts.Length < 3) | ||
{ | ||
continue; | ||
} | ||
|
||
if(colorParts.Length < 3) continue; | ||
|
||
bool parsed = false; | ||
|
||
parsed = byte.TryParse(colorParts[0], out byte r); | ||
if(!parsed) continue; | ||
|
||
parsed = byte.TryParse(colorParts[1], out byte g); | ||
if(!parsed) continue; | ||
|
||
parsed = byte.TryParse(colorParts[2], out byte b); | ||
if(!parsed) continue; | ||
|
||
var color = new Color(r, g, b, 255); // alpha is ignored in PixiEditor | ||
if (colors.Contains(color)) continue; | ||
|
||
colors.Add(color); | ||
} | ||
|
||
return new PaletteFileData(name, colors.ToArray()); | ||
} | ||
|
||
public override async Task<bool> Save(string path, PaletteFileData data) | ||
{ | ||
StringBuilder sb = new(); | ||
string name = string.IsNullOrEmpty(data.Title) ? Path.GetFileNameWithoutExtension(path) : data.Title; | ||
sb.AppendLine("GIMP Palette"); | ||
sb.AppendLine($"#Name: {name}"); | ||
sb.AppendLine($"#Colors {data.Colors.Length}"); | ||
sb.AppendLine("#Made with PixiEditor"); | ||
sb.AppendLine("#"); | ||
foreach (var color in data.Colors) | ||
{ | ||
string hex = $"{color.R:X}{color.G:X}{color.B:X}"; | ||
sb.AppendLine($"{color.R}\t{color.G}\t{color.B}\t{hex}"); | ||
} | ||
|
||
try | ||
{ | ||
await File.WriteAllTextAsync(path, sb.ToString()); | ||
return true; | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/PixiEditor/Models/IO/PaletteParsers/HexPaletteParser.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Text; | ||
using PixiEditor.DrawingApi.Core.ColorsImpl; | ||
|
||
namespace PixiEditor.Models.IO.PaletteParsers; | ||
|
||
internal class HexPaletteParser : PaletteFileParser | ||
{ | ||
public override string FileName { get; } = "Hex Palette"; | ||
public override string[] SupportedFileExtensions { get; } = { ".hex" }; | ||
public override async Task<PaletteFileData> Parse(string path) | ||
{ | ||
try | ||
{ | ||
return await ParseFile(path); | ||
} | ||
catch | ||
{ | ||
return PaletteFileData.Corrupted; | ||
} | ||
} | ||
|
||
private async Task<PaletteFileData> ParseFile(string path) | ||
{ | ||
var lines = await ReadTextLines(path); | ||
string name = Path.GetFileNameWithoutExtension(path); | ||
|
||
List<Color> colors = new(); | ||
foreach (var colorLine in lines) | ||
{ | ||
if (colorLine.Length < 6) | ||
continue; | ||
|
||
byte r = byte.Parse(colorLine.Substring(0, 2), NumberStyles.HexNumber); | ||
byte g = byte.Parse(colorLine.Substring(2, 2), NumberStyles.HexNumber); | ||
byte b = byte.Parse(colorLine.Substring(4, 2), NumberStyles.HexNumber); | ||
var color = new Color(r, g, b, 255); // alpha is ignored in PixiEditor | ||
if (colors.Contains(color)) continue; | ||
|
||
colors.Add(color); | ||
} | ||
|
||
return new PaletteFileData(name, colors.ToArray()); | ||
} | ||
|
||
public override async Task<bool> Save(string path, PaletteFileData data) | ||
{ | ||
StringBuilder sb = new(); | ||
foreach (var color in data.Colors) | ||
{ | ||
string hex = $"{color.R:X}{color.G:X}{color.B:X}"; | ||
sb.AppendLine(hex); | ||
} | ||
|
||
try | ||
{ | ||
await File.WriteAllTextAsync(path, sb.ToString()); | ||
return true; | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...odels/IO/JascPalFile/JascFileException.cs → ...eParsers/JascPalFile/JascFileException.cs
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
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
76 changes: 76 additions & 0 deletions
76
src/PixiEditor/Models/IO/PaletteParsers/PaintNetTxtParser.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Text; | ||
using PixiEditor.DrawingApi.Core.ColorsImpl; | ||
using PixiEditor.Helpers; | ||
|
||
namespace PixiEditor.Models.IO.PaletteParsers; | ||
|
||
// https://www.getpaint.net/doc/latest/WorkingWithPalettes.html | ||
|
||
internal class PaintNetTxtParser : PaletteFileParser | ||
{ | ||
public override string FileName { get; } = "Paint.NET Palette"; | ||
public override string[] SupportedFileExtensions { get; } = new string[] { ".txt" }; | ||
public override async Task<PaletteFileData> Parse(string path) | ||
{ | ||
try | ||
{ | ||
return await ParseFile(path); | ||
} | ||
catch | ||
{ | ||
return PaletteFileData.Corrupted; | ||
} | ||
} | ||
|
||
private static async Task<PaletteFileData> ParseFile(string path) | ||
{ | ||
var lines = await ReadTextLines(path); | ||
string name = Path.GetFileNameWithoutExtension(path); | ||
|
||
lines = lines.Where(x => !x.StartsWith(";")).ToArray(); | ||
|
||
List<Color> colors = new(); | ||
for (int i = 0; i < lines.Length; i++) | ||
{ | ||
// Color format aarrggbb | ||
string colorLine = lines[i]; | ||
if(colorLine.Length < 8) | ||
continue; | ||
|
||
byte a = byte.Parse(colorLine.Substring(0, 2), NumberStyles.HexNumber); | ||
byte r = byte.Parse(colorLine.Substring(2, 2), NumberStyles.HexNumber); | ||
byte g = byte.Parse(colorLine.Substring(4, 2), NumberStyles.HexNumber); | ||
byte b = byte.Parse(colorLine.Substring(6, 2), NumberStyles.HexNumber); | ||
var color = new Color(r, g, b, 255); // alpha is ignored in PixiEditor | ||
if(colors.Contains(color)) continue; | ||
|
||
colors.Add(color); | ||
} | ||
|
||
return new PaletteFileData(name, colors.ToArray()); | ||
} | ||
|
||
public override async Task<bool> Save(string path, PaletteFileData data) | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
sb.AppendLine("; Paint.NET Palette File"); | ||
sb.AppendLine($"; Made using PixiEditor {VersionHelpers.GetCurrentAssemblyVersion().ToString()}"); | ||
sb.AppendLine($"; {data.Colors.Length} colors"); | ||
foreach (Color color in data.Colors) | ||
{ | ||
sb.AppendLine(color.ToHex()); | ||
} | ||
|
||
try | ||
{ | ||
await File.WriteAllTextAsync(path, sb.ToString()); | ||
return true; | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.