summaryrefslogtreecommitdiff
path: root/utils/hp2ps/Shade.c
blob: 9e3274bf69f289a68da38a2c837eb5f200b5c833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "Main.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Defines.h"
#include "Error.h"
#include "Utilities.h"

/* own stuff */
#include "Shade.h"

static struct shade {
	char* ident;
	floatish shade;
} *shademap;

static int shademapmax = 0;
static int shademapindex = 0;

/*
 *	Set the shade to be used for "ident" to "shade".
 */

void
ShadeFor(ident, shade)
  char* ident; 
  floatish shade;
{
    if (! shademap) {
	shademapmax = (nidents > TWENTY ? nidents : TWENTY) * 2;
	         /* Assume nidents read is indication of the No of
		    idents in the .aux file (*2 for good luck) */
	         /* NB *2 is needed as .aux and .hp elements may differ */
	shademap = xmalloc(shademapmax * sizeof(struct shade));
    }

    if (shademapindex < shademapmax) {
	shademap[ shademapindex ].ident = copystring(ident);
	shademap[ shademapindex ].shade = shade;
	shademapindex++;
    } else {
	Disaster("shade map overflow");
    }
}

/*
 *	Get the shade to be used for "ident" if there is one. 
 *	Otherwise, think of a new one.
 */

static floatish ThinkOfAShade PROTO((void));	/* forward */

floatish
ShadeOf(ident)
  char* ident;
{
    int i;
    floatish shade;

    for (i = 0; i < shademapindex; i++) {
	if (strcmp(shademap[i].ident, ident) == 0) {	/* got it */
	    return(shademap[i].shade);
	}
    }

    shade = ThinkOfAShade();

    ShadeFor(ident, shade);

    return shade; 
}



#define N_MONO_SHADES 10 

static floatish m_shades[ N_MONO_SHADES ] = {
    0.00000, 0.20000, 0.60000, 0.30000, 0.90000, 
    0.40000, 1.00000, 0.70000, 0.50000,  0.80000
};

#define N_COLOUR_SHADES 27

/* HACK: 0.100505 means 100% red, 50% green, 50% blue */

static floatish c_shades[ N_COLOUR_SHADES ] = {
    0.000000, 0.000010, 0.001000, 0.001010, 0.100000,
    0.100010, 0.101000, 0.101010, 0.000005, 0.000500,
    0.000510, 0.001005, 0.050000, 0.050010, 0.051000,
    0.051010, 0.100005, 0.100500, 0.100510, 0.101005,
    0.000505, 0.050005, 0.050500, 0.050510, 0.051005,
    0.100505, 0.050505
};

static floatish
ThinkOfAShade()
{
    static int thisshade = -1;

    thisshade++;
    return cflag ?
	c_shades[ thisshade % N_COLOUR_SHADES ] :
	m_shades[ thisshade % N_MONO_SHADES   ] ;
}

static floatish
extract_colour(shade,factor)
  floatish shade;
  intish factor;
{
    intish i,j;

    i = (int)(shade * factor);
    j = i / 100;
    return (i - j * 100) / 10.0;
}

void
SetPSColour(shade)
  floatish shade;
{
    if (cflag) {
	fprintf(psfp, "%f %f %f setrgbcolor\n",
		extract_colour(shade, (intish)100),
		extract_colour(shade, (intish)10000),
		extract_colour(shade, (intish)1000000));
    } else {
	fprintf(psfp, "%f setgray\n", shade);
    }
}