fun with colors

1. Senior Member
Join Date
Dec 1969
Posts
115

fun with colors

I printing a graph in html and I&#039;d like to generate the colors for each bar on the fly based on how many bars are in each group. I started with a formula thus:<BR><BR>hex(16777215/numofbars*currentbarnumber)<BR><BR>This splits the entire spectrum of colors up evenly and it&#039;s ok but I&#039;d like my number to always fall in the more saturated areas of the color spectrum.<BR><BR>Can anyone tell me what areas of the color spectrum are more saturated and is there a formula to isolate those colors?

2. Senior Member
Join Date
Dec 1969
Posts
96,118

What is max # of bars...

...you will ever need???<BR><BR>If it&#039;s fewer than 50 or so, why not just set up an array of colors ahead of time and then pick<BR> ColorArray( currentBarNumber )<BR>or<BR> ColorArray[ currentBarNumber ]<BR>depending on language???<BR><BR>The other thing to worry about is people with displays that only support 256 colors. That means you should limit yourself to the "safe" colors, which are 00, 33, 66, 99, CC, or FF in each of the RGB slots. [That gives you 216 "web colors" and thus allows room for other colors on the user&#039;s monitor.]<BR><BR>And if you want saturated colors, you&#039;d probably want to limit it further.<BR><BR>Personally, I&#039;d go with the pre-set array.<BR><BR>Easy in VBS or JS:<BR><BR>Colors = Array("red", "lightred", "green", ... ) &#039; VBS<BR>var Colors = new Array("red", "lightred", "green", ... ); // JS<BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
115

RE: What is max # of bars...

Yeah, a fixed would probably be fine, I did want to try the mental exercise of figuring this out tho&#039;. I guess I&#039;ll create a fixed array and fiddle with this later. Thanx for the tip about websafe colors.<BR><BR>MYK

4. Senior Member
Join Date
Dec 1969
Posts
96,118

Well, if this is for the exercise...

...hmmm....<BR><BR>&#060;%<BR>Function WSColor( rownum )<BR> R = &H330000 * ( rownum MOD 6 )<BR> G = &H003300 * ( ( rownum 6 ) MOD 6 )<BR> B = &H000033 * ( ( rownum 36 ) MOD 6 )<BR> WSColor = "#" & Right ( "000000" & Hex(R+G+B), 6 )<BR>End Function<BR>%&#062;<BR><BR>???<BR><BR>Untested. Off top of my head.<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

Yep! It works...

Test page:<BR><BR>*************<BR>&#060;HTML&#062;&#06 0;BODY&#062;<BR>&#060;%<BR>Function WSColor( rownum )<BR> R = &H330000 * ( rownum MOD 6 )<BR> G = &H003300 * ( ( rownum 6 ) MOD 6 )<BR> B = &H000033 * ( ( rownum 36 ) MOD 6 )<BR> WSColor = "#" & Right ( "000000" & Hex(R+G+B), 6 )<BR>End Function<BR><BR>%&#062;<BR>&#060;TABLE Border=1 width="80%"&#062;<BR>&#060;%<BR>For cell = 0 TO 215<BR> If cell MOD 6 = 0 Then Response.Write "&#060;TR&#062;"<BR> Response.Write "&#060;TD width=&#039;15%&#039; bgColor=&#039;" & WSColor(cell) & "&#039;&#062;&nbsp;&#060;/TD&#062;"<BR> If cell MOD 6 = 5 Then Response.Write "&#060;/TR&#062;"<BR>Next<BR>%&#062;<BR>&#060;/TABLE&#062;<BR><BR>&#060;/BODY&#062;&#060;/HTML&#062;<BR><BR>You could have any number for "cell" but it will cycle around after 215, or course.<BR><BR>*NOW* you get the challenge of picking out only the "usable" colors from that!<BR><BR>

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•