File: pscol.c

package info (click to toggle)
libforms 1.0.93sp1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,548 kB
  • ctags: 9,107
  • sloc: ansic: 97,227; sh: 9,236; makefile: 858
file content (228 lines) | stat: -rw-r--r-- 5,548 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * This file is part of XForms.
 *
 * XForms is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1, or
 * (at your option) any later version.
 *
 * XForms is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XForms. If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * \file pscol.c
 *
 *  This file is part of XForms package
 *  Copyright (c) 1997-2000  by T.C. Zhao
 *  All rights reserved.
 *
 * Take care of color
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "include/forms.h"
#include "flinternal.h"
#include <math.h>
#include "fd2ps.h"

#define VN( a )    a, #a
#define NV( a )    #a, a

/* slightly different from XForms' built-in due to the difference
 * between  printer and screen
 */

#define PREVIEW

static FLI_IMAP fl_imap[ ] =
{
    { NV( FL_BLACK), 0, 0, 0, 0, 0},
    { NV( FL_WHITE), 255, 255, 255, 0, 0},

    { NV( FL_COL1), 161, 161, 161, 0, 0},     /* default color, gray63 */
    { NV( FL_MCOL), 191, 191, 191, 0, 0},     /* used as magic, gray75 */
    { NV( FL_RIGHT_BCOL), 41, 41, 41, 0, 0},  /* right  gray16  */
    { NV( FL_BOTTOM_BCOL), 89, 89, 89, 0, 0}, /* bottom  gray35 */
    { NV( FL_LEFT_BCOL), 222, 222, 222, 0, 0},    /* left color  gray87 */

    { NV( FL_SLATEBLUE), 113, 113, 198, 0, 0},
    { NV( FL_INDIANRED), 198, 113, 113, 0, 0},

    { NV( FL_RED), 255, 0, 0, 0, 0},
    { NV( FL_BLUE), 0, 0, 255, 0, 0},
    { NV( FL_GREEN), 0, 255, 0, 0, 0},
    { NV( FL_YELLOW), 255, 255, 0, 0, 0},
    { NV( FL_MAGENTA), 255, 0, 255, 0, 0},
    { NV( FL_CYAN), 0, 255, 255, 0, 0},
    { NV( FL_TOMATO), 255, 99, 71, 0, 0},

    { NV( FL_INACTIVE), 110, 110, 110, 0, 0}, /* gray43       */
    { NV( FL_TOP_BCOL), 204, 204, 204, 0, 0}, /* top  gray80  */

    { NV( FL_PALEGREEN), 113, 198, 113, 0, 0},
    { NV( FL_DARKGOLD), 205, 149, 10, 0, 0},
    { NV( FL_ORCHID), 205, 105, 201, 0, 0},
    { NV( FL_DARKCYAN), 40, 170, 175, 0, 0},
    { NV( FL_DARKTOMATO), 139, 54, 38, 0, 0},
    { NV( FL_WHEAT), 255, 231, 155, 0, 0},
    { NV( FL_DARKORANGE), 255, 128, 0, 0, 0},
    { NV( FL_DEEPPINK), 255, 0, 128, 0, 0},
    { NV( FL_CHARTREUSE), 128, 255, 0, 0, 0},
    { NV( FL_DARKVIOLET), 128, 0, 255, 0, 0},
    { NV( FL_SPRINGGREEN), 0, 255, 128, 0, 0},
    { NV( FL_DODGERBLUE), 0, 128, 255, 0, 0},
    { NV( FL_DOGERBLUE), 0, 128, 255, 0, 0},
    { NV( FL_LIGHTER_COL1), 173, 173, 173, 0, 0},
    { NV( FL_DARKER_COL1), 153, 153, 153, 0, 0}
};

#define builtin   sizeof(fl_imap)/sizeof(fl_imap[0])


/***************************************
 * do gamma adjustment
 ***************************************/

void
apply_gamma( float gamma )
{
    FLI_IMAP *fm = fl_imap,
             *fs;
    float lastgamma = 1.0;

    if ( FL_abs( gamma ) < 1.0e-3 )
    {
        fprintf( stderr, "fd2ps: Bad Gamma value %.2f\n", gamma );
        return;
    }

    for ( fs = fm + builtin; fm < fs; fm++ )
    {
        if ( psinfo.verbose )
            fprintf( stderr, "fm->r=%d\n", fm->r );

        fm->r = 0.4 + 255.0 * pow( fm->r / 255.0, lastgamma / gamma );
        fm->g = 0.4 + 255.0 * pow( fm->g / 255.0, lastgamma / gamma );
        fm->b = 0.4 + 255.0 * pow( fm->b / 255.0, lastgamma / gamma );

        if ( psinfo.verbose )
            fprintf( stderr, "fm->r=%d\n", fm->r );
    }

    lastgamma = gamma;
}


/***************************************
 ***************************************/

void
fl_query_imap( long   col,
               int  * r,
               int  * g,
               int  * b )
{
    FLI_IMAP *flmap = fl_imap,
             *flmape = flmap + builtin;

    for ( ; flmap < flmape; flmap++ )
        if ( col == ( long ) flmap->index )
        {
            *r = flmap->r;
            *g = flmap->g;
            *b = flmap->b;
            return;
        }
}


#define  C2NC( c )             ( 1.0 / 255.0 * c )

static long cur_color = -1;


/***************************************
 ***************************************/

void
ps_invalidate_color_cache( void )
{
    cur_color = -1;
}


/***************************************
 ***************************************/

void
ps_color( long color )
{
    int r = 0,
        g = 0,
        b = 0;

    if ( color == cur_color )
        return;

    fl_query_imap( color, &r, &g, &b );

    if ( psinfo.colorps && ( r != g || r != b ) )
        ps_output( "%.3g %.3g %.3g RGB ", C2NC( r), C2NC( g ), C2NC( b ) );
    else
        ps_output( "%.3g G ", C2NC( rgb2gray( r, g, b ) ) );

    cur_color = color;
}


/***************************************
 ***************************************/

int
get_gray255( long color )
{
    int r = 0,
        g = 0,
        b = 0;

    fl_query_imap( color, &r, &g, &b );

    return rgb2gray( r, g, b ) + 0.1;
}


/***************************************
 ***************************************/

int
fl_get_namedcolor( const char * s )
{
    FLI_IMAP *flmap = fl_imap,
             *flmape = flmap + builtin;

    for ( ; s && flmap < flmape; flmap++ )
        if ( strcmp(s, flmap->name) == 0 )
            return flmap->index;

    /* a wild shot */

    return atoi( s );
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */