//#region Adobe ASE (currently doesn't work) //// this file format, in particular, suffers from the lack of advanced features which this library doesn't need //// all support/handling for color groups, as well as a lot of other data, is simply stripped out //// so all that remains is a flat list of colors //// this is probably on the verge of unhelpful for users with large/organized palette files, so this may be something to revisit later on //public static List<Color> LoadAsePalette(string filename) //{ // using (Stream stream = File.OpenRead(filename)) // { // List<Color> colors = new List<Color>(); // int blockCount; // ReadAndValidateVersion(stream); // blockCount = stream.ReadUInt32BigEndian(); // for (int i = 0; i < blockCount; i++) // { // int blockType; // blockType = stream.ReadUInt16BigEndian(); // // 0x0001 denotes a color block, which is all we're looking for here // if (blockType == 0x0001) // { // colors.Add(ReadColorBlock(stream)); // } // } // return colors; // } //} //private static void ReadAndValidateVersion(Stream stream) //{ // string signature; // int majorVersion; // int minorVersion; // // get the signature (4 ascii characters) // signature = stream.ReadAsciiString(4); // if (signature != "ASEF") // { // throw new InvalidDataException("Invalid file format."); // } // // read the version // majorVersion = stream.ReadUInt16BigEndian(); // minorVersion = stream.ReadUInt16BigEndian(); // if (majorVersion != 1 && minorVersion != 0) // { // throw new InvalidDataException("Invalid version information."); // } //} //private static Color ReadColorBlock(Stream stream) //{ // string colorMode; // int r; // int g; // int b; // // get the mode of the color, which is stored // // as four ASCII characters // colorMode = stream.ReadAsciiString(4); // // read the color data // // how much data we need to read depends on the // // color mode we previously read // switch (colorMode) // { // case "RGB ": // // RGB is comprised of three floating point values ranging from 0-1.0 // float value1; // float value2; // float value3; // value1 = stream.ReadSingleBigEndian(); // value2 = stream.ReadSingleBigEndian(); // value3 = stream.ReadSingleBigEndian(); // r = Convert.ToInt32(value1 * 255); // g = Convert.ToInt32(value2 * 255); // b = Convert.ToInt32(value3 * 255); // break; // case "CMYK": // // CMYK is comprised of four floating point values // throw new InvalidDataException($"Unsupported color mode '{colorMode}'."); // case "LAB ": // // LAB is comprised of three floating point values // throw new InvalidDataException($"Unsupported color mode '{colorMode}'."); // case "Gray": // // Grayscale is comprised of a single floating point value // throw new InvalidDataException($"Unsupported color mode '{colorMode}'."); // default: // throw new InvalidDataException($"Unsupported color mode '{colorMode}'."); // } // return Color.FromRgb((byte)r, (byte)g, (byte)b); //} //#endregion #region Paint.NET/GIMP /// <summary> /// Loads colors from a PAINT.NET or basic HEX color palette file (.txt;.hex). /// </summary> /// <param name="filename">The file to load.</param> /// <exception cref="ArgumentException">Thrown if the file cannot be opened.</exception> public static List <Color> LoadPaintNetPalette(string filename) { using (Stream stream = File.OpenRead(filename)) { if (stream == null) { throw new ArgumentException("stream"); } List <Color> results = new List <Color>(); using (StreamReader reader = new StreamReader(stream)) { while (!reader.EndOfStream) { string line; line = reader.ReadLine() ?? ""; if (!string.IsNullOrEmpty(line) && !line.StartsWith(";")) { try { results.Add(ColorsHelper.CreateFromHex(line)); } catch (FormatException) { Color?c = LoadRgbLine(line); if (c.HasValue) { results.Add(c.Value); } } } } } return(results); } }
private static List <Color> ReadSwatches(Stream stream, FileVersion version) { int colorCount; List <Color> results; results = new List <Color>(); // read the number of colors, which also occupies two bytes colorCount = ReadInt16(stream); for (int i = 0; i < colorCount; i++) { ColorSpace colorSpace; int value1; int value2; int value3; int value4; // again, two bytes for the color space colorSpace = (ColorSpace)(ReadInt16(stream)); value1 = ReadInt16(stream); value2 = ReadInt16(stream); value3 = ReadInt16(stream); value4 = ReadInt16(stream); if (version == FileVersion.Version2) { int length; // need to read the name even though currently our colour collection doesn't support names length = ReadInt32(stream); ReadString(stream, length); } switch (colorSpace) { case ColorSpace.Rgb: int red; int green; int blue; // RGB. // The first three values in the color data are red , green , and blue . They are full unsigned // 16-bit values as in Apple's RGBColor data structure. Pure red = 65535, 0, 0. red = value1 / 256; // 0-255 green = value2 / 256; // 0-255 blue = value3 / 256; // 0-255 results.Add(Color.FromRgb((byte)red, (byte)green, (byte)blue)); break; case ColorSpace.Hsb: double hue; double saturation; double brightness; // HSB. // The first three values in the color data are hue , saturation , and brightness . They are full // unsigned 16-bit values as in Apple's HSVColor data structure. Pure red = 0,65535, 65535. hue = value1 / 182.04; // 0-359 saturation = value2 / 655.35 / 100; // 0-1.0 brightness = value3 / 655.35 / 100; // 0-1.0 results.Add(ColorsHelper.CreateFromHSV(hue, saturation, brightness)); break; case ColorSpace.Grayscale: int gray; // Grayscale. // The first value in the color data is the gray value, from 0...10000. gray = (int)(value1 / 39.0625); // 0-255 results.Add(Color.FromRgb((byte)gray, (byte)gray, (byte)gray)); break; default: throw new InvalidDataException(string.Format("Color space '{0}' not supported.", colorSpace)); } } return(results); }
/// <summary> /// Create a brush based upon a single color. /// </summary> /// <param name="hex">The hex value of the color to use.</param> /// <returns>A SolidColorBrush that includes the color.</returns> public static SolidColorBrush Create(string hex) { return(new SolidColorBrush(ColorsHelper.CreateFromHex(hex))); }