/*------------------------------------------------------* | Author: Maurizio Loreti, aka MLO or (HAM) I3NOO | | Work: University of Padova - Department of Physics | | Via F. Marzolo, 8 - 35131 PADOVA - Italy | | Phone: +39 (049) 827-7216 FAX: +39 (049) 827-7102 | | EMail: loreti@pd.infn.it | | WWW: http://www.pd.infn.it/~loreti/mlo.html | *------------------------------------------------------* That program reads the input file generated by "genfile.awk"; and builds two LaTeX files called "rgb.txt" and "colordef.tex". The latter contains definitions for all the known colors, suitable for the standard "color" package; the former generates a table with a framed box containing the given color, plus the RGB codes according to three different notations. *------------------------------------------------------------------* $Id: makergb.c,v 1.2 2004/02/04 07:17:28 loreti Exp $ *------------------------------------------------------------------*/ #include #include #include #define BUF_LEN 64 #define REC_PER_TABLE 44 #define OUT_FILE_1 "rgb.tex" #define OUT_FILE_2 "colordef.tex" #define ERR_OUT "Error opening output file \"%s\"\n" int main( int argc, char *argv[] ){ FILE *fin; /* Input file */ FILE *fout1; /* First output file (tex source) */ FILE *fout2; /* Second output file (color definitions) */ char buf[BUF_LEN]; /* Input buffer */ int nrec_in = 0; /* Number of records in input */ int nrec_out = 0; /* Number of records in output */ #ifdef DEBUG int len, maxlen = 0; #endif if (argc < 2) { puts("Usage: makergb "); return EXIT_SUCCESS; } if ((fin = fopen(argv[1], "r")) == 0) { printf("Error opening input file \"%s\"\n", argv[1]); return EXIT_FAILURE; } if ((fout1 = fopen(OUT_FILE_1, "w")) == 0) { printf(ERR_OUT, OUT_FILE_1); fclose(fin); return EXIT_FAILURE; } if ((fout2 = fopen(OUT_FILE_2, "w")) == 0) { printf(ERR_OUT, OUT_FILE_2); fclose(fout1); fclose(fin); return EXIT_FAILURE; } /** | LaTeX preamble **/ fputs("\\documentclass[a4paper]{article}\n", fout1); fputs("\\usepackage[dvips]{color}\n", fout1); fputs("\\usepackage[T1]{fontenc}\n", fout1); fputs("\n", fout1); fputs("\\renewcommand{\\rmdefault}{pcr}", fout1); fputs("\n", fout1); fputs("\\setlength{\\textwidth}{159.2mm}\n", fout1); fputs("\\setlength{\\oddsidemargin}{0pt}\n", fout1); fputs("\\setlength{\\evensidemargin}{0pt}\n", fout1); fputs("\\setlength{\\marginparwidth}{0pt}\n", fout1); fputs("\n", fout1); fputs("\\input colordef\n", fout1); fputs("\n", fout1); fputs("\\begin{document}\n", fout1); while (fgets(buf, BUF_LEN, fin)) { char c; /* Current input character */ char color[BUF_LEN]; /* Color name (with spaces) */ char color1[BUF_LEN]; /* Color name (without spaces) */ int r, g, b; /* Color codes [0 ... 255] */ double rr, gg, bb; /* Color codes [0.0 ... 1.0] */ char *pin; /* Pointers to the input buffer, */ char *pout; /* the color[] output buffer, */ char *pout1; /* the color1[] output buffer, */ char *lnb = 0; /* and the last non-blank char */ /** | Input file format: | 1st record - color name (may include blanks); | 2nd record - RGB values, 0..255. **/ switch (++nrec_in % 2) { case 1: /* Odd records: color name */ pin = buf; pout = color; pout1 = color1; /** | The name is copied to color[] and color1[]; the first | character will be capitalized. Only non-whitespace | characters will be copied to color1[], and "lnb" will point | to the last non-whitespace character copied into color[]. **/ for (c = toupper(*pin); c != '\0'; c = *(++pin)) { if (!isspace(c)) { *pout1++ = c; lnb = pout; } *pout++ = c; } *(++lnb) = *pout1 = '\0'; #ifdef DEBUG len = lnb - color; if (len > maxlen) maxlen = len; #endif break; case 0: /* Even records: color codes */ sscanf(buf, "%d %d %d", &r, &g, &b); rr = (double) r / 255.0; gg = (double) g / 255.0; bb = (double) b / 255.0; if (nrec_out % REC_PER_TABLE == 0) { if (nrec_out != 0) { fputs(" \\hline\n", fout1); fputs(" \\end{tabular}\n", fout1); fputs("\\end{center}\n", fout1); fputs("\\clearpage\n", fout1); } fputs("\\begin{center}\n", fout1); fputs(" \\begin{tabular}{|l|ccc|c|ccc|c|}\n", fout1); fputs(" \\hline\n", fout1); fputs(" Color Name & R & G & B & Hex & r & g & b & \\\\\n", fout1); fputs(" \\hline\n", fout1); } nrec_out++; fprintf(fout1, " %-25s & %03d & %03d & %03d & %02X%02X%02X &", color, r, g, b, r, g, b); fprintf(fout1, "%.2f & %.2f & %.2f\n", rr, gg, bb); fprintf(fout1, " & \\fcolorbox{black}{%s}{\\rule[.7ex]{53pt}{0pt}} \\\\\n", color1); fprintf(fout2, "\\definecolor{%s}{rgb}{%.2f,%.2f,%.2f}\n", color1, rr, gg, bb); break; } } #ifdef DEBUG printf("maxlen = %d\n", maxlen); #endif fputs(" \\hline\n", fout1); fputs(" \\end{tabular}\n", fout1); fputs("\\end{center}\n", fout1); fputs("\\vfill\n", fout1); fputs("\\begin{center}\n", fout1); fputs("Author: \\textcolor{OliveDrab}{Maurizio Loreti}\\\\\n", fout1); fputs("(email: \\textcolor{Blue}{loreti " "at pd dot infn dot it})\\\\[4ex]\n", fout1); fputs("\\color{DarkOrchid2}\n", fout1); fputs("Source and documentation available under the URL\\\\\n", fout1); fputs("\\textcolor{Red}{http://wwwcdf.pd.infn.it/MLO/}\\\\\n", fout1); fputs("(follow the link to the directory \\textcolor{Red}{rgb},\n", fout1); fputs("or rather download \\textcolor{Red}{ce.tar.gz}).\n", fout1); fputs("\\end{center}\n", fout1); fputs("\\end{document}\n", fout1); fclose(fin); fclose(fout1); fclose(fout2); return EXIT_SUCCESS; }