Freeciv21
Develop your civilization from humble roots to a global empire
rgbcolor.h
Go to the documentation of this file.
1 /**************************************************************************
2  Copyright (c) 1996-2021 Freeciv21 and Freeciv contributors. This file is
3  part of Freeciv21. Freeciv21 is free software: you can
4  ^oo^ redistribute it and/or modify it under the terms of the GNU
5  (..) General Public License as published by the Free Software
6  () () Foundation, either version 3 of the License, or (at your
7  ()__() option) any later version. You should have received
8  a copy of the GNU General Public License along with Freeciv21. If not,
9  see https://www.gnu.org/licenses/.
10 **************************************************************************/
11 #pragma once
12 
13 // utility
14 #include "shared.h"
15 
16 struct section_file;
17 
18 /* Used for the color system in the client and the definition of the terrain
19  * colors used in the overview/map images. The values are read from the
20  * rulesets. */
21 struct color;
22 
23 /* An RGBcolor contains the R,G,B bitvalues for a color. The color itself
24  * holds the color structure for this color but may be nullptr (it's
25  * allocated on demand at runtime). */
26 struct rgbcolor {
27  int r, g, b;
28 };
29 
30 // get 'struct color_list' and related functions:
31 #define SPECLIST_TAG rgbcolor
32 #define SPECLIST_TYPE struct rgbcolor
33 #include "speclist.h"
34 
35 #define rgbcolor_list_iterate(rgbcolorlist, prgbcolor) \
36  TYPED_LIST_ITERATE(struct rgbcolor, rgbcolorlist, prgbcolor)
37 #define rgbcolor_list_iterate_end LIST_ITERATE_END
38 
39 /* Check the RGB color values. If a value is not in the interval [0, 255]
40  * clip it to the interval boundaries. */
41 #define CHECK_RGBCOLOR(_str, _c, _colorname) \
42  { \
43  int _color_save = _c; \
44  \
45  _c = CLIP(0, _c, 255); \
46  if (_c != _color_save) { \
47  qCritical("Invalid value for '%s' in color definition '%s' (%d). " \
48  "Setting it to '%d'.", \
49  _colorname, _str, _color_save, _c); \
50  } \
51  }
52 #define rgbcolor_check(_str, _r, _g, _b) \
53  { \
54  CHECK_RGBCOLOR(_str, _r, "red"); \
55  CHECK_RGBCOLOR(_str, _g, "green"); \
56  CHECK_RGBCOLOR(_str, _b, "blue"); \
57  }
58 
59 struct rgbcolor *rgbcolor_new(int r, int g, int b);
60 struct rgbcolor *rgbcolor_copy(const struct rgbcolor *prgbcolor);
61 bool rgbcolors_are_equal(const struct rgbcolor *c1,
62  const struct rgbcolor *c2);
63 void rgbcolor_destroy(struct rgbcolor *prgbcolor);
64 
65 bool rgbcolor_load(struct section_file *file, struct rgbcolor **prgbcolor,
66  const char *path, ...)
67  fc__attribute((__format__(__printf__, 3, 4)));
68 void rgbcolor_save(struct section_file *file,
69  const struct rgbcolor *prgbcolor, const char *path, ...)
70  fc__attribute((__format__(__printf__, 3, 4)));
71 
72 bool rgbcolor_to_hex(const struct rgbcolor *prgbcolor, char *hex,
73  size_t hex_len);
74 bool rgbcolor_from_hex(struct rgbcolor **prgbcolor, const char *hex);
75 
76 int rgbcolor_brightness_score(struct rgbcolor *prgbcolor);
bool rgbcolor_from_hex(struct rgbcolor **prgbcolor, const char *hex)
Convert a hex string into a rgb color.
Definition: rgbcolor.cpp:151
void rgbcolor_destroy(struct rgbcolor *prgbcolor)
Free rgbcolor structure.
Definition: rgbcolor.cpp:65
bool rgbcolors_are_equal(const struct rgbcolor *c1, const struct rgbcolor *c2)
Test whether two rgbcolor structures represent the exact same color value.
Definition: rgbcolor.cpp:52
struct rgbcolor * rgbcolor_new(int r, int g, int b)
Allocate new rgbcolor structure.
Definition: rgbcolor.cpp:25
bool rgbcolor_load(struct section_file *file, struct rgbcolor **prgbcolor, const char *path,...) fc__attribute((__format__(__printf__
bool void bool rgbcolor_to_hex(const struct rgbcolor *prgbcolor, char *hex, size_t hex_len)
Convert a rgb color to a hex string (like 0xff0000 for red [255, 0, 0]).
Definition: rgbcolor.cpp:130
bool void rgbcolor_save(struct section_file *file, const struct rgbcolor *prgbcolor, const char *path,...) fc__attribute((__format__(__printf__
int rgbcolor_brightness_score(struct rgbcolor *prgbcolor)
Return a number indicating the perceptual brightness of this color relative to others (larger is brig...
Definition: rgbcolor.cpp:185
struct rgbcolor * rgbcolor_copy(const struct rgbcolor *prgbcolor)
Allocate new rgbcolor structure and make it copy of one given as input.
Definition: rgbcolor.cpp:41
int g
Definition: rgbcolor.h:27
int b
Definition: rgbcolor.h:27
int r
Definition: rgbcolor.h:27
int fc__attribute((nonnull(1, 3)))