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);
        }
      }
    }
    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, AdobePhotoshopColorSwatchColorSpace colorSpace)
 {
   this.Serialize(stream, palette, AdobePhotoshopColorSwatchFileVersion.Version2, colorSpace);
 }
コード例 #5
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);
                }
            }
        }
コード例 #6
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);
        }
コード例 #7
0
 public void Serialize(Stream stream, ColorCollection palette, AdobePhotoshopColorSwatchColorSpace colorSpace)
 {
     this.Serialize(stream, palette, AdobePhotoshopColorSwatchFileVersion.Version2, colorSpace);
 }
 public void Serialize(Stream stream, IEnumerable <Color> palette, AdobePhotoshopColorSwatchColorSpace colorSpace)
 {
     Serialize(stream, palette, AdobePhotoshopColorSwatchFileVersion.Version2, colorSpace);
 }