using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; namespace OpenVIII { public class Cluts : IDictionary { #region Fields private Dictionary Clut; #endregion Fields #region Constructors public Cluts(Dictionary clut, bool clone = true) { if (clone) Clut = clut.ToDictionary(id => id.Key, colors => (Color[])colors.Value.Clone()); else Clut = clut; } public Cluts(Cluts clut) : this(clut.Clut) { } public Cluts() => Clut = new Dictionary(); #endregion Constructors #region Properties public IReadOnlyList ClutIDs => Clut.Keys.OrderBy(x => x).ToList(); public int Count => ((IDictionary)Clut).Count; public bool IsReadOnly => ((IDictionary)Clut).IsReadOnly; public ICollection Keys => ((IDictionary)Clut).Keys; public int MaxColors => Clut.Values.Max(x => x.Length); public ICollection Values => ((IDictionary)Clut).Values; private byte MaxClut => Keys.Max(x => x); #endregion Properties #region Indexers public Color[] this[byte key] { get => ((IDictionary)Clut)[key]; set => ((IDictionary)Clut)[key] = value; } #endregion Indexers #region Methods public void Add(byte key, Color[] value) => ((IDictionary)Clut).Add(key, value); public void Add(KeyValuePair item) => ((IDictionary)Clut).Add(item); public void Clear() => ((IDictionary)Clut).Clear(); public bool Contains(KeyValuePair item) => ((IDictionary)Clut).Contains(item); public bool ContainsKey(byte key) => ((IDictionary)Clut).ContainsKey(key); public void CopyTo(KeyValuePair[] array, int arrayIndex) => ((IDictionary)Clut).CopyTo(array, arrayIndex); public IEnumerator> GetEnumerator() => ((IDictionary)Clut).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => ((IDictionary)Clut).GetEnumerator(); public bool Remove(byte key) => ((IDictionary)Clut).Remove(key); public bool Remove(KeyValuePair item) => ((IDictionary)Clut).Remove(item); public void Save(string path) { using (var clutTexture = new Texture2D(Memory.Graphics.GraphicsDevice, MaxColors, MaxClut + 1)) { foreach (var _Y_Colors in Clut.OrderBy(x => x.Key)) { var colors = _Y_Colors.Value; var y = _Y_Colors.Key; clutTexture.SetData(0, new Rectangle(0, y, colors.Length, 1), colors, 0, colors.Length); } Extended.Save_As_PNG(clutTexture, path, clutTexture.Width, clutTexture.Height); } } public bool TryGetValue(byte key, out Color[] value) => ((IDictionary)Clut).TryGetValue(key, out value); #endregion Methods } }