Freeciv-3.1: /fast/freeciv/nightly/S3_1/client/colors_common.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
colors_common.c
Go to the documentation of this file.
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.
7
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***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* utility */
19#include "log.h"
20#include "shared.h"
21
22/* common */
23#include "player.h"
24#include "rgbcolor.h"
25
26/* client/include */
27#include "colors_g.h"
28
29/* client */
30#include "tilespec.h"
31
32#include "colors_common.h"
33
34 struct color_system {
35 struct rgbcolor **stdcolors;
36};
37
38/************************************************************************/
44 struct color_system *color_system_read(struct section_file *file)
45{
46 struct color_system *colors = fc_malloc(sizeof(*colors));
47 enum color_std stdcolor;
48
49 colors->stdcolors = fc_calloc(COLOR_LAST, sizeof(*colors->stdcolors));
50
51 for (stdcolor = color_std_begin(); stdcolor != color_std_end();
52 stdcolor = color_std_next(stdcolor)) {
53 struct rgbcolor *prgbcolor = NULL;
54
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);
61 }
62 }
63
64 return colors;
65}
66
67/************************************************************************/
70 void color_system_free(struct color_system *colors)
71{
72 enum color_std stdcolor;
73
74 for (stdcolor = color_std_begin(); stdcolor != color_std_end();
75 stdcolor = color_std_next(stdcolor)) {
76 rgbcolor_destroy(*(colors->stdcolors + stdcolor));
77 }
78
79 free(colors->stdcolors);
80
81 free(colors);
82}
83
84/************************************************************************/
87 struct color *ensure_color(struct rgbcolor *rgb)
88{
89 fc_assert_ret_val(rgb != NULL, NULL);
90
91 if (!rgb->color) {
92 rgb->color = color_alloc(rgb->r, rgb->g, rgb->b);
93 }
94 return rgb->color;
95}
96
97/************************************************************************/
100 struct color *get_color(const struct tileset *t, enum color_std stdcolor)
101{
102 struct color_system *colors = get_color_system(t);
103
104 fc_assert_ret_val(colors != NULL, NULL);
105
106 return ensure_color(*(colors->stdcolors + stdcolor));
107}
108
109/************************************************************************/
113 bool player_has_color(const struct player *pplayer)
114{
115 fc_assert_ret_val(pplayer != NULL, FALSE);
116
117 return pplayer->rgb != NULL;
118}
119
120/************************************************************************/
125 struct color *get_player_color(const struct tileset *t,
126 const struct player *pplayer)
127{
128 fc_assert_ret_val(pplayer != NULL, NULL);
129 fc_assert_ret_val(pplayer->rgb != NULL, NULL);
130
131 return ensure_color(pplayer->rgb);
132}
133
134/************************************************************************/
137 struct color *get_terrain_color(const struct tileset *t,
138 const struct terrain *pterrain)
139{
140 fc_assert_ret_val(pterrain != NULL, NULL);
141 fc_assert_ret_val(pterrain->rgb != NULL, NULL);
142
143 return ensure_color(pterrain->rgb);
144}
145
146/************************************************************************/
150 struct color *color_best_contrast(struct color *subject,
151 struct color **candidates, int ncandidates)
152{
153 int sbright = color_brightness_score(subject), bestdiff = 0;
154 int i;
155 struct color *best = NULL;
156
157 fc_assert_ret_val(candidates != NULL, NULL);
158 fc_assert_ret_val(ncandidates > 0, NULL);
159
160 for (i = 0; i < ncandidates; i++) {
161 int cbright = color_brightness_score(candidates[i]);
162 int diff = ABS(sbright - cbright);
163
164 if (best == NULL || diff > bestdiff) {
165 best = candidates[i];
166 bestdiff = diff;
167 }
168 }
169
170 return best;
171}
color_system_free
void color_system_free(struct color_system *colors)
Definition colors_common.c:70
color_best_contrast
struct color * color_best_contrast(struct color *subject, struct color **candidates, int ncandidates)
Definition colors_common.c:150
get_player_color
struct color * get_player_color(const struct tileset *t, const struct player *pplayer)
Definition colors_common.c:125
color_system_read
struct color_system * color_system_read(struct section_file *file)
Definition colors_common.c:44
ensure_color
struct color * ensure_color(struct rgbcolor *rgb)
Definition colors_common.c:87
player_has_color
bool player_has_color(const struct player *pplayer)
Definition colors_common.c:113
get_color
struct color * get_color(const struct tileset *t, enum color_std stdcolor)
Definition colors_common.c:100
get_terrain_color
struct color * get_terrain_color(const struct tileset *t, const struct terrain *pterrain)
Definition colors_common.c:137
colors_common.h
colors_g.h
color_alloc
struct color * color_alloc(int r, int g, int b)
Definition colors.c:38
color_brightness_score
int color_brightness_score(struct color *pcolor)
Definition colors.c:62
log.h
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
log_error
#define log_error(message,...)
Definition log.h:103
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
player.h
secfile_error
const char * secfile_error(void)
Definition section_file.c:36
rgbcolor_destroy
void rgbcolor_destroy(struct rgbcolor *prgbcolor)
Definition rgbcolor.c:74
rgbcolor_load
bool rgbcolor_load(struct section_file *file, struct rgbcolor **prgbcolor, char *path,...)
Definition rgbcolor.c:90
rgbcolor_new
struct rgbcolor * rgbcolor_new(int r, int g, int b)
Definition rgbcolor.c:34
rgbcolor.h
shared.h
ABS
#define ABS(x)
Definition shared.h:61
color_system
Definition colors_common.c:34
color_system::stdcolors
struct rgbcolor ** stdcolors
Definition colors_common.c:35
color
Definition colors.h:20
player
Definition player.h:249
player::rgb
struct rgbcolor * rgb
Definition player.h:308
rgbcolor
Definition rgbcolor.h:33
rgbcolor::g
int g
Definition rgbcolor.h:34
rgbcolor::color
struct color * color
Definition rgbcolor.h:35
rgbcolor::b
int b
Definition rgbcolor.h:34
rgbcolor::r
int r
Definition rgbcolor.h:34
section_file
Definition section_file.h:36
terrain
Definition terrain.h:176
terrain::rgb
struct rgbcolor * rgb
Definition terrain.h:247
tileset
Definition tilespec.c:459
FALSE
#define FALSE
Definition support.h:47
get_color_system
struct color_system * get_color_system(const struct tileset *t)
Definition tilespec.c:6760
tilespec.h