diff --git a/Models/PaletteModel.cs b/Models/PaletteModel.cs index 2861093..d8acada 100644 --- a/Models/PaletteModel.cs +++ b/Models/PaletteModel.cs @@ -32,26 +32,23 @@ namespace fxl.codes.kisekae.Models public string FileName { get; } public string Comment { get; } - - public Color[] Colors { get; private set; } - - public void ParseColors(byte[] bytes, int depth) - { - Colors = new Color[0]; - } + public Color[] Colors { get; set; } } public class Color { - public Color(int red, int green, int blue) + public Color(int red, int green, int blue, int group, int depth) { - Red = red; - Green = green; - Blue = blue; + var multiplier = depth == 12 ? 16 : 1; + Red = red * multiplier; + Green = green * multiplier; + Blue = blue * multiplier; + Group = group; } public int Red { get; } public int Green { get; } public int Blue { get; } + public int Group { get; } } } \ No newline at end of file diff --git a/Scripts/main.ts b/Scripts/main.ts index 0c04366..b144ed0 100644 --- a/Scripts/main.ts +++ b/Scripts/main.ts @@ -17,6 +17,9 @@ export default class Main { init() { let main = document.querySelector("main") let directories = main.querySelector("ul") + + if (!directories) return + let links = directories.querySelectorAll("a") links.forEach(link => { diff --git a/Services/FileParserService.cs b/Services/FileParserService.cs index f24e79a..04e23db 100644 --- a/Services/FileParserService.cs +++ b/Services/FileParserService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.IO; using System.IO.IsolatedStorage; using System.Linq; @@ -31,8 +32,51 @@ namespace fxl.codes.kisekae.Services var header = Encoding.ASCII.GetBytes("KiSS"); if (buffer[..header.Length].SequenceEqual(header)) { - // Matching header I guess? + // Verify palette mark? + if (buffer[4] != 16) throw new InvalidDataException("File provided is not a valid palette file"); + var colorDepth = Convert.ToInt32(buffer[5]); + var colorsPerGroup = BitConverter.ToInt16(buffer, 8); + var groups = BitConverter.ToInt16(buffer, 10); + + palette.Colors = GetColors(new BitArray(buffer[32..]), colorDepth, colorsPerGroup, groups); } + else + { + palette.Colors = GetColors(new BitArray(buffer)); + } + } + + private Color[] GetColors(BitArray bitArray, int depth = 12, int colorsPerGroup = 16, int groups = 10) + { + _logger.LogTrace($"Converting bit array to colors"); + var colorCounter = 0; + var colors = new Color[colorsPerGroup * groups]; + var data = new bool[bitArray.Length]; + bitArray.CopyTo(data, 0); + + var length = depth / 3; + for (var index = 0; index < colorsPerGroup * groups; index += depth) + { + var greenStart = index + length; + var blueStart = index + length * 2; + + var red = GetValue(data[index..greenStart]); + var green = GetValue(data[greenStart..blueStart]); + var blue = GetValue(data[blueStart..(index + depth)]); + colors[colorCounter] = new Color(red, green, blue, colorCounter / colorsPerGroup, depth); + colorCounter++; + } + + return colors; + } + + private static int GetValue(bool[] data) + { + var bits = new BitArray(data); + var value = new int[1]; + bits.CopyTo(value, 0); + + return value[0]; } } } \ No newline at end of file