Array Help [modified]
-
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
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.
-
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.
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.
-
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.
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.
-
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.
in the first place, it is mixed up. tts why i would wanna separate it.
-
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
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(); -
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();am i still able to do this if my array has got 400 entries?
-
am i still able to do this if my array has got 400 entries?
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. -
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.i will try it out thanks :D
-
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.if i do not use c# 4 is there any way i can do it instead?
-
if i do not use c# 4 is there any way i can do it instead?
Without Linq then using loops like in first reponse by Nuri Ismail is the best approach.
-
Without Linq then using loops like in first reponse by Nuri Ismail is the best approach.
okay thanks!