Ejemplo n.º 1
0
        public void Serialize(Stream stream, ColorCollection palette, AdobePhotoshopColorSwatchFileVersion version, AdobePhotoshopColorSwatchColorSpace colorSpace)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

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

            if (version == AdobePhotoshopColorSwatchFileVersion.Version2)
            {
                this.WritePalette(stream, palette, AdobePhotoshopColorSwatchFileVersion.Version1, colorSpace);
            }
            this.WritePalette(stream, palette, version, colorSpace);
        }
        public void Serialize(Stream stream, IEnumerable <Color> palette, AdobePhotoshopColorSwatchFileVersion version,
                              AdobePhotoshopColorSwatchColorSpace colorSpace)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

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

            if (version == AdobePhotoshopColorSwatchFileVersion.Version2)
            {
                WritePaletteNew(stream, palette, AdobePhotoshopColorSwatchFileVersion.Version1, colorSpace);
            }

            WritePaletteNew(stream, palette, version, colorSpace);
        }
    protected virtual void WritePalette(Stream stream, ColorCollection palette, AdobePhotoshopColorSwatchFileVersion version, AdobePhotoshopColorSwatchColorSpace colorSpace)
    {
      int swatchIndex;

      this.WriteInt16(stream, (short)version);
      this.WriteInt16(stream, (short)palette.Count);

      swatchIndex = 0;

      foreach (Color color in palette)
      {
        short value1;
        short value2;
        short value3;
        short value4;

        swatchIndex++;

        switch (colorSpace)
        {
          case AdobePhotoshopColorSwatchColorSpace.Rgb:
            value1 = (short)(color.R * 256);
            value2 = (short)(color.G * 256);
            value3 = (short)(color.B * 256);
            value4 = 0;
            break;
          case AdobePhotoshopColorSwatchColorSpace.Hsb:
            value1 = (short)(color.GetHue() * 182.04);
            value2 = (short)(color.GetSaturation() * 655.35);
            value3 = (short)(color.GetBrightness() * 655.35);
            value4 = 0;
            break;
          case AdobePhotoshopColorSwatchColorSpace.Grayscale:
            if (color.R == color.G && color.R == color.B)
            {
              // already grayscale
              value1 = (short)(color.R * 39.0625);
            }
            else
            {
              // color is not grayscale, convert
              value1 = (short)(((color.R + color.G + color.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 == AdobePhotoshopColorSwatchFileVersion.Version2)
        {
          string name;

#if USENAMEHACK
          name = palette.GetName(swatchIndex - 1);
          if (string.IsNullOrEmpty(name))
          {
            name = string.Format("Swatch {0}", swatchIndex);
          }
#else
          name = color.IsNamedColor ? color.Name : string.Format("Swatch {0}", swatchIndex);
#endif

          this.WriteInt32(stream, name.Length);
          this.WriteString(stream, name);
        }
      }
    }
    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;
    }
    public void Serialize(Stream stream, ColorCollection palette, AdobePhotoshopColorSwatchFileVersion version, AdobePhotoshopColorSwatchColorSpace colorSpace)
    {
      if (stream == null)
      {
        throw new ArgumentNullException(nameof(stream));
      }

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

      if (version == AdobePhotoshopColorSwatchFileVersion.Version2)
      {
        this.WritePalette(stream, palette, AdobePhotoshopColorSwatchFileVersion.Version1, colorSpace);
      }
      this.WritePalette(stream, palette, version, colorSpace);
    }
 public void Serialize(Stream stream, ColorCollection palette, AdobePhotoshopColorSwatchFileVersion version)
 {
   this.Serialize(stream, palette, version, AdobePhotoshopColorSwatchColorSpace.Rgb);
 }
Ejemplo n.º 7
0
        protected virtual void WritePalette(Stream stream, ColorCollection palette, AdobePhotoshopColorSwatchFileVersion version, AdobePhotoshopColorSwatchColorSpace colorSpace)
        {
            int swatchIndex;

            this.WriteInt16(stream, (short)version);
            this.WriteInt16(stream, (short)palette.Count);

            swatchIndex = 0;

            foreach (Color color in palette)
            {
                short value1;
                short value2;
                short value3;
                short value4;

                swatchIndex++;

                switch (colorSpace)
                {
                case AdobePhotoshopColorSwatchColorSpace.Rgb:
                    value1 = (short)(color.R * 256);
                    value2 = (short)(color.G * 256);
                    value3 = (short)(color.B * 256);
                    value4 = 0;
                    break;

                case AdobePhotoshopColorSwatchColorSpace.Hsb:
                    value1 = (short)(color.GetHue() * 182.04);
                    value2 = (short)(color.GetSaturation() * 655.35);
                    value3 = (short)(color.GetBrightness() * 655.35);
                    value4 = 0;
                    break;

                case AdobePhotoshopColorSwatchColorSpace.Grayscale:
                    if (color.R == color.G && color.R == color.B)
                    {
                        // already grayscale
                        value1 = (short)(color.R * 39.0625);
                    }
                    else
                    {
                        // color is not grayscale, convert
                        value1 = (short)(((color.R + color.G + color.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 == AdobePhotoshopColorSwatchFileVersion.Version2)
                {
                    string name;

#if USENAMEHACK
                    name = palette.GetName(swatchIndex - 1);
                    if (string.IsNullOrEmpty(name))
                    {
                        name = string.Format("Swatch {0}", swatchIndex);
                    }
#else
                    name = color.IsNamedColor ? color.Name : string.Format("Swatch {0}", swatchIndex);
#endif

                    this.WriteInt32(stream, name.Length);
                    this.WriteString(stream, name);
                }
            }
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
 public void Serialize(Stream stream, ColorCollection palette, AdobePhotoshopColorSwatchFileVersion version)
 {
     this.Serialize(stream, palette, version, AdobePhotoshopColorSwatchColorSpace.Rgb);
 }