Exemplo n.º 1
0
        private static List <Color> ReadSwatches(Stream stream, FileVersion version)
        {
            int          colorCount;
            List <Color> results;

            results = new List <Color>();

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

            for (int i = 0; i < colorCount; i++)
            {
                ColorSpace colorSpace;
                int        value1;
                int        value2;
                int        value3;
                int        value4;

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

                value1 = ReadInt16(stream);
                value2 = ReadInt16(stream);
                value3 = ReadInt16(stream);
                value4 = ReadInt16(stream);

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

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

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

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

                case ColorSpace.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;       // 0-359
                    saturation = value2 / 655.35 / 100; // 0-1.0
                    brightness = value3 / 655.35 / 100; // 0-1.0

                    results.Add(ColorsHelper.CreateFromHSV(hue, saturation, brightness));
                    break;

                case ColorSpace.Grayscale:

                    int gray;

                    // Grayscale.
                    // The first value in the color data is the gray value, from 0...10000.

                    gray = (int)(value1 / 39.0625);     // 0-255

                    results.Add(Color.FromRgb((byte)gray, (byte)gray, (byte)gray));
                    break;

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

            return(results);
        }