/// <summary>
    /// Creates the DB32 palette.
    /// </summary>
    /// <remarks>http://www.pixeljoint.com/forum/forum_posts.asp?TID=16247</remarks>
    /// <param name="pad">if set to <c>true</c> the palette is padded with black to fill 256 entries.</param>
    protected ColorCollection CreateDawnBringer32Palette(bool pad)
    {
      ColorCollection results;

      results = new ColorCollection();

      results.Add(Color.FromArgb(0, 0, 0));
      results.Add(Color.FromArgb(34, 32, 52));
      results.Add(Color.FromArgb(69, 40, 60));
      results.Add(Color.FromArgb(102, 57, 49));
      results.Add(Color.FromArgb(143, 86, 59));
      results.Add(Color.FromArgb(223, 113, 38));
      results.Add(Color.FromArgb(217, 160, 102));
      results.Add(Color.FromArgb(238, 195, 154));
      results.Add(Color.FromArgb(251, 242, 54));
      results.Add(Color.FromArgb(153, 229, 80));
      results.Add(Color.FromArgb(106, 190, 48));
      results.Add(Color.FromArgb(55, 148, 110));
      results.Add(Color.FromArgb(75, 105, 47));
      results.Add(Color.FromArgb(82, 75, 36));
      results.Add(Color.FromArgb(50, 60, 57));
      results.Add(Color.FromArgb(63, 63, 116));
      results.Add(Color.FromArgb(48, 96, 130));
      results.Add(Color.FromArgb(91, 110, 225));
      results.Add(Color.FromArgb(99, 155, 255));
      results.Add(Color.FromArgb(95, 205, 228));
      results.Add(Color.FromArgb(203, 219, 252));
      results.Add(Color.FromArgb(255, 255, 255));
      results.Add(Color.FromArgb(155, 173, 183));
      results.Add(Color.FromArgb(132, 126, 135));
      results.Add(Color.FromArgb(105, 106, 106));
      results.Add(Color.FromArgb(89, 86, 82));
      results.Add(Color.FromArgb(118, 66, 138));
      results.Add(Color.FromArgb(172, 50, 50));
      results.Add(Color.FromArgb(217, 87, 99));
      results.Add(Color.FromArgb(215, 123, 186));
      results.Add(Color.FromArgb(143, 151, 74));
      results.Add(Color.FromArgb(138, 111, 48));

      if (pad)
      {
        while (results.Count < 256)
        {
          results.Add(Color.FromArgb(0, 0, 0));
        }
      }

      return results;
    }
        /// <summary>
        ///     Deserializes the <see cref="ColorCollection" /> 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="ColorCollection" /> being deserialized..</returns>
        public override ColorCollection Deserialize(Stream stream)
        {
            ColorCollection results;

            if (stream == null)
                throw new ArgumentNullException("stream");

            results = new ColorCollection();

            using (var reader = new StreamReader(stream))
            {
                while (!reader.EndOfStream)
                {
                    string line;

                    line = reader.ReadLine();
                    if (!string.IsNullOrEmpty(line) && !line.StartsWith(";") && line.Length == 8)
                    {
                        int a;
                        int r;
                        int g;
                        int b;

                        a = int.Parse(line.Substring(0, 2), NumberStyles.HexNumber);
                        r = int.Parse(line.Substring(2, 2), NumberStyles.HexNumber);
                        g = int.Parse(line.Substring(4, 2), NumberStyles.HexNumber);
                        b = int.Parse(line.Substring(6, 2), NumberStyles.HexNumber);

                        results.Add(Color.FromArgb(a, r, g, b));
                    }
                }
            }

            return results;
        }
Exemplo n.º 3
0
        private void colorHandle()
        {
            LinearScale linearScale = new LinearScale();

            linearScale.MinimumValue = 0;
            linearScale.MaximumValue = 100;

            ColorCollection colors = new ColorCollection();

            colors.Add(System.Windows.Media.Colors.Blue);
            colors.Add(System.Windows.Media.Colors.Pink);
            colors.Add(System.Windows.Media.Colors.Red);


            linearScale.Colors = colors;

            SetColors = linearScale;
        }
Exemplo n.º 4
0
        public void FindIgnoreAlphaNegativeTest()
        {
            // arrange
            ColorCollection target;
            int             expected;
            int             actual;

            target = new ColorCollection();
            target.Add(Color.Black);
            target.Add(Color.CornflowerBlue);
            target.Add(Color.Violet);
            expected = -1;

            // act
            actual = target.Find(Color.FromArgb(128, Color.CornflowerBlue), false);

            // assert
            actual.Should().Be(expected);
        }
