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)
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 ***********************************************************************/
15 #include <fc_config.h>
32 #include "colors_common.h"
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
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
;
59 log_error("Color %s: %s", color_std_name(stdcolor
), secfile_error());
60 *(colors
->stdcolors
+ stdcolor
) = rgbcolor_new(0, 0, 0);
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
);
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
);
92 rgb
->color
= color_alloc(rgb
->r
, rgb
->g
, rgb
->b
);
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
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
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;
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
];