Rulesave saves trade.type and trade.bonus correctly.
[freeciv.git] / client / colors_common.c
blob2d0532b1ab450b295699b1bf2159edd9c62a902b
1 /**********************************************************************
2 Freeciv - Copyright (C) 2005 - The Freeciv Project
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 /* utility */
19 #include "log.h"
20 #include "shared.h"
22 /* common */
23 #include "player.h"
24 #include "rgbcolor.h"
26 /* client/include */
27 #include "colors_g.h"
29 /* client */
30 #include "tilespec.h"
32 #include "colors_common.h"
34 struct color_system {
35 struct rgbcolor **stdcolors;
38 /****************************************************************************
39 Called when the client first starts to allocate the default colors.
41 Currently this must be called in ui_main, generally after UI
42 initialization.
43 ****************************************************************************/
44 struct color_system *color_system_read(struct section_file *file)
46 struct color_system *colors = fc_malloc(sizeof(*colors));
47 enum color_std stdcolor;
49 colors->stdcolors = fc_calloc(COLOR_LAST, sizeof(*colors->stdcolors));
51 for (stdcolor= color_std_begin(); stdcolor!= color_std_end();
52 stdcolor= color_std_next(stdcolor)) {
53 struct rgbcolor *prgbcolor = NULL;
54 if (rgbcolor_load(file, &prgbcolor, "colors.%s0",
55 color_std_name(stdcolor))) {
56 *(colors->stdcolors + stdcolor) = prgbcolor;
57 } else {
58 log_error("Color %s: %s", color_std_name(stdcolor), secfile_error());
59 *(colors->stdcolors + stdcolor) = rgbcolor_new(0, 0, 0);
63 return colors;
66 /****************************************************************************
67 Called when the client first starts to free any allocated colors.
68 ****************************************************************************/
69 void color_system_free(struct color_system *colors)
71 enum color_std stdcolor;
73 for (stdcolor= color_std_begin(); stdcolor!= color_std_end();
74 stdcolor= color_std_next(stdcolor)) {
75 rgbcolor_destroy(*(colors->stdcolors + stdcolor));
78 free(colors->stdcolors);
80 free(colors);
83 /****************************************************************************
84 Return the RGB color, allocating it if necessary.
85 ****************************************************************************/
86 struct color *ensure_color(struct rgbcolor *rgb)
88 fc_assert_ret_val(rgb != NULL, NULL);
90 if (!rgb->color) {
91 rgb->color = color_alloc(rgb->r, rgb->g, rgb->b);
93 return rgb->color;
96 /****************************************************************************
97 Return a pointer to the given "standard" color.
98 ****************************************************************************/
99 struct color *get_color(const struct tileset *t, enum color_std stdcolor)
101 struct color_system *colors = get_color_system(t);
103 fc_assert_ret_val(colors != NULL, NULL);
105 return ensure_color(*(colors->stdcolors + stdcolor));
108 /**********************************************************************
109 Return whether the player has a color assigned yet.
110 Should only be FALSE in pregame.
111 ***********************************************************************/
112 bool player_has_color(const struct tileset *t,
113 const struct player *pplayer)
115 fc_assert_ret_val(pplayer != NULL, NULL);
117 return pplayer->rgb != NULL;
120 /**********************************************************************
121 Return the color of the player.
122 In pregame, callers should check player_has_color() before calling
123 this.
124 ***********************************************************************/
125 struct color *get_player_color(const struct tileset *t,
126 const struct player *pplayer)
128 fc_assert_ret_val(pplayer != NULL, NULL);
129 fc_assert_ret_val(pplayer->rgb != NULL, NULL);
131 return ensure_color(pplayer->rgb);
134 /****************************************************************************
135 Return a pointer to the given "terrain" color.
136 ****************************************************************************/
137 struct color *get_terrain_color(const struct tileset *t,
138 const struct terrain *pterrain)
140 fc_assert_ret_val(pterrain != NULL, NULL);
141 fc_assert_ret_val(pterrain->rgb != NULL, NULL);
143 return ensure_color(pterrain->rgb);
146 /****************************************************************************
147 Find the colour from 'candidates' with the best perceptual contrast from
148 'subject'.
149 ****************************************************************************/
150 struct color *color_best_contrast(struct color *subject,
151 struct color **candidates, int ncandidates)
153 int sbright = color_brightness_score(subject), bestdiff = 0;
154 int i;
155 struct color *best = NULL;
157 fc_assert_ret_val(candidates != NULL, NULL);
158 fc_assert_ret_val(ncandidates > 0, NULL);
160 for (i = 0; i < ncandidates; i++) {
161 int cbright = color_brightness_score(candidates[i]);
162 int diff = ABS(sbright - cbright);
164 if (best == NULL || diff > bestdiff) {
165 best = candidates[i];
166 bestdiff = diff;
170 return best;