Example #1
0
        public bool LoadXML_palette256(XmlNode xnode)
        {
            short nColors = 0;

            foreach (XmlNode xn in xnode.ChildNodes)
            {
                if (xn.Name == "color")
                {
                    string strRGB = XMLUtils.GetXMLAttribute(xn, "rgb");
                    int    nRGB;
                    if (!Palette.ParseRGBColorValue(strRGB, out nRGB))
                    {
                        m_doc.ErrorString("Unable to parse color value '{0}' in palette '{1}'.", strRGB, Name);
                        return(false);
                    }

                    Color c = Color555.CalcColor(nRGB);
                    if (nColors < 256)
                    {
                        m_mapId2Color.Add(nColors, c);
                    }
                    nColors++;
                }
            }

            if (nColors != 256)
            {
                m_doc.ErrorString("Incorrect number of colors in background image palette");
                return(false);
            }
            return(true);
        }
Example #2
0
        /// <summary>
        /// Export the image as an array of direct 16-bit color values.
        /// </summary>
        /// <param name="tw"></param>
        public void Export_BgImageData_Direct(System.IO.TextWriter tw)
        {
            StringBuilder sb       = null;
            int           nPerLine = 32;

            for (int iy = 0; iy < m_height; iy++)
            {
                tw.WriteLine("\t// Row {0}", iy);
                for (int ix = 0; ix < m_width; ix++)
                {
                    if ((ix % nPerLine) == 0)
                    {
                        if (sb != null)
                        {
                            tw.WriteLine(sb.ToString());
                        }
                        sb = new StringBuilder("\t");
                    }
                    short palette_index = m_ImageData[ix, iy];
                    Color c             = m_mapId2Color[palette_index];
                    int   encoded       = Color555.Encode(c);
                    sb.Append(String.Format("0x{0:x4},", encoded));
                }
                if (sb != null)
                {
                    tw.WriteLine(sb.ToString());
                }
                sb = null;
            }
        }
Example #3
0
        /// <summary>
        /// Import a single color into the palette.
        /// </summary>
        /// <param name="nIndex"></param>
        /// <param name="color"></param>
        public void ImportColor(int nIndex, int color)
        {
            int cRed, cGreen, cBlue;

            Color555.ExtractColors(color, out cRed, out cGreen, out cBlue);
            UpdateColor(nIndex, cRed, cGreen, cBlue);
        }
Example #4
0
        public void UpdateColor(int nIndex, int cRed, int cGreen, int cBlue)
        {
            m_data.cRed[nIndex]   = cRed;
            m_data.cGreen[nIndex] = cGreen;
            m_data.cBlue[nIndex]  = cBlue;

            if (m_Brush[nIndex] != null)
            {
                m_Brush[nIndex].Dispose();
            }
            m_Brush[nIndex] = new SolidBrush(Color555.CalcColor(cRed, cGreen, cBlue));
        }
Example #5
0
        public static bool ParseRGBColorValue(string strRGB, out int nRGB)
        {
            Regex rxRGB = new Regex(@"([0-1][0-9A-Fa-f])([0-1][0-9A-Fa-f])([0-1][0-9A-Fa-f])");
            Match mxRGB = rxRGB.Match(strRGB);

            nRGB = 0;
            if (!mxRGB.Success)
            {
                return(false);
            }

            GroupCollection matchGroups = mxRGB.Groups;
            int             r           = Convert.ToInt32(matchGroups[1].Value, 16);
            int             g           = Convert.ToInt32(matchGroups[2].Value, 16);
            int             b           = Convert.ToInt32(matchGroups[3].Value, 16);

            nRGB = Color555.Encode(r, g, b);
            return(true);
        }
