Looping through colors numerically

  • Thread starter Thread starter Jason soby
  • Start date Start date
J

Jason soby

Hi, i need to loop through a group of colors and right now, i have them set
up in an array by name, but that doesnt allow for much flexibility..

I'm writing a report using DevExpress XtraReports and i'm looping through
all the GroupHeader bands and assigning a color based on the level. Level 0
(the closest to the detail band) gets WhiteSmoke, the next one up gets
Gainsboro, the next one up gets Light Gray, but right now, i have an array

Color[] bandColors = { Color.WhiteSmoke, Color.Gainsboro, Color.LightGray };

But if there are more than three grouping bands, this will be a problem. So,
is it possible to loop through colors using math to get from WhiteSmoke >
Gainsboro > LightGray
 
Jason soby said:
Hi, i need to loop through a group of colors and right now, i have them
set
up in an array by name, but that doesnt allow for much flexibility..

I'm writing a report using DevExpress XtraReports and i'm looping through
all the GroupHeader bands and assigning a color based on the level. Level
0
(the closest to the detail band) gets WhiteSmoke, the next one up gets
Gainsboro, the next one up gets Light Gray, but right now, i have an array

Color[] bandColors = { Color.WhiteSmoke, Color.Gainsboro,
Color.LightGray };

But if there are more than three grouping bands, this will be a problem.
So,
is it possible to loop through colors using math to get from WhiteSmoke >
Gainsboro > LightGray

One way to do it is to get the RGB values of each initial and final color,
interpolate on each of the three components, and then "rebuild" the color
out of the three interpolated values.
For Simplicity, in the following example I am using equal values for R, G
and B (so you will get varios shades of gray) and I am starting with 64 and
finishing with 192.

int numberOfBands=4;
int initialShade=64;
int finalShade=192;
Color[] bandColors = new Color[numberOfBands];
for (int i=0; i<numberOfBands; i++)
{
int thisShade =
(i*(finalShade-initialShade))/numberOfbands+initialShade;
Color c = Color.FromArgb(thisShade, thisShade, thisShade);
bandColors = c;
}
 
Jason soby said:
Hi, i need to loop through a group of colors and right now, i have them set
up in an array by name, but that doesnt allow for much flexibility..

I'm writing a report using DevExpress XtraReports and i'm looping through
all the GroupHeader bands and assigning a color based on the level. Level 0
(the closest to the detail band) gets WhiteSmoke, the next one up gets
Gainsboro, the next one up gets Light Gray, but right now, i have an array

Color[] bandColors = { Color.WhiteSmoke, Color.Gainsboro, Color.LightGray };

But if there are more than three grouping bands, this will be a problem. So,
is it possible to loop through colors using math to get from WhiteSmoke >
Gainsboro > LightGray

Use bandColors [ihdr % 3], or better yet, bandColors [ihdr %
bandColors.Length]. Just increment ihdr with each new header.

Mike
 
Mike, thats a good way to loop through the colors that are there but i wanted
to actually generate the colors programatically.

Family Tree Mike said:
Jason soby said:
Hi, i need to loop through a group of colors and right now, i have them set
up in an array by name, but that doesnt allow for much flexibility..

I'm writing a report using DevExpress XtraReports and i'm looping through
all the GroupHeader bands and assigning a color based on the level. Level 0
(the closest to the detail band) gets WhiteSmoke, the next one up gets
Gainsboro, the next one up gets Light Gray, but right now, i have an array

Color[] bandColors = { Color.WhiteSmoke, Color.Gainsboro, Color.LightGray };

But if there are more than three grouping bands, this will be a problem. So,
is it possible to loop through colors using math to get from WhiteSmoke >
Gainsboro > LightGray

Use bandColors [ihdr % 3], or better yet, bandColors [ihdr %
bandColors.Length]. Just increment ihdr with each new header.

Mike
 
i thought about this, but i wasnt able to see any distinguishable pattern
with these three colors, but then again, i am somewhat a newb.

Patrice said:
You could just use
http://msdn.microsoft.com/en-us/library/system.drawing.color.fromargb.aspx
to compute a color depending on the total number of levels and the current
level...

--
Patrice


