Freeciv-3.2
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/************************************************************************/
34struct 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/************************************************************************/
52{
54
55 return rgbcolor_new(prgbcolor->r, prgbcolor->g, prgbcolor->b);
56}
57
58/************************************************************************/
62bool rgbcolors_are_equal(const struct rgbcolor *c1, const struct rgbcolor *c2)
63{
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/************************************************************************/
75{
76 if (!prgbcolor) {
77 return;
78 }
79
80 if (prgbcolor->color) {
82 }
84}
85
86/************************************************************************/
90bool 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
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
112 *prgbcolor = rgbcolor_new(r, g, b);
113
114 return TRUE;
115}
116
117/************************************************************************/
121void 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);
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/************************************************************************/
142bool rgbcolor_to_hex(const struct rgbcolor *prgbcolor, char *hex,
143 size_t hex_len)
144{
146 /* Needs a length greater than 7 ('#' + 6 hex digites and '\0'). */
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/************************************************************************/
162bool rgbcolor_from_hex(struct rgbcolor **prgbcolor, const char *hex)
163{
164 int rgb, r, g, b;
165 char hex2[16];
166
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/************************************************************************/
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}
char * incite_cost
Definition comments.c:74
const struct functions * fc_funcs
#define fc_assert_ret(condition)
Definition log.h:191
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define fc_calloc(n, esz)
Definition mem.h:38
bool secfile_lookup_int(const struct section_file *secfile, int *ival, const char *path,...)
#define secfile_insert_int(secfile, value, path,...)
bool rgbcolor_from_hex(struct rgbcolor **prgbcolor, const char *hex)
Definition rgbcolor.c:162
void rgbcolor_destroy(struct rgbcolor *prgbcolor)
Definition rgbcolor.c:74
bool rgbcolors_are_equal(const struct rgbcolor *c1, const struct rgbcolor *c2)
Definition rgbcolor.c:62
bool rgbcolor_load(struct section_file *file, struct rgbcolor **prgbcolor, char *path,...)
Definition rgbcolor.c:90
struct rgbcolor * rgbcolor_copy(const struct rgbcolor *prgbcolor)
Definition rgbcolor.c:51
bool rgbcolor_to_hex(const struct rgbcolor *prgbcolor, char *hex, size_t hex_len)
Definition rgbcolor.c:142
struct rgbcolor * rgbcolor_new(int r, int g, int b)
Definition rgbcolor.c:34
int rgbcolor_brightness_score(struct rgbcolor *prgbcolor)
Definition rgbcolor.c:196
void rgbcolor_save(struct section_file *file, const struct rgbcolor *prgbcolor, char *path,...)
Definition rgbcolor.c:121
#define rgbcolor_check(_str, _r, _g, _b)
Definition rgbcolor.h:59
void(* gui_color_free)(struct color *pcolor)
int g
Definition rgbcolor.h:34
int b
Definition rgbcolor.h:34
int r
Definition rgbcolor.h:34
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:974
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:900
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47