Пример #1
0
 /// <summary>
 /// Serializes the specified <see cref="ColourCollection" /> and writes the palette to a file using the specified Stream.
 /// </summary>
 /// <param name="stream">The <see cref="Stream" /> used to write the palette.</param>
 /// <param name="palette">The <see cref="ColourCollection" /> to serialize.</param>
 void IPaletteSerialiser.Serialise(Stream stream, ColourCollection palette)
 {
     this.Serialise(stream, palette);
 }
        /// <summary>
        /// Deserializes the <see cref="ColourCollection" /> contained by the specified <see cref="Stream" />.
        /// </summary>
        /// <param name="stream">The <see cref="Stream" /> that contains the palette to deserialize.</param>
        /// <returns>The <see cref="ColourCollection" /> being deserialized.</returns>
        public override ColourCollection Deserialise(Stream stream)
        {
            ColourCollection results;

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            results = new ColourCollection();

            using (StreamReader reader = new StreamReader(stream))
            {
                string header;
                int    swatchIndex;
                bool   readingPalette;

                readingPalette = false;

                // check signature
                header = reader.ReadLine();

                if (header != "GIMP Palette")
                {
                    throw new InvalidDataException("Invalid palette file");
                }

                // read the swatches
                swatchIndex = 0;

                while (!reader.EndOfStream)
                {
                    string data;

                    data = reader.ReadLine();

                    if (!string.IsNullOrEmpty(data))
                    {
                        if (data[0] == '#')
                        {
                            // comment
                            readingPalette = true;
                        }
                        else if (!readingPalette)
                        {
                            // custom attribute
                        }
                        else if (readingPalette)
                        {
                            int      r;
                            int      g;
                            int      b;
                            string[] parts;
                            string   name;

                            // TODO: Optimize this a touch. Microoptimization? Maybe.

                            parts = !string.IsNullOrEmpty(data) ? data.Split(new[]
                            {
                                ' ',
                                '\t'
                            }, StringSplitOptions.RemoveEmptyEntries) : new string[0];
                            name = parts.Length > 3 ? string.Join(" ", parts, 3, parts.Length - 3) : null;

                            if (!int.TryParse(parts[0], out r) || !int.TryParse(parts[1], out g) || !int.TryParse(parts[2], out b))
                            {
                                throw new InvalidDataException(string.Format("Invalid palette contents found with data '{0}'", data));
                            }

                            results.Add(Color.FromArgb(r, g, b));
#if USENAMEHACK
                            results.SetName(swatchIndex, name);
#endif

                            swatchIndex++;
                        }
                    }
                }
            }

            return(results);
        }
Пример #3
0
 /// <summary>
 /// Serializes the specified <see cref="ColourCollection" /> and writes the palette to a file using the specified <see cref="Stream"/>.
 /// </summary>
 /// <param name="stream">The <see cref="Stream" /> used to write the palette.</param>
 /// <param name="palette">The <see cref="ColourCollection" /> to serialize.</param>
 public abstract void Serialise(Stream stream, ColourCollection palette);