File: colors_common.c

package info (click to toggle)
freeciv 2.6.2-1~bpo10+1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 212,500 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (171 lines) | stat: -rw-r--r-- 5,617 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/********************************************************************** 
 Freeciv - Copyright (C) 2005 - The Freeciv Project
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/

#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif

/* utility */
#include "log.h"
#include "shared.h"

/* common */
#include "player.h"
#include "rgbcolor.h"

/* client/include */
#include "colors_g.h"

/* client */
#include "tilespec.h"

#include "colors_common.h"

struct color_system {
  struct rgbcolor **stdcolors;
};

/****************************************************************************
  Called when the client first starts to allocate the default colors.

  Currently this must be called in ui_main, generally after UI
  initialization.
****************************************************************************/
struct color_system *color_system_read(struct section_file *file)
{
  struct color_system *colors = fc_malloc(sizeof(*colors));
  enum color_std stdcolor;

  colors->stdcolors = fc_calloc(COLOR_LAST, sizeof(*colors->stdcolors));

  for (stdcolor= color_std_begin(); stdcolor!= color_std_end();
       stdcolor= color_std_next(stdcolor)) {
    struct rgbcolor *prgbcolor = NULL;
    if (rgbcolor_load(file, &prgbcolor, "colors.%s0",
                      color_std_name(stdcolor))) {
      *(colors->stdcolors + stdcolor) = prgbcolor;
    } else {
      log_error("Color %s: %s", color_std_name(stdcolor), secfile_error());
      *(colors->stdcolors + stdcolor) = rgbcolor_new(0, 0, 0);
    }
  }

  return colors;
}

/****************************************************************************
  Called when the client first starts to free any allocated colors.
****************************************************************************/
void color_system_free(struct color_system *colors)
{
  enum color_std stdcolor;

  for (stdcolor= color_std_begin(); stdcolor!= color_std_end();
       stdcolor= color_std_next(stdcolor)) {
    rgbcolor_destroy(*(colors->stdcolors + stdcolor));
  }

  free(colors->stdcolors);

  free(colors);
}

/****************************************************************************
  Return the RGB color, allocating it if necessary.
****************************************************************************/
struct color *ensure_color(struct rgbcolor *rgb)
{
  fc_assert_ret_val(rgb != NULL, NULL);

  if (!rgb->color) {
    rgb->color = color_alloc(rgb->r, rgb->g, rgb->b);
  }
  return rgb->color;
}

/****************************************************************************
  Return a pointer to the given "standard" color.
****************************************************************************/
struct color *get_color(const struct tileset *t, enum color_std stdcolor)
{
  struct color_system *colors = get_color_system(t);

  fc_assert_ret_val(colors != NULL, NULL);

  return ensure_color(*(colors->stdcolors + stdcolor));
}

/**********************************************************************
  Return whether the player has a color assigned yet.
  Should only be FALSE in pregame.
***********************************************************************/
bool player_has_color(const struct tileset *t,
                      const struct player *pplayer)
{
  fc_assert_ret_val(pplayer != NULL, NULL);

  return pplayer->rgb != NULL;
}

/**********************************************************************
  Return the color of the player.
  In pregame, callers should check player_has_color() before calling
  this.
***********************************************************************/
struct color *get_player_color(const struct tileset *t,
                               const struct player *pplayer)
{
  fc_assert_ret_val(pplayer != NULL, NULL);
  fc_assert_ret_val(pplayer->rgb != NULL, NULL);

  return ensure_color(pplayer->rgb);
}

/****************************************************************************
  Return a pointer to the given "terrain" color.
****************************************************************************/
struct color *get_terrain_color(const struct tileset *t,
                                const struct terrain *pterrain)
{
  fc_assert_ret_val(pterrain != NULL, NULL);
  fc_assert_ret_val(pterrain->rgb != NULL, NULL);

  return ensure_color(pterrain->rgb);
}

/****************************************************************************
  Find the colour from 'candidates' with the best perceptual contrast from
  'subject'.
****************************************************************************/
struct color *color_best_contrast(struct color *subject,
                                  struct color **candidates, int ncandidates)
{
  int sbright = color_brightness_score(subject), bestdiff = 0;
  int i;
  struct color *best = NULL;

  fc_assert_ret_val(candidates != NULL, NULL);
  fc_assert_ret_val(ncandidates > 0, NULL);

  for (i = 0; i < ncandidates; i++) {
    int cbright = color_brightness_score(candidates[i]);
    int diff = ABS(sbright - cbright);

    if (best == NULL || diff > bestdiff) {
      best = candidates[i];
      bestdiff = diff;
    }
  }

  return best;
}