Example #1
0
 public static void ApplyTransparency(Tileset tileset)
 {
     Color[] pixel = new Color[tileset.Width * tileset.Height];
     tileset.Texture.GetData<Color>(pixel);
     for (int i = 0; i < pixel.Count(); i++)
     {
         if (pixel[i].Equals(tileset.Transparency))
             pixel[i] = new Color();
     }
     tileset.Texture.SetData<Color>(pixel);
 }
        public static Texture2D CreateTexture(GraphicsDevice device, int width, int height)
        {
            //initialize a texture
            Random    rndRandom = new Random();
            Texture2D texture   = new Texture2D(device, width, height);

            //the array holds the color for each pixel in the texture
            Color[] data        = new Color[width * height];
            Color   empty       = new Color();
            int     colorRange  = 5;
            int     startMin    = 0;
            int     startMax    = 255;
            double  minAddition = 2.56;
            double  maxAddition = 1;

            for (int pixel = 0; pixel < data.Count(); pixel++)
            {
                //the function applies the color according to the specified pixel

                if (pixel % width > 0 && data[pixel - 1] != empty)
                {
                    if (pixel - width > 0 && data[pixel - width] != empty)                                        // inside
                    {
                        if (pixel - 2 * width > 0 && data[pixel - 2 * width] != empty && (pixel - 2) % width > 0) // further rows inside
                        {
                            Color cl = new Color(
                                rndRandom.Next((int)Math.Round((data[pixel - 1].R + data[pixel - width].R + data[pixel - 2].R + data[pixel - 2 * width].R + minAddition) / 4.0f - colorRange), (int)Math.Round((data[pixel - 1].R + data[pixel - width].R + data[pixel - 2].R + data[pixel - 2 * width].R + maxAddition) / 4.0f + colorRange)),
                                rndRandom.Next((int)Math.Round((data[pixel - 1].G + data[pixel - width].G + data[pixel - 2].G + data[pixel - 2 * width].G + minAddition) / 4.0f - colorRange), (int)Math.Round((data[pixel - 1].G + data[pixel - width].G + data[pixel - 2].G + data[pixel - 2 * width].G + maxAddition) / 4.0f + colorRange)),
                                rndRandom.Next((int)Math.Round((data[pixel - 1].B + data[pixel - width].B + data[pixel - 2].B + data[pixel - 2 * width].B + minAddition) / 4.0f - colorRange), (int)Math.Round((data[pixel - 1].B + data[pixel - width].B + data[pixel - 2].B + data[pixel - 2 * width].B + maxAddition) / 4.0f + colorRange)))
                            ;
                            data[pixel] = cl;
                        }
                        else // first row inside
                        {
                            Color cl = new Color(
                                rndRandom.Next((int)Math.Floor((data[pixel - 1].R + data[pixel - width].R) / 2.0f - colorRange), (int)Math.Ceiling((data[pixel - 1].R + data[pixel - width].R) / 2.0f + colorRange)),
                                rndRandom.Next((int)Math.Floor((data[pixel - 1].G + data[pixel - width].G) / 2.0f - colorRange), (int)Math.Ceiling((data[pixel - 1].G + data[pixel - width].G) / 2.0f + colorRange)),
                                rndRandom.Next((int)Math.Floor((data[pixel - 1].B + data[pixel - width].B) / 2.0f - colorRange), (int)Math.Ceiling((data[pixel - 1].B + data[pixel - width].B) / 2.0f + colorRange)))
                            ;
                            data[pixel] = cl;
                        }
                    }
                    else // top edge
                    {
                        Color cl = new Color(
                            rndRandom.Next(data[pixel - 1].R - colorRange, data[pixel - 1].R + colorRange),
                            rndRandom.Next(data[pixel - 1].G - colorRange, data[pixel - 1].G + colorRange),
                            rndRandom.Next(data[pixel - 1].B - colorRange, data[pixel - 1].B + colorRange)
                            );
                        data[pixel] = cl;
                    }
                }
                else if (pixel - width >= 0 && data[pixel - width] != empty) // left edge
                {
                    Color cl = new Color(
                        rndRandom.Next((data[pixel - width].R) - colorRange, (data[pixel - width].R) + colorRange),
                        rndRandom.Next((data[pixel - width].G) - colorRange, (data[pixel - width].G) + colorRange),
                        rndRandom.Next((data[pixel - width].B) - colorRange, (data[pixel - width].B) + colorRange));

                    data[pixel] = cl;
                }
                else //  first pixel
                {
                    Color cl = new Color(rndRandom.Next(startMin, startMax), rndRandom.Next(startMin, startMax), rndRandom.Next(startMin, startMax));
                    data[pixel] = cl;
                }



                // data[pixel] = paint(pixel);
            }

            //set the color
            texture.SetData(data);
            return(texture);
        }