add Outputable instance for OccIfaceEq
[ghc-hetmet.git] / utils / hp2ps / Shade.c
1 #include "Main.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "Defines.h"
6 #include "Error.h"
7 #include "Utilities.h"
8
9 /* own stuff */
10 #include "Shade.h"
11
12 static struct shade {
13         char* ident;
14         floatish shade;
15 } *shademap;
16
17 static int shademapmax = 0;
18 static int shademapindex = 0;
19
20 /*
21  *      Set the shade to be used for "ident" to "shade".
22  */
23
24 void
25 ShadeFor(ident, shade)
26   char* ident; 
27   floatish shade;
28 {
29     if (! shademap) {
30         shademapmax = (nidents > TWENTY ? nidents : TWENTY) * 2;
31                  /* Assume nidents read is indication of the No of
32                     idents in the .aux file (*2 for good luck) */
33                  /* NB *2 is needed as .aux and .hp elements may differ */
34         shademap = xmalloc(shademapmax * sizeof(struct shade));
35     }
36
37     if (shademapindex < shademapmax) {
38         shademap[ shademapindex ].ident = copystring(ident);
39         shademap[ shademapindex ].shade = shade;
40         shademapindex++;
41     } else {
42         Disaster("shade map overflow");
43     }
44 }
45
46 /*
47  *      Get the shade to be used for "ident" if there is one. 
48  *      Otherwise, think of a new one.
49  */
50
51 static floatish ThinkOfAShade PROTO((void));    /* forward */
52
53 floatish
54 ShadeOf(ident)
55   char* ident;
56 {
57     int i;
58     floatish shade;
59
60     for (i = 0; i < shademapindex; i++) {
61         if (strcmp(shademap[i].ident, ident) == 0) {    /* got it */
62             return(shademap[i].shade);
63         }
64     }
65
66     shade = ThinkOfAShade();
67
68     ShadeFor(ident, shade);
69
70     return shade; 
71 }
72
73
74
75 #define N_MONO_SHADES 10 
76
77 static floatish m_shades[ N_MONO_SHADES ] = {
78     0.00000, 0.20000, 0.60000, 0.30000, 0.90000, 
79     0.40000, 1.00000, 0.70000, 0.50000,  0.80000
80 };
81
82 #define N_COLOUR_SHADES 27
83
84 /* HACK: 0.100505 means 100% red, 50% green, 50% blue */
85
86 static floatish c_shades[ N_COLOUR_SHADES ] = {
87     0.000000, 0.000010, 0.001000, 0.001010, 0.100000,
88     0.100010, 0.101000, 0.101010, 0.000005, 0.000500,
89     0.000510, 0.001005, 0.050000, 0.050010, 0.051000,
90     0.051010, 0.100005, 0.100500, 0.100510, 0.101005,
91     0.000505, 0.050005, 0.050500, 0.050510, 0.051005,
92     0.100505, 0.050505
93 };
94
95 static floatish
96 ThinkOfAShade()
97 {
98     static int thisshade = -1;
99
100     thisshade++;
101     return cflag ?
102         c_shades[ thisshade % N_COLOUR_SHADES ] :
103         m_shades[ thisshade % N_MONO_SHADES   ] ;
104 }
105
106 static floatish
107 extract_colour(shade,factor)
108   floatish shade;
109   intish factor;
110 {
111     intish i,j;
112
113     i = (int)(shade * factor);
114     j = i / 100;
115     return (i - j * 100) / 10.0;
116 }
117
118 void
119 SetPSColour(shade)
120   floatish shade;
121 {
122     if (cflag) {
123         fprintf(psfp, "%f %f %f setrgbcolor\n",
124                 extract_colour(shade, (intish)100),
125                 extract_colour(shade, (intish)10000),
126                 extract_colour(shade, (intish)1000000));
127     } else {
128         fprintf(psfp, "%f setgray\n", shade);
129     }
130 }