1 /*
2   css3write.c
3 
4   write a gnuplot colour map struct to
5   a file or stream.
6 
7   J.J.Green 2010
8 */
9 
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13 
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <time.h>
18 #include <errno.h>
19 
20 #include "css3.h"
21 #include "btrace.h"
22 
css3_write(const char * file,css3_t * css3)23 extern int css3_write(const char* file,css3_t* css3)
24 {
25   FILE *st;
26   int n = css3->n;
27 
28   if ( n < 2 )
29     {
30       btrace("CSS3 does not support %i stops\n", n);
31       return 1;
32     }
33 
34   if ( file )
35     {
36       st = fopen(file, "w");
37       if (!st)
38 	{
39 	  btrace("failed to open %s : %s", strerror(errno));
40 	  return 1;
41 	}
42     }
43   else st = stdout;
44 
45   time_t t = time(NULL);
46 
47   fprintf(st,"/*\n");
48   fprintf(st,"   CSS3 gradient\n");
49   fprintf(st,"   cptutils %s\n",VERSION);
50   fprintf(st,"   %s",ctime(&t));
51   fprintf(st,"*/\n");
52   fprintf(st,"\n");
53   fprintf(st,"linear-gradient(\n");
54   fprintf(st,"  %.3gdeg,\n",css3->angle);
55 
56   for (int i = 0 ; i < n ; i++)
57     {
58       css3_stop_t stop = css3->stop[i];
59 
60       if (stop.alpha < 1.0)
61 	fprintf(st,"  rgba(%3i,%3i,%3i,%5.3f) ",
62 		stop.rgb.red,
63 		stop.rgb.green,
64 		stop.rgb.blue,
65 		stop.alpha);
66       else
67 	fprintf(st,"  rgb(%3i,%3i,%3i) ",
68 		stop.rgb.red,
69 		stop.rgb.green,
70 		stop.rgb.blue);
71 
72       fprintf(st,"%7.3f%%%s\n", stop.z, ((n-1-i) ? "," : ""));
73     }
74 
75   fprintf(st,"  );\n");
76 
77   fclose(st);
78 
79   return 0;
80 }
81