// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Drawing; namespace Spludlow.Drawing { public class ACT { private Dictionary ColourTables; public ACT() { List items = new List(); foreach (string filename in Directory.GetFiles(Spludlow.Config.ProgramData + @"\Data\ACT", "*.act")) items.Add(Path.GetFileNameWithoutExtension(filename) + "\t" + filename); this.Setup(items.ToArray()); } public ACT(string[] keyAndFilenames) { this.Setup(keyAndFilenames); } public void Setup(string[] keyAndFilenames) { this.ColourTables = new Dictionary(); foreach (string line in keyAndFilenames) { string[] words = Spludlow.Text.Split(line, '\t', true, false); if (words.Length != 2) throw new ApplicationException("ACT; Expected 'key[TAB]filename':\t" + line); string key = words[0]; string filename = words[1]; if (filename.Contains(@"\") == false) filename = Spludlow.Config.ProgramData + @"\Data\ACT\" + filename; if (File.Exists(filename) == false) throw new ApplicationException("ACT; File does not exist, filename':\t" + filename); this.ColourTables.Add(key, File.ReadAllBytes(filename)); } } public byte[] Get(string key) { return this.ColourTables[key]; } public static Color[] Load(string filename) { List colours = new List(); byte[] data = File.ReadAllBytes(filename); for (int colourIndex = 0; colourIndex < 256; ++colourIndex) { int startIndex = colourIndex * 3; int red = data[startIndex + 0]; int green = data[startIndex + 1]; int blue = data[startIndex + 2]; colours.Add(Color.FromArgb(red, green, blue)); } return colours.ToArray(); } public static void Save(Color[] colours, string filename) { byte[] data = Make(colours); File.WriteAllBytes(filename, data); } public static byte[] Make(Color[] colours) { byte[] data = new byte[(256 * 3)]; for (int colourIndex = 0; colourIndex < 256; ++colourIndex) { int startIndex = colourIndex * 3; data[startIndex + 0] = colours[colourIndex].R; data[startIndex + 1] = colours[colourIndex].G; data[startIndex + 2] = colours[colourIndex].B; } return data; } public static Color[] MakeRange(Color[] colours) { double parts = colours.Length - 1; double size = 256 / parts; Color[] colourRange = new Color[256]; for (int part = 0; part < parts; ++part) { double startIndex = part * size; double endIndex = ((part + 1) * size) - 1; double width = endIndex - startIndex; Color startColour = colours[part]; Color endColour = colours[part + 1]; double redStep = (double)(endColour.R - startColour.R) / width; double greenStep = (double)(endColour.G - startColour.G) / width; double blueStep = (double)(endColour.B - startColour.B) / width; startIndex = Math.Round(startIndex, 0); endIndex = Math.Round(endIndex, 0); Console.WriteLine(part.ToString() + "\t" + startIndex.ToString() + "\t" + endIndex.ToString()); for (int index = (int)startIndex; index <= endIndex; ++index) { if (index == (int)startIndex) { colourRange[index] = startColour; } else { if (index == (int)endIndex) { colourRange[index] = endColour; } else { colourRange[index] = Color.FromArgb( (int)Math.Round(startColour.R + (redStep * (index - startIndex)), 0), (int)Math.Round(startColour.G + (greenStep * (index - startIndex)), 0), (int)Math.Round(startColour.B + (blueStep * (index - startIndex)), 0) ); } } } } return colourRange; } } }