LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Generating colour values for multi-line graphs automatically?

Has anyone here seen or heard of any code to generate colour values automatically, for a large number of plotlines? I tried a search on these forums, but didn't see anything.

I will need to plot at least 30 lines, sometimes more. I was hoping for an elegant way to generate fairly distinguishable colours. I'm far too lazy to build a table from scratch, with a ton of colour values - or - am I losing it, and is there a way to get CVI to take care of it automatically?

Thanks,
Avner Ginsburg


0 Kudos
Message 1 of 5
(3,903 Views)

Hello,

I never been faced to a soo large plot graph. I think however you can use the MakeColor function, changing the RGB value you pass.

This can be an example:

#define NUMCOLOR 30        
int shift = 0, i,color[NUMCOLOR],inc = 0, clr[3];   
 
 for(i=0;i<NUMCOLOR;i++){
  inc += 0xFF/NUMCOLOR;   
  clr[shift%3] = inc;         // one color increased
  clr[(shift+1)%3] = 0xff-i*NUMCOLOR/0xff;
  clr[(shift+2)%3] = 0;        // one color set to 0
  
  shift++;           // shift RGB for next loop
  color[i] = MakeColor (clr[0], clr[1], clr[2]);  // create a color
 }

I don't know if using a random function you obtain better distinguishing colors, or if you need to modify some colors because they are too similar to your background color.

Hope this help

0 Kudos
Message 2 of 5
(3,888 Views)

Hi.

Depending on the type of data you are working with, you might be able to work with a reasonably small number of colors, if you use them in combination with multiple Line Styles, Point Styles or even Plot Styles.

Regards,
Colin.

 

0 Kudos
Message 3 of 5
(3,850 Views)
Thanks for the replies (Sorry, I was away).

The code posted above isn't bad. It alternates 3 different colours, which all slowly get more intense. Which would be ok, if each graph plot was distinct.

I guess I just hoped that someone would have encountered this before. Out of say 30 or 40 plotlines, I'd think there could be a colour set that were fairly independant (hence distinguishable) from each other.

I ended up trying to emulate a colour wheel, with advancing angle computed onto projections from three points, i.e. red, green, and blue, but that still didn't produce enough trully independent colour combos (due to the cheap way I computed them 🙂 ).

If I get a chance to have another go at it, I'll post anything interesting I come up with. Or I'll just manually build a table (ick!).

Cheers,
Avner Ginsburg


0 Kudos
Message 4 of 5
(3,835 Views)
I found your message while googling for similar problem.
 
I solved this specific issue by following algorithm:
  1. select random color
  2. compute distance from group of selected colors, and select most far away color as next color
  3. go back to 1
I sorted out 216 "web-safe" colors with above method, and the result was pretty acceptable.
I haven't tackled more generic N-color selection problem, but for me, above was good enough for N < 50 or so.
 
For distance calculation, I tried calculation in L*a*b*/Lch colorspace and modified-YIQ colorspace recommended
by W3C Accessibility Guideline (http://www.w3.org/TR/AERT#color-contrast). Both resulted to acceptable result,
but W3C method is simpler and doesn't require understanding of color definition.
double
RGB_distance_w3c(RGB a, RGB b) {
     double db
          = (a.r * 299 + a.g * 587 + a.b * 114) / 1000
          - (b.r * 299 + b.g * 587 + b.b * 114) / 1000;
     double dc
          = (maxl(a.r, b.r) - minl(a.r, b.r))
          + (maxl(a.g, b.g) - minl(a.g, b.g))
          + (maxl(a.b, b.b) - minl(a.b, b.b));
     // smaller brightness difference is enough to express color difference
     db /= 125;
     dc /= 500;
     return sqrt(db * db + dc * dc) * 10000;
}
I've done it in C and above was the distance calculation function I used.
 
If you want to go with much complex (but theoretically backed) L*a*b*, check out
 
 
for RGB-to-L*a*b* conversion.
 
Hope this helps.
0 Kudos
Message 5 of 5
(3,683 Views)