Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Array Help [modified]

Array Help [modified]

Scheduled Pinned Locked Moved C#
javascriptdata-structurestoolshelpquestion
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P pancakeleh

    I have an array which contains of RGB values as shown below: <script src='http://img801.imageshack.us/shareable/?i=array.png&p=tl' type='text/javascript'></script><noscript>[URL=http://img801.imageshack.us/i/array.png/][IMG]http://img801.imageshack.us/img801/7560/array.png[/IMG][/URL]</noscript>[^]; is there any way to just extract R into one array, B into another array and G into another array? if (array[x].Contains("R")) { .... } by using the above is it achievable?

    modified on Monday, November 8, 2010 2:51 AM

    N Offline
    N Offline
    Nuri Ismail
    wrote on last edited by
    #2

    Here is the obvious way (just an example):

    // Initial array with ARGB values
    byte[] arrARGBValues = { 255, 255, 0, 0, 255, 255, 0, 0, 255, 150, 255, 255 };

    // Get length of the array and calculate the length of sub arrays
    int lengthARGBValues = arrARGBValues.Length;
    int lengthSubArrays = lengthARGBValues / 4;

    // Create the sub arrays for each value
    byte[] arrAValues = new byte[lengthSubArrays];
    byte[] arrRValues = new byte[lengthSubArrays];
    byte[] arrGValues = new byte[lengthSubArrays];
    byte[] arrBValues = new byte[lengthSubArrays];

    // Assign each element from initial array to the appropriate sub array
    for (int i = 0, j = 0; j < lengthSubArrays; i += 4, j++)
    {
    arrAValues[j] = arrARGBValues[i];
    arrRValues[j] = arrARGBValues[i + 1];
    arrGValues[j] = arrARGBValues[i + 2];
    arrBValues[j] = arrARGBValues[i + 3];
    }

    :)

    1 Reply Last reply
    0
    • P pancakeleh

      I have an array which contains of RGB values as shown below: <script src='http://img801.imageshack.us/shareable/?i=array.png&p=tl' type='text/javascript'></script><noscript>[URL=http://img801.imageshack.us/i/array.png/][IMG]http://img801.imageshack.us/img801/7560/array.png[/IMG][/URL]</noscript>[^]; is there any way to just extract R into one array, B into another array and G into another array? if (array[x].Contains("R")) { .... } by using the above is it achievable?

      modified on Monday, November 8, 2010 2:51 AM

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #3

      when you have an array (or any other collection) of colors, you can manipulate them directly. There is no need to first display them (i.e. their string representation) in a ListBox and then parse those strings, that approach is so wrong. Here is an example of what one can do, without ListBox, without parsing text:

      // creating an array of colors
      Color[] colors=new Color[3];
      colors[0]=Color.Yellow;
      colors[1]=Color.Red;
      colors[2]=myImage.GetPixel(0,0);

      or

      // creating a collection of colors
      List<Color> colors=new List<Color>();
      colors.Add(Color.Yellow);
      colors.Add(Color.Red);
      colors.Add(myImage.GetPixel(0,0));
      ...

      then

      // calculating average value of red component
      int sumRed=0;
      int count=0;
      foreach (Color color in colors) {
      sumRed+=color.R;
      count++;
      }
      int averageRed=sumRed/count;

      see? no ListBox, no strings, no problems. Also less code, and much faster. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      P 1 Reply Last reply
      0
      • L Luc Pattyn

        when you have an array (or any other collection) of colors, you can manipulate them directly. There is no need to first display them (i.e. their string representation) in a ListBox and then parse those strings, that approach is so wrong. Here is an example of what one can do, without ListBox, without parsing text:

        // creating an array of colors
        Color[] colors=new Color[3];
        colors[0]=Color.Yellow;
        colors[1]=Color.Red;
        colors[2]=myImage.GetPixel(0,0);

        or

        // creating a collection of colors
        List<Color> colors=new List<Color>();
        colors.Add(Color.Yellow);
        colors.Add(Color.Red);
        colors.Add(myImage.GetPixel(0,0));
        ...

        then

        // calculating average value of red component
        int sumRed=0;
        int count=0;
        foreach (Color color in colors) {
        sumRed+=color.R;
        count++;
        }
        int averageRed=sumRed/count;

        see? no ListBox, no strings, no problems. Also less code, and much faster. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        P Offline
        P Offline
        pancakeleh
        wrote on last edited by
        #4

        oh because my array consist of many entries made up of A, R, G, B. i dont think i can make use of your methods as listed above.

        L 1 Reply Last reply
        0
        • P pancakeleh

          oh because my array consist of many entries made up of A, R, G, B. i dont think i can make use of your methods as listed above.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #5

          pancakeleh wrote:

          my array consist of many entries made up of A, R, G, B

          and that is completely silly, why would you mix up things that you later on need separately? :|

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          P 1 Reply Last reply
          0
          • L Luc Pattyn

            pancakeleh wrote:

            my array consist of many entries made up of A, R, G, B

            and that is completely silly, why would you mix up things that you later on need separately? :|

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            P Offline
            P Offline
            pancakeleh
            wrote on last edited by
            #6

            in the first place, it is mixed up. tts why i would wanna separate it.

            1 Reply Last reply
            0
            • P pancakeleh

              I have an array which contains of RGB values as shown below: <script src='http://img801.imageshack.us/shareable/?i=array.png&p=tl' type='text/javascript'></script><noscript>[URL=http://img801.imageshack.us/i/array.png/][IMG]http://img801.imageshack.us/img801/7560/array.png[/IMG][/URL]</noscript>[^]; is there any way to just extract R into one array, B into another array and G into another array? if (array[x].Contains("R")) { .... } by using the above is it achievable?

              modified on Monday, November 8, 2010 2:51 AM

              D Offline
              D Offline
              David Ewen
              wrote on last edited by
              #7

              If you are using C# 4 you can do it like this:

              //if you have an array of strings
              string[] argbArray = new string[] { "A=255", "R=255", "G=0", "B=0", "A=255", "R=0", "G=255", "B=0" };
              string[] rArray = argbArray.Where(s => s.StartsWith("R=")).ToArray();
              string[] gArray = argbArray.Where(s => s.StartsWith("G=")).ToArray();
              string[] bArray = argbArray.Where(s => s.StartsWith("B=")).ToArray();

              //if you have an array of ints where the order is A,R,B,G. This would also work in the string one if the order was the same
              int[] argbintArray = new int[] { 255, 255, 0, 0, 255, 0, 255, 0 };
              int[] rintArray = Enumerable.Range(0, argbintArray.Length).Where(i => i % 4 == 1).Select(i => argbintArray[i]).ToArray();
              int[] gintArray = Enumerable.Range(0, argbintArray.Length).Where(i => i % 4 == 2).Select(i => argbintArray[i]).ToArray();
              int[] bintArray = Enumerable.Range(0, argbintArray.Length).Where(i => i % 4 == 3).Select(i => argbintArray[i]).ToArray();

              P 1 Reply Last reply
              0
              • D David Ewen

                If you are using C# 4 you can do it like this:

                //if you have an array of strings
                string[] argbArray = new string[] { "A=255", "R=255", "G=0", "B=0", "A=255", "R=0", "G=255", "B=0" };
                string[] rArray = argbArray.Where(s => s.StartsWith("R=")).ToArray();
                string[] gArray = argbArray.Where(s => s.StartsWith("G=")).ToArray();
                string[] bArray = argbArray.Where(s => s.StartsWith("B=")).ToArray();

                //if you have an array of ints where the order is A,R,B,G. This would also work in the string one if the order was the same
                int[] argbintArray = new int[] { 255, 255, 0, 0, 255, 0, 255, 0 };
                int[] rintArray = Enumerable.Range(0, argbintArray.Length).Where(i => i % 4 == 1).Select(i => argbintArray[i]).ToArray();
                int[] gintArray = Enumerable.Range(0, argbintArray.Length).Where(i => i % 4 == 2).Select(i => argbintArray[i]).ToArray();
                int[] bintArray = Enumerable.Range(0, argbintArray.Length).Where(i => i % 4 == 3).Select(i => argbintArray[i]).ToArray();

                P Offline
                P Offline
                pancakeleh
                wrote on last edited by
                #8

                am i still able to do this if my array has got 400 entries?

                D 1 Reply Last reply
                0
                • P pancakeleh

                  am i still able to do this if my array has got 400 entries?

                  D Offline
                  D Offline
                  David Ewen
                  wrote on last edited by
                  #9

                  Either method will work with any sized array... The first method iterates over each entry in the souce array and pulls out values that start with whatever we are looking for ie "R=" and puts them into a new array. The second method works like this: Create an array of integers that represent the indexes in the source array Enumerable.Range(0, argbintArray.Length) In groups of 4 select every seconds index .Where(i => i % 4 == 1) Select the items out of the source array at the identified indexes .Select(i => argbintArray[i]) Hopefully that makes sense. Try outputing the result of each step it might help clear up what is happening.

                  P 2 Replies Last reply
                  0
                  • D David Ewen

                    Either method will work with any sized array... The first method iterates over each entry in the souce array and pulls out values that start with whatever we are looking for ie "R=" and puts them into a new array. The second method works like this: Create an array of integers that represent the indexes in the source array Enumerable.Range(0, argbintArray.Length) In groups of 4 select every seconds index .Where(i => i % 4 == 1) Select the items out of the source array at the identified indexes .Select(i => argbintArray[i]) Hopefully that makes sense. Try outputing the result of each step it might help clear up what is happening.

                    P Offline
                    P Offline
                    pancakeleh
                    wrote on last edited by
                    #10

                    i will try it out thanks :D

                    1 Reply Last reply
                    0
                    • D David Ewen

                      Either method will work with any sized array... The first method iterates over each entry in the souce array and pulls out values that start with whatever we are looking for ie "R=" and puts them into a new array. The second method works like this: Create an array of integers that represent the indexes in the source array Enumerable.Range(0, argbintArray.Length) In groups of 4 select every seconds index .Where(i => i % 4 == 1) Select the items out of the source array at the identified indexes .Select(i => argbintArray[i]) Hopefully that makes sense. Try outputing the result of each step it might help clear up what is happening.

                      P Offline
                      P Offline
                      pancakeleh
                      wrote on last edited by
                      #11

                      if i do not use c# 4 is there any way i can do it instead?

                      D 1 Reply Last reply
                      0
                      • P pancakeleh

                        if i do not use c# 4 is there any way i can do it instead?

                        D Offline
                        D Offline
                        David Ewen
                        wrote on last edited by
                        #12

                        Without Linq then using loops like in first reponse by Nuri Ismail is the best approach.

                        P 1 Reply Last reply
                        0
                        • D David Ewen

                          Without Linq then using loops like in first reponse by Nuri Ismail is the best approach.

                          P Offline
                          P Offline
                          pancakeleh
                          wrote on last edited by
                          #13

                          okay thanks!

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups