当前位置: 首页>>代码示例>>C#>>正文


C# ColorCollection.SetName方法代码示例

本文整理汇总了C#中ColorCollection.SetName方法的典型用法代码示例。如果您正苦于以下问题:C# ColorCollection.SetName方法的具体用法?C# ColorCollection.SetName怎么用?C# ColorCollection.SetName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ColorCollection的用法示例。


在下文中一共展示了ColorCollection.SetName方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReadPalette

    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;
    }
开发者ID:FrankGITDeveloper,项目名称:Landsknecht_GreenScreen,代码行数:95,代码来源:AdobePhotoShopColorSwatchSerializer.cs

示例2: Deserialize

    /// <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;
    }
开发者ID:leebivip,项目名称:ColorHighlighter,代码行数:86,代码来源:GimpPaletteSerializer.cs


注:本文中的ColorCollection.SetName方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。