locked
Color each element in a loop RRS feed

  • Question

  • I need to create 3 different sets of brushes and grab a set based on a variable that is being passed. So if the variable is 'blue' I need to be able to grab the blue brushes and apply a different brush to each element in my loop. For example

    Dim i As Integer

    i += 1

    For Each element in layer.Elements

           element.fill = SolidColorBrush(blue(i))

           i = i + 1

    Next

    I tried stuffing all of names of the blue brushes into a variable as strings but that didn't work. Does anyone have any suggestions?

    Wednesday, February 17, 2010 3:15 PM

Answers

  • In C# Syntactically what you have typed in vb.net will be

     var color[] = new color[]{new color(),new color(),new color()} ;

    Suggesting to your first question on the top, you can always fix one of the 3 color randomness like 'blue' also you can restrict the random int range from 255 to a smaller range.

    Friday, February 19, 2010 10:08 PM

All replies

  •  I used a Random Generator.. Maybe this might help.

     

     internal static Color GetRandomColor()
            {
                byte a = 255; // or whatever...
                byte r = (byte)(new Random().Next(0,255));
                byte g = (byte)(new Random().Next(0, 255));
                byte b = (byte)(new Random().Next(0, 255));
                return Color.FromArgb(a, r, g, b);
            }

     

    Call this method within the constructor of SolidColorBrush like this

     

    element.fill = new SolidColorBrush(GetRandomColor());

    Wednesday, February 17, 2010 5:17 PM
  •  Thanks koolraaga. The concept is right, but I need to be able to pull the same color more than once so random won't work.  I did something similar in the past in Flash:

     var BlueArray:Array = [0xffffff,0xf80404,0x77060A,0x94050B,0xA60108,0xCF1D0F,0x520005,0xB01D14,0xC01D14,0xf80404];
     var colTrans:Array = [colTrans0,colTrans1,colTrans2,colTrans3,colTrans4,colTrans5,colTrans6,colTrans7,colTrans8,colTrans9];
        if (var == "Blue") {
            colTrans.push(new ColorTransform());
            ColorTransform(colTrans[f]).color = BlueArray[f];

        }

    Now I just need to figure out how to do it in vb.

    Wednesday, February 17, 2010 6:42 PM
  • Hi,

      Sorry , but i still feel confused about what you are doing.

      To create sets of color:

                Color[] colors = new Color[10]{
                    new Color(){ A=0,B=xx,G=xx,R=xx },
                    new Color(){ A=0,B=xx,G=xx,R=xx },
                    new Color(){ A=0,B=xx,G=xx,R=xx },
                    new Color(){ A=0,B=xx,G=xx,R=xx },
                    new Color(){ A=0,B=xx,G=xx,R=xx },
                    new Color(){ A=0,B=xx,G=xx,R=xx },
                    new Color(){ A=0,B=xx,G=xx,R=xx },
                    new Color(){ A=0,B=xx,G=xx,R=xx },
                    new Color(){ A=0,B=xx,G=xx,R=xx },
                    new Color(){ A=0,B=xx,G=xx,R=xx },
                };

    Best Regards

    Friday, February 19, 2010 2:02 AM
  •  Sorry for the confusion. I know how to create colors, I was just unsure how to put them into an array and call them later on. I am new to Silverlight and vb so I'm having some trouble with the syntax. There is a C# to vb.net converter that I have been using that normally works really well but it had a tough time with this one.

    Dim colors As Color() = New Color(9) {New Color(), New Color(), New Color(), New Color(), New Color(), New Color(),_

    New Color(), New Color(), New Color(), New Color()}

    Can anyone decipher this a little bette?

    Thanks.

     

    Friday, February 19, 2010 12:04 PM
  • In C# Syntactically what you have typed in vb.net will be

     var color[] = new color[]{new color(),new color(),new color()} ;

    Suggesting to your first question on the top, you can always fix one of the 3 color randomness like 'blue' also you can restrict the random int range from 255 to a smaller range.

    Friday, February 19, 2010 10:08 PM
  •  Hi,

    Just a thought, try using string array of color codes, and only convert them when u need them just before assigning.

    Consider this http://forums.silverlight.net/forums/p/20392/233837.aspx

    Monday, February 22, 2010 6:34 AM
  •  Thanks koolraaga. This worked.

    Monday, February 22, 2010 11:20 AM