1/* color edges based on normalized length alpha. 2 * If we have lim[i-1] <= alpha <= lim[i], the color 3 * is linearly interpolated between color[i-1] and color[i]. 4 * Edges already having a color attribute are left unchanged. 5 */ 6BEGIN { 7 double lens [edge_t]; 8 double maxlen, len, minlen=1.7976931348623157e+308; 9 double alpha, x0, y0, x1, y1; 10 double h0, s0, v0; 11 int i; 12 13 int ncolors; /* number of colors: ncolors >= 2 */ 14 /* start of color i. 15 * lim[0]=0 < lim[1] < ... < lim[ncolors-1]=1 16 */ 17 double lim[int]; 18 /* HSV values for color i */ 19 double h[int]; 20 double s[int]; 21 double v[int]; 22 23 /* simple default */ 24 ncolors = 2; 25 lim[0] = 0; 26 lim[1] = 1; 27 h[0] = 0; 28 h[1] = 0.67; 29 s[0] = 1; 30 s[1] = 1; 31 v[0] = 1; 32 v[1] = 1; 33} 34E{ 35 sscanf ($.tail.pos, "%f,%f", &x0, &y0); 36 sscanf ($.head.pos, "%f,%f", &x1, &y1); 37 38 len = sqrt ((x0-x1)*(x0-x1) + (y0-y1)*(y0-y1)); 39 40 lens[$] = len; 41 if (len > maxlen) maxlen = len; 42 if (len < minlen) minlen = len; 43} 44BEG_G { 45 if (!isAttr($,"E","color")) 46 setDflt($,"E","color",""); 47} 48E{ 49 if ($.color != "") return; 50 alpha = (lens[$]-minlen)/(maxlen-minlen); 51 for (i = 1; i < ncolors; i++) { 52 if (alpha < lim[i]) break; 53 } 54 alpha = (alpha - lim[i-1])/(lim[i] - lim[i-1]); 55 h0 = (1-alpha)*h[i-1] + alpha*h[i]; 56 s0 = (1-alpha)*s[i-1] + alpha*s[i]; 57 v0 = (1-alpha)*v[i-1] + alpha*v[i]; 58 $.color = sprintf ("%.02f %.02f %.02f", h0, s0, v0); 59} 60