Exemplo n.º 5
0
        public void FindNegativeTest()
        {
            // arrange
            ColorCollection target;
            int             expected;
            int             actual;

            target = new ColorCollection();
            target.Add(Color.Black);
            target.Add(Color.CornflowerBlue);
            target.Add(Color.Violet);
            expected = -1;

            // act
            actual = target.Find(Color.Yellow);

            // assert
            actual.Should().Be(expected);
        }
Exemplo n.º 6
0
        public void FindTest()
        {
            // arrange
            ColorCollection target;
            int             expected;
            int             actual;

            target = new ColorCollection();
            target.Add(Color.Black);
            target.Add(Color.CornflowerBlue);
            target.Add(Color.Violet);
            expected = 1;

            // act
            actual = target.Find(Color.FromArgb(100, 149, 237));

            // assert
            actual.Should().Be(expected);
        }
Exemplo n.º 7
0
        public PaletteDialog()
        {
            InitializeComponent();

            for (int i = 0; i < 32; i++)
            {
                colors.Add(Color.Red);
            }
            PaletteUpload();
        }
Exemplo n.º 8
0
        private void colorHandle()
        {
            LinearScale linearScale = new LinearScale();

            //Heatmap heatmap = new Heatmap();

            linearScale.MinimumValue = 0;
            linearScale.MaximumValue = 100;

            ColorCollection colors = new ColorCollection();

            colors.Add(System.Windows.Media.Colors.Blue);
            colors.Add(System.Windows.Media.Colors.Pink);
            colors.Add(System.Windows.Media.Colors.Red);

            //linearScale.Colors =new[] { System.Windows.Media.Colors.Blue, System.Windows.Media.Colors.Pink, System.Windows.Media.Colors.Red };

            linearScale.Colors = colors;

            //heatmap.ValueScale = linearScale;
            SetColors = linearScale;
        }
    /// <summary>
    /// Deserializes the <see cref="ColorCollection" /> 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="ColorCollection" /> being deserialized..</returns>
    public override ColorCollection Deserialize(Stream stream)
    {
      ColorCollection results;

      if (stream == null)
        throw new ArgumentNullException("stream");

      results = new ColorCollection();

      using (StreamReader reader = new StreamReader(stream))
      {
        string header;
        string startHeader;

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

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

        while (startHeader != "#")
          startHeader = reader.ReadLine();

        while (!reader.EndOfStream)
        {
          int r;
          int g;
          int b;
          string data;
          string[] parts;

          data = reader.ReadLine();
          parts = !string.IsNullOrEmpty(data) ? data.Split(new[]
          {
            ' ', '\t'
          }, StringSplitOptions.RemoveEmptyEntries) : new string[0];

          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));
        }
      }

      return results;
    }
    /// <summary>
    /// Deserializes the <see cref="ColorCollection" /> 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="ColorCollection" /> being deserialized..</returns>
    public override ColorCollection Deserialize(Stream stream)
    {
      ColorCollection results;

      if (stream == null)
        throw new ArgumentNullException("stream");

      results = new ColorCollection();

      using (StreamReader reader = new StreamReader(stream))
      {
        string header;
        string version;
        int colorCount;

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

        if (header != "JASC-PAL" || version != "0100")
          throw new InvalidDataException("Invalid palette file");

        colorCount = Convert.ToInt32(reader.ReadLine());
        for (int i = 0; i < colorCount; i++)
        {
          int r;
          int g;
          int b;
          string data;
          string[] parts;

          data = reader.ReadLine();
          parts = !string.IsNullOrEmpty(data) ? data.Split(new[]
          {
            ' ', '\t'
          }, StringSplitOptions.RemoveEmptyEntries) : new string[0];

          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));
        }
      }

      return results;
    }
    public static ColorCollection ScaledPalette(IEnumerable<Color> topRow)
    {
      ColorCollection results;

      results = new ColorCollection();

      topRow = topRow.ToArray();
      results.AddRange(topRow);

      for (int i = 5; i >= 0; i--)
      {
        foreach (Color color in topRow)
        {
          HslColor hsl;

          hsl = new HslColor(color);
          hsl.L = (5 + i + (16 * i)) / 100D;

          results.Add(hsl.ToRgbColor());
        }
      }

      return results;
    }
    protected virtual ColorCollection ReadPalette(Stream stream, AdobePhotoshopColorSwatchFileVersion version)
    {
      int colorCount;
      ColorCollection results;

      results = new ColorCollection();

      // read the number of colors, which also occupies two bytes
      colorCount = this.ReadInt16(stream);

      for (int i = 0; i < colorCount; i++)
      {
        AdobePhotoshopColorSwatchColorSpace colorSpace;
        int value1;
        int value2;
        int value3;
        string name;

        // again, two bytes for the color space
        colorSpace = (AdobePhotoshopColorSwatchColorSpace)(this.ReadInt16(stream));

        value1 = this.ReadInt16(stream);
        value2 = this.ReadInt16(stream);
        value3 = this.ReadInt16(stream);
        this.ReadInt16(stream); // only CMYK supports this field. As we can't handle CMYK colors, we read the value to advance the stream but don't do anything with it

        if (version == AdobePhotoshopColorSwatchFileVersion.Version2)
        {
          int length;

          // need to read the name even though currently our colour collection doesn't support names
          length = ReadInt32(stream);
          name = this.ReadString(stream, length);
        }
        else
        {
          name = string.Empty;
        }

        switch (colorSpace)
        {
          case AdobePhotoshopColorSwatchColorSpace.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;
            green = value2 / 256;
            blue = value3 / 256;

            results.Add(Color.FromArgb(red, green, blue));
            break;

          case AdobePhotoshopColorSwatchColorSpace.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;
            saturation = value2 / 655.35;
            brightness = value3 / 655.35;

            results.Add(new HslColor(hue, saturation, brightness).ToRgbColor());
            break;

          case AdobePhotoshopColorSwatchColorSpace.Grayscale:

            int gray;

            // Grayscale.
            // The first value in the color data is the gray value, from 0...10000.
            gray = (int)(value1 / 39.0625);

            results.Add(Color.FromArgb(gray, gray, gray));
            break;

          default:
            throw new InvalidDataException(string.Format("Color space '{0}' not supported.", colorSpace));
        }

#if USENAMEHACK
        results.SetName(i, name);
#endif
      }

      return results;
    }