Example #6
0
        /// <summary>
        /// Export the 256-color palette for this image.
        /// </summary>
        /// <param name="tw"></param>
        public void Export_BgImagePaletteData(System.IO.TextWriter tw)
        {
            StringBuilder sb       = null;
            int           nPerLine = 8;

            for (short i = 0; i < 256; i++)
            {
                if ((i % nPerLine) == 0)
                {
                    if (sb != null)
                    {
                        tw.WriteLine(sb.ToString());
                    }
                    sb = new StringBuilder("\t");
                }
                sb.Append(String.Format("0x{0:x4},", Color555.Encode(m_mapId2Color[i])));
            }
            if (sb != null)
            {
                tw.WriteLine(sb.ToString());
            }
        }
        /// <summary>
        /// Does this UndoAction change the color of one of the palette entries?
        /// </summary>
        /// <param name="nColorIndex"></param>
        /// <returns></returns>
        public bool IsColorChange(out int nColorIndex, out int nColorValue1, out int nColorValue2)
        {
            nColorIndex  = -1;
            nColorValue1 = 0;
            nColorValue2 = 0;
            int nColors = m_before.numColors;

            for (int i = 0; i < nColors; i++)
            {
                // Note: this assumes that there is only 1 color change in an UndoAction
                if (m_before.cRed[i] != m_after.cRed[i] ||
                    m_before.cGreen[i] != m_after.cGreen[i] ||
                    m_before.cBlue[i] != m_after.cBlue[i]
                    )
                {
                    nColorIndex  = i;
                    nColorValue1 = Color555.Encode(m_before.cRed[i], m_before.cGreen[i], m_before.cBlue[i]);
                    nColorValue2 = Color555.Encode(m_after.cRed[i], m_after.cGreen[i], m_after.cBlue[i]);
                }
            }
            return(nColorIndex != -1);
        }
Example #8
0
        public void Save(System.IO.TextWriter tw)
        {
            tw.Write("\t\t<bgimage_pal8");
            tw.Write(String.Format(" name=\"{0}\"", Name));
            tw.Write(String.Format(" id=\"{0}\"", ExportId));
            tw.Write(String.Format(" desc=\"{0}\"", Description));
            tw.Write(" size=\"{0}x{1}\"", m_width, m_height);
            tw.WriteLine(">");

            tw.Write("\t\t\t<palette256");
            tw.Write(String.Format(" name=\"{0}\"", Name));
            tw.Write(String.Format(" id=\"{0}\"", ExportId));
            tw.WriteLine(">");

            int           nPerLine  = 8;
            string        strIndent = "\t\t\t\t";
            StringBuilder sb        = null;

            for (short i = 0; i < 256; i++)
            {
                if ((i % nPerLine) == 0)
                {
                    if (sb != null)
                    {
                        tw.WriteLine(sb.ToString());
                    }
                    sb = new StringBuilder(strIndent);
                }
                Color c = m_mapId2Color[i];
                int   r, g, b;
                Color555.ExtractColors(c, out r, out g, out b);
                sb.Append(String.Format("<color rgb=\"{0:x2}{1:x2}{2:x2}\"/>", r, g, b));
            }
            if (sb != null)
            {
                tw.WriteLine(sb.ToString());
            }

            tw.WriteLine("\t\t\t</palette256>");

            tw.Write("\t\t\t<bgimagedata");
            tw.Write(String.Format(" id=\"{0}\"", m_id));
            tw.WriteLine(">");

            for (int iy = 0; iy < m_height; iy++)
            {
                sb = new StringBuilder(strIndent);
                sb.Append("<bgimagerow>");
                for (int ix = 0; ix < m_width - 1; ix++)
                {
                    sb.Append(String.Format("{0:x2},", m_ImageData[ix, iy]));
                }
                sb.Append(String.Format("{0:x2}", m_ImageData[m_width - 1, iy]));
                sb.Append("</bgimagerow>");
                tw.WriteLine(sb.ToString());
            }

            tw.WriteLine("\t\t\t</bgimagedata>");

            tw.WriteLine("\t\t</bgimage_pal8>");
        }
Example #9
0
 /// <summary>
 /// Return the 16-bit encoding for the specified palette index.
 /// </summary>
 /// <param name="nIndex">The index of the palette entry</param>
 /// <returns>The encoding for the specified palette entry</returns>
 public int Encoding(int nIndex)
 {
     return(Color555.Encode(cRed[nIndex], cGreen[nIndex], cBlue[nIndex]));
 }