/// <summary> /// Initializes a new instance of the <see cref="ColourCollection"/> class that contains elements copied from the specified collection. /// </summary> /// <param name="collection">The collection whose elements are copied to the new collection.</param> public ColourCollection(ColourCollection collection) : this() { for (int i = 0; i < collection.Count; i++) { this.Add(collection[i]); #if USENAMEHACK this.SetName(i, collection.GetName(i)); #endif } }
/// <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 override void Serialise(Stream stream, ColourCollection palette) { int swatchIndex; if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (palette == null) { throw new ArgumentNullException(nameof(palette)); } swatchIndex = 0; // TODO: Allow name and columns attributes to be specified using (StreamWriter writer = new StreamWriter(stream, Encoding.ASCII)) { writer.WriteLine("GIMP Palette"); writer.WriteLine("Name: "); writer.WriteLine("Columns: 8"); writer.WriteLine("#"); foreach (Color colour in palette) { writer.Write("{0,-3} ", colour.R); writer.Write("{0,-3} ", colour.G); writer.Write("{0,-3} ", colour.B); #if USENAMEHACK writer.Write(palette.GetName(swatchIndex)); #else if (colour.IsNamedColor) { writer.Write(colour.Name); } else { writer.Write("#{0:X2}{1:X2}{2:X2} Swatch {3}", colour.R, colour.G, colour.B, swatchIndex); } #endif writer.WriteLine(); swatchIndex++; } } }
protected virtual void WritePalette(Stream stream, ColourCollection palette, AdobePhotoshopColourSwatchFileVersion version, AdobePhotoshopColourSwatchColourSpace colorSpace) { int swatchIndex; this.WriteInt16(stream, (short)version); this.WriteInt16(stream, (short)palette.Count); swatchIndex = 0; foreach (Color colour in palette) { short value1; short value2; short value3; short value4; swatchIndex++; switch (colorSpace) { case AdobePhotoshopColourSwatchColourSpace.RGB: value1 = (short)(colour.R * 256); value2 = (short)(colour.G * 256); value3 = (short)(colour.B * 256); value4 = 0; break; case AdobePhotoshopColourSwatchColourSpace.HSB: value1 = (short)(colour.GetHue() * 182.04); value2 = (short)(colour.GetSaturation() * 655.35); value3 = (short)(colour.GetBrightness() * 655.35); value4 = 0; break; case AdobePhotoshopColourSwatchColourSpace.GRAYSCALE: if (colour.R == colour.G && colour.R == colour.B) { // already grayscale value1 = (short)(colour.R * 39.0625); } else { // color is not grayscale, convert value1 = (short)((colour.R + colour.G + colour.B) / 3.0 * 39.0625); } value2 = 0; value3 = 0; value4 = 0; break; default: throw new InvalidOperationException("Color space not supported."); } this.WriteInt16(stream, (short)colorSpace); this.WriteInt16(stream, value1); this.WriteInt16(stream, value2); this.WriteInt16(stream, value3); this.WriteInt16(stream, value4); if (version == AdobePhotoshopColourSwatchFileVersion.VERSION2) { string name; #if USENAMEHACK name = palette.GetName(swatchIndex - 1); if (string.IsNullOrEmpty(name)) { name = string.Format("Swatch {0}", swatchIndex); } #else name = colour.IsNamedColor ? colour.Name : string.Format("Swatch {0}", swatchIndex); #endif this.WriteInt32(stream, name.Length); this.WriteString(stream, name); } } }