"Jason soby" <[email protected]> a écrit dans le message
de groupe de discussion :
(e-mail address removed)...
Hi, i need to loop through a group of colors and right now, i have them
set
up in an array by name, but that doesnt allow for much flexibility..

I'm writing a report using DevExpress XtraReports and i'm looping through
all the GroupHeader bands and assigning a color based on the level. Level
0
(the closest to the detail band) gets WhiteSmoke, the next one up gets
Gainsboro, the next one up gets Light Gray, but right now, i have an array

Color[] bandColors = { Color.WhiteSmoke, Color.Gainsboro,
Color.LightGray };

But if there are more than three grouping bands, this will be a problem.
So,
is it possible to loop through colors using math to get from WhiteSmoke >
Gainsboro > LightGray
 
What if i didnt yet know what color i wanted to end on?

Alberto Poblacion said:
Jason soby said:
Hi, i need to loop through a group of colors and right now, i have them
set
up in an array by name, but that doesnt allow for much flexibility..

I'm writing a report using DevExpress XtraReports and i'm looping through
all the GroupHeader bands and assigning a color based on the level. Level
0
(the closest to the detail band) gets WhiteSmoke, the next one up gets
Gainsboro, the next one up gets Light Gray, but right now, i have an array

Color[] bandColors = { Color.WhiteSmoke, Color.Gainsboro,
Color.LightGray };

But if there are more than three grouping bands, this will be a problem.
So,
is it possible to loop through colors using math to get from WhiteSmoke >
Gainsboro > LightGray

One way to do it is to get the RGB values of each initial and final color,
interpolate on each of the three components, and then "rebuild" the color
out of the three interpolated values.
For Simplicity, in the following example I am using equal values for R, G
and B (so you will get varios shades of gray) and I am starting with 64 and
finishing with 192.

int numberOfBands=4;
int initialShade=64;
int finalShade=192;
Color[] bandColors = new Color[numberOfBands];
for (int i=0; i<numberOfBands; i++)
{
int thisShade =
(i*(finalShade-initialShade))/numberOfbands+initialShade;
Color c = Color.FromArgb(thisShade, thisShade, thisShade);
bandColors = c;
}
 
Do you care about those 3 colors ? IMO no...

Alberto seems close but I don't see why you tell him you don't know the
ending color. IMHO the idea would be :
- to choose a start color (possibly a level of gray) once for all, it could
be whitesmoke
- to choose a stop color (possibly a level of gray) once for all, it could
be ligthgray

Then your code will just create colors heavenly spaced between those so
that you produce gray scale levels between those you choosed so that each
section is printed with one of these variation...

--
Patrice


"Jason soby" <[email protected]> a écrit dans le message
de groupe de discussion :
(e-mail address removed)...
i thought about this, but i wasnt able to see any distinguishable pattern
with these three colors, but then again, i am somewhat a newb.

Patrice said:
You could just use
http://msdn.microsoft.com/en-us/library/system.drawing.color.fromargb.aspx
to compute a color depending on the total number of levels and the
current
level...

--
Patrice


"Jason soby" <[email protected]> a écrit dans le
message
de groupe de discussion :
(e-mail address removed)...
Hi, i need to loop through a group of colors and right now, i have
them
set
up in an array by name, but that doesnt allow for much flexibility..

I'm writing a report using DevExpress XtraReports and i'm looping
through
all the GroupHeader bands and assigning a color based on the level.
Level
0
(the closest to the detail band) gets WhiteSmoke, the next one up gets
Gainsboro, the next one up gets Light Gray, but right now, i have an
array

Color[] bandColors = { Color.WhiteSmoke, Color.Gainsboro,
Color.LightGray };

But if there are more than three grouping bands, this will be a
problem.
So,
is it possible to loop through colors using math to get from WhiteSmoke

Gainsboro > LightGray
 
Jason soby said:
What if i didnt yet know what color i wanted to end on?

If you are doing shades of gray, there is an absolute limit to the end
color: either completely black if you are going from light to dark, or
completely white if you are going from dark to light. If you don't want to
reach that "absolue limit", then you will need to decide upon some criterion
to determine which is the value at which you want to stop. Either way, there
is no way for you to "not know" where yo want to end on.
 
Back
Top