Freeciv-3.1: /fast/freeciv/nightly/S3_1/common/rgbcolor.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
rgbcolor.c
Go to the documentation of this file.
1/****************************************************************************
2 Freeciv - Copyright (C) 2010 - The Freeciv Team
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#include <stdarg.h>
19
20/* utility */
21#include "fcintl.h"
22#include "log.h"
23#include "registry.h"
24
25/* common */
26#include "fc_interface.h"
27#include "game.h"
28
29#include "rgbcolor.h"
30
31/************************************************************************/
34 struct rgbcolor *rgbcolor_new(int r, int g, int b)
35{
36 struct rgbcolor *prgbcolor;
37
38 prgbcolor = fc_calloc(1, sizeof(*prgbcolor));
39 prgbcolor->r = r;
40 prgbcolor->g = g;
41 prgbcolor->b = b;
42 prgbcolor->color = NULL;
43
44 return prgbcolor;
45}
46
47/************************************************************************/
51 struct rgbcolor *rgbcolor_copy(const struct rgbcolor *prgbcolor)
52{
53 fc_assert_ret_val(prgbcolor != NULL, NULL);
54
55 return rgbcolor_new(prgbcolor->r, prgbcolor->g, prgbcolor->b);
56}
57
58/************************************************************************/
62 bool rgbcolors_are_equal(const struct rgbcolor *c1, const struct rgbcolor *c2)
63{
64 fc_assert_ret_val(c1 != NULL && c2 != NULL, FALSE);
65
66 /* No check of cached 'color' member -- if values are equal, it should be
67 * equivalent */
68 return (c1->r == c2->r && c1->g == c2->g && c1->b == c2->b);
69}
70
71/************************************************************************/
74 void rgbcolor_destroy(struct rgbcolor *prgbcolor)
75{
76 if (!prgbcolor) {
77 return;
78 }
79
80 if (prgbcolor->color) {
81 fc_funcs->gui_color_free(prgbcolor->color);
82 }
83 free(prgbcolor);
84}
85
86/************************************************************************/
90 bool rgbcolor_load(struct section_file *file, struct rgbcolor **prgbcolor,
91 char *path, ...)
92{
93 int r, g, b;
94 char colorpath[256];
95 va_list args;
96
97 fc_assert_ret_val(file != NULL, FALSE);
98 fc_assert_ret_val(*prgbcolor == NULL, FALSE);
99
100 va_start(args, path);
101 fc_vsnprintf(colorpath, sizeof(colorpath), path, args);
102 va_end(args);
103
104 if (!secfile_lookup_int(file, &r, "%s.r", colorpath)
105 || !secfile_lookup_int(file, &g, "%s.g", colorpath)
106 || !secfile_lookup_int(file, &b, "%s.b", colorpath)) {
107 /* One color value (red, green or blue) is missing. */
108 return FALSE;
109 }
110
111 rgbcolor_check(colorpath, r, g, b);
112 *prgbcolor = rgbcolor_new(r, g, b);
113
114 return TRUE;
115}
116
117/************************************************************************/
121 void rgbcolor_save(struct section_file *file,
122 const struct rgbcolor *prgbcolor, char *path, ...)
123{
124 char colorpath[256];
125 va_list args;
126
127 fc_assert_ret(file != NULL);
128 fc_assert_ret(prgbcolor != NULL);
129
130 va_start(args, path);
131 fc_vsnprintf(colorpath, sizeof(colorpath), path, args);
132 va_end(args);
133
134 secfile_insert_int(file, prgbcolor->r, "%s.r", colorpath);
135 secfile_insert_int(file, prgbcolor->g, "%s.g", colorpath);
136 secfile_insert_int(file, prgbcolor->b, "%s.b", colorpath);
137}
138
139/************************************************************************/
142 bool rgbcolor_to_hex(const struct rgbcolor *prgbcolor, char *hex,
143 size_t hex_len)
144{
145 fc_assert_ret_val(prgbcolor != NULL, FALSE);
146 /* Needs a length greater than 7 ('#' + 6 hex digites and '\0'). */
147 fc_assert_ret_val(hex_len > 7, FALSE);
148
149 fc_assert_ret_val(0 <= prgbcolor->r && prgbcolor->r <= 255, FALSE);
150 fc_assert_ret_val(0 <= prgbcolor->g && prgbcolor->g <= 255, FALSE);
151 fc_assert_ret_val(0 <= prgbcolor->b && prgbcolor->b <= 255, FALSE);
152
153 fc_snprintf(hex, hex_len, "#%06x",
154 (prgbcolor->r * 256 + prgbcolor->g) * 256 + prgbcolor->b);
155
156 return TRUE;
157}
158
159/************************************************************************/
162 bool rgbcolor_from_hex(struct rgbcolor **prgbcolor, const char *hex)
163{
164 int rgb, r, g, b;
165 char hex2[16];
166
167 fc_assert_ret_val(*prgbcolor == NULL, FALSE);
168 fc_assert_ret_val(hex != NULL, FALSE);
169
170 if (hex[0] == '#') {
171 hex++;
172 }
173
174 if (strlen(hex) != 6) {
175 return FALSE;
176 }
177
178 fc_snprintf(hex2, sizeof(hex2), "0x%s", hex);
179 if (!sscanf(hex2, "%x", &rgb)) {
180 return FALSE;
181 }
182
183 r = rgb / 256 / 256;
184 g = (rgb - r * 256 * 256) / 256;
185 b = rgb % 256;
186
187 *prgbcolor = rgbcolor_new(r, g, b);
188
189 return TRUE;
190}
191
192/************************************************************************/
196 int rgbcolor_brightness_score(struct rgbcolor *prgbcolor)
197{
198 /* This simple scoring system taken from W3C "Techniques For Accessibility
199 * Evaluation And Repair Tools", https://www.w3.org/TR/AERT#color-contrast
200 *
201 * "Color brightness is determined by the following formula:
202 * ((Red value X 299) + (Green value X 587) + (Blue value X 114)) / 1000
203 * Note: This algorithm is taken from a formula for converting RGB values to
204 * YIQ [NTSC] values [specifically the Y component]. This brightness value
205 * gives a perceived brightness for a color." */
206 return (prgbcolor->r*299 + prgbcolor->g*587 + prgbcolor->b*114) / 1000;
207}
fc_funcs
const struct functions * fc_funcs
Definition fc_interface.c:38
fc_interface.h
fcintl.h
game.h
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
registry.h
secfile_lookup_int
bool secfile_lookup_int(const struct section_file *secfile, int *ival, const char *path,...)
Definition registry_ini.c:1912
secfile_insert_int
#define secfile_insert_int(secfile, value, path,...)
Definition registry_ini.h:116
rgbcolor_from_hex
bool rgbcolor_from_hex(struct rgbcolor **prgbcolor, const char *hex)
Definition rgbcolor.c:162
rgbcolor_destroy
void rgbcolor_destroy(struct rgbcolor *prgbcolor)
Definition rgbcolor.c:74
rgbcolors_are_equal
bool rgbcolors_are_equal(const struct rgbcolor *c1, const struct rgbcolor *c2)
Definition rgbcolor.c:62
rgbcolor_load
bool rgbcolor_load(struct section_file *file, struct rgbcolor **prgbcolor, char *path,...)
Definition rgbcolor.c:90
rgbcolor_copy
struct rgbcolor * rgbcolor_copy(const struct rgbcolor *prgbcolor)
Definition rgbcolor.c:51
rgbcolor_to_hex
bool rgbcolor_to_hex(const struct rgbcolor *prgbcolor, char *hex, size_t hex_len)
Definition rgbcolor.c:142
rgbcolor_new
struct rgbcolor * rgbcolor_new(int r, int g, int b)
Definition rgbcolor.c:34
rgbcolor_brightness_score
int rgbcolor_brightness_score(struct rgbcolor *prgbcolor)
Definition rgbcolor.c:196
rgbcolor_save
void rgbcolor_save(struct section_file *file, const struct rgbcolor *prgbcolor, char *path,...)
Definition rgbcolor.c:121
rgbcolor.h
rgbcolor_check
#define rgbcolor_check(_str, _r, _g, _b)
Definition rgbcolor.h:59
functions::gui_color_free
void(* gui_color_free)(struct color *pcolor)
Definition fc_interface.h:54
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
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
fc_vsnprintf
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:896
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47