2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
14 * Graphical routines for setting the palette
33 //added/remove by dph on 1/9/99
37 extern int gr_installed;
39 ubyte gr_palette[256*3];
40 ubyte gr_current_pal[256*3];
41 ubyte gr_fade_table[256*34];
43 ubyte gr_palette_gamma = 0;
44 int gr_palette_gamma_param = 0;
45 ubyte gr_palette_faded_out = 1;
47 extern void gr_palette_load( ubyte * pal );
49 void gr_palette_set_gamma( int gamma )
51 if ( gamma < 0 ) gamma = 0;
52 //added/changed on 10/27/98 by Victor Rachels to increase brightness slider
53 if ( gamma > 16 ) gamma = 16; //was 8
54 //end this section change - Victor Rachels
56 if (gr_palette_gamma_param != gamma ) {
57 gr_palette_gamma_param = gamma;
58 gr_palette_gamma = gamma;
59 if (!gr_palette_faded_out)
60 gr_palette_load( gr_palette );
64 int gr_palette_get_gamma()
66 return gr_palette_gamma_param;
70 void gr_use_palette_table( char * filename )
75 fp = cfopen( filename, "rb" );
77 Error("Can't open palette file <%s>",filename);
79 fsize = cfilelength( fp );
80 Assert( fsize == 9472 );
81 cfread( gr_palette, 256*3, 1, fp );
82 cfread( gr_fade_table, 256*34, 1, fp );
85 // This is the TRANSPARENCY COLOR
86 for (i=0; i<GR_FADE_LEVELS; i++ ) {
87 gr_fade_table[i*256+255] = 255;
92 #define SQUARE(x) ((x)*(x))
94 #define MAX_COMPUTED_COLORS 32
96 int Num_computed_colors=0;
99 ubyte r,g,b,color_num;
102 color_record Computed_colors[MAX_COMPUTED_COLORS];
104 // Add a computed color (by gr_find_closest_color) to list of computed colors in Computed_colors.
105 // If list wasn't full already, increment Num_computed_colors.
106 // If was full, replace a random one.
107 void add_computed_color(int r, int g, int b, int color_num)
111 if (Num_computed_colors < MAX_COMPUTED_COLORS) {
112 add_index = Num_computed_colors;
113 Num_computed_colors++;
115 add_index = (d_rand() * MAX_COMPUTED_COLORS) >> 15;
117 Computed_colors[add_index].r = r;
118 Computed_colors[add_index].g = g;
119 Computed_colors[add_index].b = b;
120 Computed_colors[add_index].color_num = color_num;
123 void init_computed_colors(void)
127 for (i=0; i<MAX_COMPUTED_COLORS; i++)
128 Computed_colors[i].r = 255; // Make impossible to match.
131 int gr_find_closest_color( int r, int g, int b )
134 int best_value, best_index, value;
136 if (Num_computed_colors == 0)
137 init_computed_colors();
139 // If we've already computed this color, return it!
140 for (i=0; i<Num_computed_colors; i++)
141 if (r == Computed_colors[i].r)
142 if (g == Computed_colors[i].g)
143 if (b == Computed_colors[i].b) {
146 trec = Computed_colors[i-1];
147 Computed_colors[i-1] = Computed_colors[i];
148 Computed_colors[i] = trec;
149 return Computed_colors[i-1].color_num;
151 return Computed_colors[i].color_num;
158 best_value = SQUARE(r-gr_palette[0])+SQUARE(g-gr_palette[1])+SQUARE(b-gr_palette[2]);
161 add_computed_color(r, g, b, best_index);
165 // only go to 255, 'cause we dont want to check the transparent color.
166 for (i=1; i<254; i++ ) {
168 value = SQUARE(r-gr_palette[j])+SQUARE(g-gr_palette[j+1])+SQUARE(b-gr_palette[j+2]);
169 if ( value < best_value ) {
171 add_computed_color(r, g, b, i);
178 add_computed_color(r, g, b, best_index);
182 int gr_find_closest_color_15bpp( int rgb )
184 return gr_find_closest_color( ((rgb>>10)&31)*2, ((rgb>>5)&31)*2, (rgb&31)*2 );
188 int gr_find_closest_color_current( int r, int g, int b )
191 int best_value, best_index, value;
197 best_value = SQUARE(r-gr_current_pal[0])+SQUARE(g-gr_current_pal[1])+SQUARE(b-gr_current_pal[2]);
203 // only go to 255, 'cause we dont want to check the transparent color.
204 for (i=1; i<254; i++ ) {
206 value = SQUARE(r-gr_current_pal[j])+SQUARE(g-gr_current_pal[j+1])+SQUARE(b-gr_current_pal[j+2]);
207 if ( value < best_value ) {
217 void gr_make_cthru_table(ubyte * table, ubyte r, ubyte g, ubyte b )
222 for (i=0; i<256; i++ ) {
223 r1 = gr_palette[i*3+0] + r;
224 if ( r1 > 63 ) r1 = 63;
225 g1 = gr_palette[i*3+1] + g;
226 if ( g1 > 63 ) g1 = 63;
227 b1 = gr_palette[i*3+2] + b;
228 if ( b1 > 63 ) b1 = 63;
229 table[i] = gr_find_closest_color( r1, g1, b1 );