Ejemplo n.º 1
0
        public virtual Bitmap RenderImage(LayerSettings ls, Size flagSize)
        {
            int    blue = Color.Blue.ToArgb();
            Bitmap b    = (Bitmap)Image.Clone();

            FlagGenerator.recolorImage(b, blue, ls.Color);
            return(b);
        }
Ejemplo n.º 2
0
        public override Bitmap RenderImage(LayerSettings ls, Size flagSize)
        {
            //draw crest
            IRenderer g = IRendererFactory.GetPreferredRenderer(flagSize.Width, flagSize.Height);

            int    xPos      = 0;
            int    yPos      = 0;
            int    newHeight = flagSize.Height / 3;
            int    newWidth  = (int)((newHeight / (double)Image.Height) * Image.Width); //maintain aspect ratio
            double ratio;

            switch (ls.crestPosition)
            {
            case CrestPositions.Central:
                ratio     = ls.nominalSize / (double)Math.Max(Image.Width, Image.Height);
                newWidth  = (int)(Image.Width * ratio);
                newHeight = (int)(Image.Height * ratio);
                xPos      = (flagSize.Width / 2) - (newWidth / 2);
                yPos      = (flagSize.Height / 2) - (newHeight / 2);
                break;

            case CrestPositions.TopLeft:
                xPos = 0;
                yPos = 0;
                break;

            case CrestPositions.Left:
                newHeight = (int)(flagSize.Height * 0.75);
                newWidth  = (int)((newHeight / (double)Image.Height) * Image.Width);    //maintain aspect ratio
                xPos      = 0;
                yPos      = (flagSize.Height / 2) - (newHeight / 2);
                break;

            default:
                break;
            }

            int    blue = Color.Blue.ToArgb();
            Bitmap b    = (Bitmap)Image.Clone();

            FlagGenerator.recolorImage(b, blue, ls.Color);
            g.DrawImage(b, xPos, yPos, newWidth, newHeight);
            return(g.RenderTargetAsGDIBitmap());
        }
Ejemplo n.º 3
0
        public Flag Generate(int seed = 0x7777777)
        {
            try
            {
                Random r = new Random(seed);
                //setup
                int numLayers = layerDistrib.GetRandomItem(r);

                Flag flag = new Flag();
                flag.FieldCol = Color.FromArgb(flagCols.GetRandomItem(r));
                //generate layers
                for (int i = 0; i < numLayers; i++)
                {
                    int           layer = pickRandomLayer(flag.FlagLayers, r);
                    int           color = flagCols.GetRandomItem(r);
                    LayerSettings ls    = new LayerSettings(color);
                    flag.FlagLayers.Add(new Tuple <FlagLayer, LayerSettings>(Layers[layer], ls));
                }

                flag.FlagLayers = (from L in flag.FlagLayers
                                   orderby L.Item1.Priority ascending
                                   select L).ToList();

                //draw crest
                if (r.Next(100) < 50)
                {
                    CrestLayer cl = (CrestLayer)Layers.Where(L => L is CrestLayer).ToList().GetRandomItem(r);

                    if (cl == null)
                    {
                        return(null);
                    }
                    //is ther a graphic that accomidates a crest
                    var crestsHolders = (from L in flag.FlagLayers
                                         where (L.Item1.CanHoldCrest)
                                         select L).ToList();

                    if (crestsHolders.Count > 0)
                    {
                        //crest in middle
                        int crestSize   = crestsHolders.Min(L => L.Item1.MaxCres);
                        var crestHolder = flag.FlagLayers.FirstOrDefault(L => L.Item1.MaxCres == crestSize);
                        //move the crest holder to the end
                        flag.FlagLayers.Remove(crestHolder);
                        flag.FlagLayers.Add(crestHolder);

                        LayerSettings ls = new LayerSettings(crestCols.GetRandomItem(r));
                        ls.nominalSize   = crestSize;
                        ls.crestPosition = CrestPositions.Central;
                        if (r.Next(100) < 50)
                        {
                            //paint the back
                            ls.BackColor = crestBackCols.GetRandomItem();
                        }
                        flag.FlagLayers.Add(new Tuple <FlagLayer, LayerSettings>(cl, ls));
                    }
                    else
                    {
                        //crest in top left
                        LayerSettings ls = new LayerSettings(crestCols.GetRandomItem(r));
                        ls.nominalSize   = 0;
                        ls.crestPosition = (r.Next(100) < 50) ? CrestPositions.TopLeft : CrestPositions.Left;
                        flag.FlagLayers.Add(new Tuple <FlagLayer, LayerSettings>(cl, ls));
                    }
                }

                return(flag);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }