Stop sharing requirement_unit_state_ereq().
[freeciv.git] / client / colors_common.c
blobb4ef253ed2b80bd467a55a072e3a53f413cbbe26
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;
55 if (rgbcolor_load(file, &prgbcolor, "colors.%s0",
56 color_std_name(stdcolor))) {
57 *(colors->stdcolors + stdcolor) = prgbcolor;
58 } else {
59 log_error("Color %s: %s", color_std_name(stdcolor), secfile_error());
60 *(colors->stdcolors + stdcolor) = rgbcolor_new(0, 0, 0);
64 return colors;
67 /****************************************************************************
68 Called when the client first starts to free any allocated colors.
69 ****************************************************************************/
70 void color_system_free(struct color_system *colors)
72 enum color_std stdcolor;
74 for (stdcolor = color_std_begin(); stdcolor != color_std_end();
75 stdcolor = color_std_next(stdcolor)) {
76 rgbcolor_destroy(*(colors->stdcolors + stdcolor));
79 free(colors->stdcolors);
81 free(colors);
84 /****************************************************************************
85 Return the RGB color, allocating it if necessary.
86 ****************************************************************************/
87 struct color *ensure_color(struct rgbcolor *rgb)
89 fc_assert_ret_val(rgb != NULL, NULL);
91 if (!rgb->color) {
92 rgb->color = color_alloc(rgb->r, rgb->g, rgb->b);
94 return rgb->color;
97 /****************************************************************************
98 Return a pointer to the given "standard" color.
99 ****************************************************************************/
100 struct color *get_color(const struct tileset *t, enum color_std stdcolor)
102 struct color_system *colors = get_color_system(t);
104 fc_assert_ret_val(colors != NULL, NULL);
106 return ensure_color(*(colors->stdcolors + stdcolor));
109 /**********************************************************************
110 Return whether the player has a color assigned yet.
111 Should only be FALSE in pregame.
112 ***********************************************************************/
113 bool player_has_color(const struct tileset *t,
114 const struct player *pplayer)
116 fc_assert_ret_val(pplayer != NULL, NULL);
118 return pplayer->rgb != NULL;
121 /**********************************************************************
122 Return the color of the player.
123 In pregame, callers should check player_has_color() before calling
124 this.
125 ***********************************************************************/
126 struct color *get_player_color(const struct tileset *t,
127 const struct player *pplayer)
129 fc_assert_ret_val(pplayer != NULL, NULL);
130 fc_assert_ret_val(pplayer->rgb != NULL, NULL);
132 return ensure_color(pplayer->rgb);
135 /****************************************************************************
136 Return a pointer to the given "terrain" color.
137 ****************************************************************************/
138 struct color *get_terrain_color(const struct tileset *t,
139 const struct terrain *pterrain)
141 fc_assert_ret_val(pterrain != NULL, NULL);
142 fc_assert_ret_val(pterrain->rgb != NULL, NULL);
144 return ensure_color(pterrain->rgb);
147 /****************************************************************************
148 Find the colour from 'candidates' with the best perceptual contrast from
149 'subject'.
150 ****************************************************************************/
151 struct color *color_best_contrast(struct color *subject,
152 struct color **candidates, int ncandidates)
154 int sbright = color_brightness_score(subject), bestdiff = 0;
155 int i;
156 struct color *best = NULL;
158 fc_assert_ret_val(candidates != NULL, NULL);
159 fc_assert_ret_val(ncandidates > 0, NULL);
161 for (i = 0; i < ncandidates; i++) {
162 int cbright = color_brightness_score(candidates[i]);
163 int diff = ABS(sbright - cbright);
165 if (best == NULL || diff > bestdiff) {
166 best = candidates[i];
167 bestdiff = diff;
171 return best;