Exemplo n.º 13
0
        /// <summary>
        /// Creates the DB32 palette.
        /// </summary>
        /// <remarks>http://www.pixeljoint.com/forum/forum_posts.asp?TID=16247</remarks>
        /// <param name="pad">if set to <c>true</c> the palette is padded with black to fill 256 entries.</param>
        protected ColorCollection CreateDawnBringer32Palette(bool pad)
        {
            ColorCollection results;

            results = new ColorCollection();

            results.Add(Color.FromArgb(0, 0, 0));
            results.Add(Color.FromArgb(34, 32, 52));
            results.Add(Color.FromArgb(69, 40, 60));
            results.Add(Color.FromArgb(102, 57, 49));
            results.Add(Color.FromArgb(143, 86, 59));
            results.Add(Color.FromArgb(223, 113, 38));
            results.Add(Color.FromArgb(217, 160, 102));
            results.Add(Color.FromArgb(238, 195, 154));
            results.Add(Color.FromArgb(251, 242, 54));
            results.Add(Color.FromArgb(153, 229, 80));
            results.Add(Color.FromArgb(106, 190, 48));
            results.Add(Color.FromArgb(55, 148, 110));
            results.Add(Color.FromArgb(75, 105, 47));
            results.Add(Color.FromArgb(82, 75, 36));
            results.Add(Color.FromArgb(50, 60, 57));
            results.Add(Color.FromArgb(63, 63, 116));
            results.Add(Color.FromArgb(48, 96, 130));
            results.Add(Color.FromArgb(91, 110, 225));
            results.Add(Color.FromArgb(99, 155, 255));
            results.Add(Color.FromArgb(95, 205, 228));
            results.Add(Color.FromArgb(203, 219, 252));
            results.Add(Color.FromArgb(255, 255, 255));
            results.Add(Color.FromArgb(155, 173, 183));
            results.Add(Color.FromArgb(132, 126, 135));
            results.Add(Color.FromArgb(105, 106, 106));
            results.Add(Color.FromArgb(89, 86, 82));
            results.Add(Color.FromArgb(118, 66, 138));
            results.Add(Color.FromArgb(172, 50, 50));
            results.Add(Color.FromArgb(217, 87, 99));
            results.Add(Color.FromArgb(215, 123, 186));
            results.Add(Color.FromArgb(143, 151, 74));
            results.Add(Color.FromArgb(138, 111, 48));

            if (pad)
            {
                while (results.Count < 256)
                {
                    results.Add(Color.FromArgb(0, 0, 0));
                }
            }

            return(results);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Creates the DB16 palette.
        /// </summary>
        /// <remarks>http://www.pixeljoint.com/forum/forum_posts.asp?TID=12795</remarks>
        /// <param name="pad">if set to <c>true</c> the palette is padded with black to fill 256 entries.</param>
        protected ColorCollection CreateDawnBringer16Palette(bool pad)
        {
            ColorCollection results;

            results = new ColorCollection();

            results.Add(Color.FromArgb(20, 12, 28));
            results.Add(Color.FromArgb(68, 36, 52));
            results.Add(Color.FromArgb(48, 52, 109));
            results.Add(Color.FromArgb(78, 74, 78));
            results.Add(Color.FromArgb(133, 76, 48));
            results.Add(Color.FromArgb(52, 101, 36));
            results.Add(Color.FromArgb(208, 70, 72));
            results.Add(Color.FromArgb(117, 113, 97));
            results.Add(Color.FromArgb(89, 125, 206));
            results.Add(Color.FromArgb(210, 125, 44));
            results.Add(Color.FromArgb(133, 149, 161));
            results.Add(Color.FromArgb(109, 170, 44));
            results.Add(Color.FromArgb(210, 170, 153));
            results.Add(Color.FromArgb(109, 194, 202));
            results.Add(Color.FromArgb(218, 212, 94));
            results.Add(Color.FromArgb(222, 238, 214));

            if (pad)
            {
                while (results.Count < 256)
                {
                    results.Add(Color.FromArgb(0, 0, 0));
                }
            }

            return(results);
        }
    /// <summary>
    /// Deserializes the <see cref="ColorCollection" /> 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="ColorCollection" /> being deserialized.</returns>
    public override ColorCollection Deserialize(Stream stream)
    {
      ColorCollection results;

      if (stream == null)
        throw new ArgumentNullException("stream");

      results = new ColorCollection();

      byte[] buffer;
      string header;

      // read the FORM header that identifies the document as an IFF file
      buffer = new byte[4];
      stream.Read(buffer, 0, buffer.Length);
      if (Encoding.ASCII.GetString(buffer) != "FORM")
        throw new InvalidDataException("Form header not found.");

      // the next value is the size of all the data in the FORM chunk
      // We don't actually need this value, but we have to read it
      // regardless to advance the stream
      this.ReadInt(stream);

      // read either the PBM or ILBM header that identifies this document as an image file
      stream.Read(buffer, 0, buffer.Length);
      header = Encoding.ASCII.GetString(buffer);
      if (header != "PBM " && header != "ILBM")
        throw new InvalidDataException("Bitmap header not found.");

      while (stream.Read(buffer, 0, buffer.Length) == buffer.Length)
      {
        int chunkLength;

        chunkLength = this.ReadInt(stream);

        if (Encoding.ASCII.GetString(buffer) != "CMAP")
        {
          // some other LBM chunk, skip it
          if (stream.CanSeek)
            stream.Seek(chunkLength, SeekOrigin.Current);
          else
          {
            for (int i = 0; i < chunkLength; i++)
              stream.ReadByte();
          }
        }
        else
        {
          // color map chunk!
          for (int i = 0; i < chunkLength / 3; i++)
          {
            int r;
            int g;
            int b;

            r = stream.ReadByte();
            g = stream.ReadByte();
            b = stream.ReadByte();

            results.Add(Color.FromArgb(r, g, b));
          }

          // all done so stop reading the rest of the file
          break;
        }

        // chunks always contain an even number of bytes even if the recorded length is odd
        // if the length is odd, then there's a padding byte in the file - just read and discard
        if (chunkLength % 2 != 0)
          stream.ReadByte();
      }

      return results;
    }
    public void FindNegativeTest()
    {
      // arrange
      ColorCollection target;
      int expected;
      int actual;

      target = new ColorCollection();
      target.Add(Color.Black);
      target.Add(Color.CornflowerBlue);
      target.Add(Color.Violet);
      expected = -1;

      // act
      actual = target.Find(Color.Yellow);

      // assert
      actual.Should().Be(expected);
    }
    /// <summary>
    /// Creates the DB16 palette.
    /// </summary>
    /// <remarks>http://www.pixeljoint.com/forum/forum_posts.asp?TID=12795</remarks>
    /// <param name="pad">if set to <c>true</c> the palette is padded with black to fill 256 entries.</param>
    protected ColorCollection CreateDawnBringer16Palette(bool pad)
    {
      ColorCollection results;

      results = new ColorCollection();

      results.Add(Color.FromArgb(20, 12, 28));
      results.Add(Color.FromArgb(68, 36, 52));
      results.Add(Color.FromArgb(48, 52, 109));
      results.Add(Color.FromArgb(78, 74, 78));
      results.Add(Color.FromArgb(133, 76, 48));
      results.Add(Color.FromArgb(52, 101, 36));
      results.Add(Color.FromArgb(208, 70, 72));
      results.Add(Color.FromArgb(117, 113, 97));
      results.Add(Color.FromArgb(89, 125, 206));
      results.Add(Color.FromArgb(210, 125, 44));
      results.Add(Color.FromArgb(133, 149, 161));
      results.Add(Color.FromArgb(109, 170, 44));
      results.Add(Color.FromArgb(210, 170, 153));
      results.Add(Color.FromArgb(109, 194, 202));
      results.Add(Color.FromArgb(218, 212, 94));
      results.Add(Color.FromArgb(222, 238, 214));

      if (pad)
      {
        while (results.Count < 256)
        {
          results.Add(Color.FromArgb(0, 0, 0));
        }
      }

      return results;
    }
    public void FindTest()
    {
      // arrange
      ColorCollection target;
      int expected;
      int actual;

      target = new ColorCollection();
      target.Add(Color.Black);
      target.Add(Color.CornflowerBlue);
      target.Add(Color.Violet);
      expected = 1;

      // act
      actual = target.Find(Color.FromArgb(100, 149, 237));

      // assert
      actual.Should().Be(expected);
    }
        /// <summary>
        /// Deserializes the <see cref="ColorCollection" /> 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="ColorCollection" /> being deserialized.</returns>
        public override ColorCollection Deserialize(Stream stream)
        {
            ColorCollection results;

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

              results = new ColorCollection();

              for (int i = 0; i < stream.Length / 3; i++)
              {
            int r;
            int g;
            int b;

            r = stream.ReadByte();
            g = stream.ReadByte();
            b = stream.ReadByte();

            results.Add(Color.FromArgb(r, g, b));
              }

              return results;
        }
Exemplo n.º 20
0
        public virtual int AddCustomColor(Color value)
        {
            int newIndex;

            newIndex = GetColorIndex(value);

            if (newIndex == InvalidIndex)
            {
                if (AutoAddColors)
                    CustomColors.Add(value);
                else
                {
                    if (CustomColors == null)
                    {
                        CustomColors = new ColorCollection();
                        CustomColors.Add(value);
                    }
                    else
                        CustomColors[0] = value;

                    newIndex = GetColorIndex(value);
                }

                RefreshColors();
            }

            return newIndex;
        }
    public void FindIgnoreAlphaNegativeTest()
    {
      // arrange
      ColorCollection target;
      int expected;
      int actual;

      target = new ColorCollection();
      target.Add(Color.Black);
      target.Add(Color.CornflowerBlue);
      target.Add(Color.Violet);
      expected = -1;

      // act
      actual = target.Find(Color.FromArgb(128, Color.CornflowerBlue), false);

      // assert
      actual.Should().Be(expected);
    }
    /// <summary>
    /// Deserializes the <see cref="ColorCollection" /> 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="ColorCollection" /> being deserialized.</returns>
    public override ColorCollection Deserialize(Stream stream)
    {
      ColorCollection results;

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

      results = new ColorCollection();

      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;
    }
    /// <summary>
    /// Deserializes the <see cref="ColorCollection" /> 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="ColorCollection" /> being deserialized.</returns>
    public override ColorCollection Deserialize(Stream stream)
    {
      ColorCollection results;
      int count;

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

      results = new ColorCollection();

      count = (int)(stream.Length / 3);

      for (int i = 0; i < count; i++)
      {
        int r;
        int g;
        int b;

        r = stream.ReadByte();
        g = stream.ReadByte();
        b = stream.ReadByte();

        results.Add(Color.FromArgb(r, g, b));
      }

      if (count == 257)
      {
        int realCount;

        // undocumented on the spec page, but when poking around
        // the Optimized Colors preset files of CS2, I found that
        // four extra bytes were added to some files. The second of
        // these extra bytes specified how many colours are
        // really in use (anything else is just padding)

        realCount = results[256].G;

        while (results.Count > realCount)
        {
          results.RemoveAt(realCount);
        }
      }

      return results;
    }