Example #1
0
        /// <summary>
        /// Returns an empty palette (with every color being black, 0x0000)
        /// </summary>
        public static Palette Empty(int max)
        {
            Palette result = new Palette(max);

            for (int i = 0; i < max; i++)
            {
                result.Add(new Color(0x0000));
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// Returns a Palette with the alpha bit of all its colors set to 0
        /// </summary>
        public static Palette Opacify(Palette palette)
        {
            Palette result = new Palette(palette.Maximum);

            for (int i = 0; i < palette.Count; i++)
            {
                result.Add(palette[i].SetAlpha(false));
            }
            return(result);
        }
Example #3
0
        /// <summary>
        /// Merges several (up to 16) 16-color palettes together
        /// </summary>
        public static Palette Merge(Palette[] palettes)
        {
            if (palettes.Length > 16)
            {
                throw new Exception("Cannot merge more than 16 palettes together.");
            }

            Palette result = new Palette(palettes.Length * GBA.Palette.MAX);

            for (int p = 0; p < palettes.Length; p++)
            {
                if (palettes[p].Maximum != 16)
                {
                    throw new Exception("Palette number " + p + " has invalid color amount");
                }

                result.Add(palettes[p]);
            }
            return(result);
        }
Example #4
0
        /// <summary>
        /// Creates a Palette from a palette file (if its an image file, must be 16 pixels in width)
        /// </summary>
        public Palette(string filepath, int maximum = MAX)
        {
            Colors = new List <Color>(maximum);

            if (filepath.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
                filepath.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) ||
                filepath.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase))
            {
                Bitmap source;
                try
                {
                    source = new GBA.Bitmap(filepath);
                }
                catch { throw new Exception("Could not open palette file:\n" + filepath); }

                bool  addEveryPixel = (source.Width == 16);
                int   index         = 0;
                Color color;
                for (int y = 0; y < source.Height; y++)
                {
                    for (int x = 0; x < source.Width; x++)
                    {
                        color = source.GetColor(x, y);
                        if (!addEveryPixel && this.Contains(color))
                        {
                            continue;
                        }
                        else
                        {
                            if (this.IsFull)
                            {
                                throw new Exception("This palette cannot hold more than " + maximum + " colors.");
                            }
                            else
                            {
                                this.Colors.Add(color);
                                index++;
                            }
                        }
                    }
                }
            }
            else if (filepath.EndsWith(".pal", StringComparison.OrdinalIgnoreCase))
            {
                byte[] palette = File.ReadAllBytes(filepath);

                if (palette[0] == 0x43 &&
                    palette[1] == 0x4C &&
                    palette[2] == 0x52 &&
                    palette[3] == 0x58) // 'CLRX' is the header for usenti .pal files
                {
                    string[] file = File.ReadAllLines(filepath);
                    int      channelBits;
                    int      colorAmount;

                    int parse  = 5;
                    int length = 0;

                    while (parse + length < file[0].Length && file[0][parse + length] != ' ')
                    {
                        length++;
                    }
                    channelBits = int.Parse(file[0].Substring(parse, length));

                    parse += length + 1;
                    length = 0;

                    while (parse + length < file[0].Length && file[0][parse + length] != ' ')
                    {
                        length++;
                    }
                    colorAmount = int.Parse(file[0].Substring(parse, length));

                    uint    color  = 0x00000000;
                    Palette colors = new Palette(colorAmount);
                    for (int i = 0; i < (colorAmount / 4); i++)
                    {
                        parse  = 0;
                        length = 0;

                        for (int j = 0; j < 4; j++)
                        {
                            while (parse + length < file[1 + i].Length &&
                                   file[1 + i][parse + length] != ' ')
                            {
                                length++;
                            }

                            color = Util.HexToInt(file[1 + i].Substring(parse, length));
                            colors.Add(new Color(color));

                            parse += length + 1;
                            length = 0;
                        }
                    }
                    palette = colors.ToBytes(false);
                }
                else if (palette.Length != maximum * 2)
                {
                    throw new Exception(
                              "Cannot load palette, the file given is of invalid length.\n" +
                              "It should be " + maximum * 2 + " bytes long.");
                }


                byte[] buffer = new byte[2];
                for (int i = 0; i < palette.Length / 2; i++)
                {
                    buffer[0] = palette[i * 2 + 1];
                    buffer[1] = palette[i * 2];
                    Colors.Add(new Color(buffer));
                }
            }
            else
            {
                throw new Exception("Invalid filetype given to load palette.");
            }
        }