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
30 //added/remove by dph on 1/9/99
34 extern int gr_installed;
36 ubyte gr_palette[256*3];
37 ubyte gr_current_pal[256*3];
38 ubyte gr_fade_table[256*34];
40 ubyte gr_palette_gamma = 0;
41 int gr_palette_gamma_param = 0;
42 ubyte gr_palette_faded_out = 1;
44 extern void gr_palette_load( ubyte * pal );
46 void gr_palette_set_gamma( int gamma )
48 if ( gamma < 0 ) gamma = 0;
49 //added/changed on 10/27/98 by Victor Rachels to increase brightness slider
50 if ( gamma > 16 ) gamma = 16; //was 8
51 //end this section change - Victor Rachels
53 if (gr_palette_gamma_param != gamma ) {
54 gr_palette_gamma_param = gamma;
55 gr_palette_gamma = gamma;
56 if (!gr_palette_faded_out)
57 gr_palette_load( gr_palette );
61 int gr_palette_get_gamma()
63 return gr_palette_gamma_param;
67 void gr_use_palette_table( char * filename )
72 fp = cfopen( filename, "rb" );
74 Error("Can't open palette file <%s>",filename);
76 fsize = cfilelength( fp );
77 Assert( fsize == 9472 );
78 cfread( gr_palette, 256*3, 1, fp );
79 cfread( gr_fade_table, 256*34, 1, fp );
82 // This is the TRANSPARENCY COLOR
83 for (i=0; i<GR_FADE_LEVELS; i++ ) {
84 gr_fade_table[i*256+255] = 255;
89 #define SQUARE(x) ((x)*(x))
91 #define MAX_COMPUTED_COLORS 32
93 int Num_computed_colors=0;
96 ubyte r,g,b,color_num;
99 color_record Computed_colors[MAX_COMPUTED_COLORS];
101 // Add a computed color (by gr_find_closest_color) to list of computed colors in Computed_colors.
102 // If list wasn't full already, increment Num_computed_colors.
103 // If was full, replace a random one.
104 void add_computed_color(int r, int g, int b, int color_num)
108 if (Num_computed_colors < MAX_COMPUTED_COLORS) {
109 add_index = Num_computed_colors;
110 Num_computed_colors++;
112 add_index = (d_rand() * MAX_COMPUTED_COLORS) >> 15;
114 Computed_colors[add_index].r = r;
115 Computed_colors[add_index].g = g;
116 Computed_colors[add_index].b = b;
117 Computed_colors[add_index].color_num = color_num;
120 void init_computed_colors(void)
124 for (i=0; i<MAX_COMPUTED_COLORS; i++)
125 Computed_colors[i].r = 255; // Make impossible to match.
128 int gr_find_closest_color( int r, int g, int b )
131 int best_value, best_index, value;
133 if (Num_computed_colors == 0)
134 init_computed_colors();
136 // If we've already computed this color, return it!
137 for (i=0; i<Num_computed_colors; i++)
138 if (r == Computed_colors[i].r)
139 if (g == Computed_colors[i].g)
140 if (b == Computed_colors[i].b) {
143 trec = Computed_colors[i-1];
144 Computed_colors[i-1] = Computed_colors[i];
145 Computed_colors[i] = trec;
146 return Computed_colors[i-1].color_num;
148 return Computed_colors[i].color_num;
155 best_value = SQUARE(r-gr_palette[0])+SQUARE(g-gr_palette[1])+SQUARE(b-gr_palette[2]);
158 add_computed_color(r, g, b, best_index);
162 // only go to 255, 'cause we dont want to check the transparent color.
163 for (i=1; i<254; i++ ) {
165 value = SQUARE(r-gr_palette[j])+SQUARE(g-gr_palette[j+1])+SQUARE(b-gr_palette[j+2]);
166 if ( value < best_value ) {
168 add_computed_color(r, g, b, i);
175 add_computed_color(r, g, b, best_index);
179 int gr_find_closest_color_15bpp( int rgb )
181 return gr_find_closest_color( ((rgb>>10)&31)*2, ((rgb>>5)&31)*2, (rgb&31)*2 );
185 int gr_find_closest_color_current( int r, int g, int b )
188 int best_value, best_index, value;
194 best_value = SQUARE(r-gr_current_pal[0])+SQUARE(g-gr_current_pal[1])+SQUARE(b-gr_current_pal[2]);
200 // only go to 255, 'cause we dont want to check the transparent color.
201 for (i=1; i<254; i++ ) {
203 value = SQUARE(r-gr_current_pal[j])+SQUARE(g-gr_current_pal[j+1])+SQUARE(b-gr_current_pal[j+2]);
204 if ( value < best_value ) {
214 void gr_make_cthru_table(ubyte * table, ubyte r, ubyte g, ubyte b )
219 for (i=0; i<256; i++ ) {
220 r1 = gr_palette[i*3+0] + r;
221 if ( r1 > 63 ) r1 = 63;
222 g1 = gr_palette[i*3+1] + g;
223 if ( g1 > 63 ) g1 = 63;
224 b1 = gr_palette[i*3+2] + b;
225 if ( b1 > 63 ) b1 = 63;
226 table[i] = gr_find_closest_color( r1, g1, b1 );