1 /* gEDA - GPL Electronic Design Automation
2 * libgeda - gEDA's library
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2020 gEDA Contributors (see ChangeLog for details)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "libgeda_priv.h"
30 COLOR print_colors
[MAX_COLORS
];
32 #define NOCOLOR {0xff, 0xff, 0xff, 0xff, FALSE}
33 #define WHITE {0xff, 0xff, 0xff, 0xff, TRUE}
34 #define GRAY {0x88, 0x88, 0x88, 0xff, TRUE}
35 #define BLACK {0x00, 0x00, 0x00, 0xff, TRUE}
36 #define ENDMAP {0x00, 0x00, 0x00, 0x00, FALSE}
38 static COLOR default_colors
[] = {
39 WHITE
, /* 0: background */
41 BLACK
, /* 2: net-endpoint */
42 BLACK
, /* 3: graphic */
44 BLACK
, /* 5: attribute */
45 BLACK
, /* 6: logic-bubble */
46 BLACK
, /* 7: dots-grid */
47 BLACK
, /* 8: detached-attribute */
50 GRAY
, /* 11: select */
51 GRAY
, /* 12: bounding-box */
52 GRAY
, /* 13: zoom-box */
53 GRAY
, /* 14: stroke */
55 NOCOLOR
, /* 16: output-background */
56 NOCOLOR
, /* 17: freestyle1 */
57 NOCOLOR
, /* 18: freestyle2 */
58 NOCOLOR
, /* 19: freestyle3 */
59 NOCOLOR
, /* 20: freestyle4 */
60 BLACK
, /* 21: junction */
61 GRAY
, /* 22: mesh-grid-major */
62 NOCOLOR
, /* 23: mesh-grid-minor */
63 BLACK
, /* 24: origin */
64 BLACK
, /* 25: place-origin */
68 /*! \brief Initialises the color subsystem
69 * \par Function Description
70 * At the moment, just initialises the print color map.
75 s_color_map_defaults (print_colors
);
78 /*! \brief Initialise a color map to B&W
79 * \par Function Description
80 * Initialises a color map to a simple default: black features on a
81 * white background, with "special" colors as gray.
83 * \warning \a map must be have length of at least #MAX_COLORS.
85 * \param map Color map to initialise.
88 s_color_map_defaults (COLOR
*map
)
91 gboolean reached_end
= FALSE
;
93 for (i
= 0; i
< MAX_COLORS
; i
++) {
95 map
[i
].enabled
= FALSE
;
98 c
= default_colors
[i
];
99 if (c
.a
== 0) { /* Check for end of default map */
108 /* \brief Decode a hexadecimal RGB or RGBA color code.
109 * \par Function Description
110 * Accepts a hexadecimal color code \a rgba of either the form #RRGGBB
111 * or #RRGGBBAA, and parses it to extract the numerical color values,
112 * placing them in the the #guchar pointers passed as arguments. If
113 * the six-digit form is used, the alpha channel is set to full
114 * opacity. If an error occurs during parsing, the return values are
115 * set to solid white.
117 * Note that this function implements similar functionality to
118 * gdk_color_parse(). However, for consistency, <em>only</em> this
119 * function should be used to parse color strings from gEDA
120 * configuration files, as gdk_color_parse() does not support the
123 * \todo Use GError mechanism to give more specific error messages.
125 * \param [in] rgba Colour code to parse.
126 * \param [out] r Location to store red value.
127 * \param [out] g Location to store green value.
128 * \param [out] b Location to store blue value.
130 * \returns #TRUE on success, #FALSE on failure.
133 s_color_rgba_decode (const gchar
*rgba
,
134 guint8
*r
, guint8
*g
, guint8
*b
, guint8
*a
)
136 gint len
, i
, ri
, gi
, bi
, ai
;
139 /* Default to solid white */
140 *r
= 0xff; *g
= 0xff; *b
= 0xff; *a
= 0xff;
142 /* Check that the string is a valid length and starts with a '#' */
144 if ((len
!= 9 && len
!= 7) || rgba
[0] != '#')
147 /* Check we only have [0-9a-fA-F] */
148 for (i
= 1; i
< len
; i
++) {
150 if ((c
< '0' || c
> '9')
151 && (c
< 'a' || c
> 'f')
152 && (c
< 'A' || c
> 'F'))
156 /* Use sscanf to extract values */
157 c
= sscanf (rgba
+ 1, "%2x%2x%2x", &ri
, &gi
, &bi
);
160 *r
= (guint8
) ri
; *g
= (guint8
) gi
; *b
= (guint8
) bi
;
163 c
= sscanf (rgba
+ 7, "%2x", &ai
);
172 /* \brief Encode a hexadecimal RGB or RGBA color code.
173 * \par Function Description
174 * Encodes four colour components into either the form #RRGGBB or
175 * #RRGGBBAA. The shorter form is used when the alpha component is
178 * \param [in] r Red component.
179 * \param [in] g Green component.
180 * \param [in] b Blue component.
181 * \returns A newly allocated string containing the encoded string.
184 s_color_rgba_encode (guint8 r
, guint8 g
, guint8 b
, guint8 a
)
187 return g_strdup_printf("#%02x%02x%02x%02x",
188 (gint
) r
, (gint
) g
, (gint
) b
, (gint
) a
);
190 return g_strdup_printf("#%02x%02x%02x",
191 (gint
) r
, (gint
) g
, (gint
) b
);
195 s_color_map_to_scm (const COLOR
*map
)
197 SCM result
= SCM_EOL
;
199 for (i
= MAX_COLORS
- 1; i
>= 0; i
--) {
200 SCM color_val
= SCM_BOOL_F
;
201 if (map
[i
].enabled
) {
203 gchar
*rgba
= s_color_rgba_encode (c
.r
, c
.g
, c
.b
, c
.a
);
204 color_val
= scm_from_utf8_string (rgba
);
207 result
= scm_cons (scm_list_2 (scm_from_int (i
), color_val
), result
);
213 * \warning This function should ONLY be called from Scheme procedures.
216 s_color_map_from_scm (COLOR
*map
, SCM lst
, const char *scheme_proc_name
)
219 SCM wrong_type_arg_sym
= scm_from_utf8_symbol ("wrong-type-arg");
220 SCM proc_name
= scm_from_utf8_string (scheme_proc_name
);
221 while (!scm_is_null (curr
)) {
225 COLOR c
= {0x00, 0x00, 0x00, FALSE
};
227 SCM entry
= scm_car (curr
);
229 /* Check map entry has correct type */
230 if (!scm_is_true (scm_list_p (entry
))
231 || (scm_to_int (scm_length (entry
)) != 2)) {
232 scm_error_scm (wrong_type_arg_sym
, proc_name
,
233 scm_from_utf8_string (_("Color map entry must be a two-element list")),
234 SCM_EOL
, scm_list_1 (entry
));
237 /* Check color index has correct type, and extract it */
239 if (!scm_is_integer (s
)) {
240 scm_error_scm (wrong_type_arg_sym
, proc_name
,
241 scm_from_utf8_string (_("Index in color map entry must be an integer")),
242 SCM_EOL
, scm_list_1 (s
));
246 /* Check color index is within bounds. If it's out of bounds, it's
247 * legal, but warn & ignore it.
249 * FIXME one day we will have dynamically-expanding colorspace.
251 if ((i
< 0) || (i
>= MAX_COLORS
)) {
252 g_critical ("Color map index out of bounds: %i\n", i
);
256 /* If color value is #F, disable color */
257 s
= scm_cadr (entry
);
258 if (scm_is_false (s
)) {
259 map
[i
].enabled
= FALSE
;
263 /* Otherwise, we require a string */
264 s
= scm_cadr (entry
);
265 if (!scm_is_string (s
)) {
266 scm_error_scm (wrong_type_arg_sym
, proc_name
,
267 scm_from_utf8_string (_("Value in color map entry must be #f or a string")),
268 SCM_EOL
, scm_list_1 (s
));
270 rgba
= scm_to_utf8_string (s
);
272 result
= s_color_rgba_decode (rgba
, &c
.r
, &c
.g
, &c
.b
, &c
.a
);
274 /* FIXME should we generate a Guile error if there's a problem here? */
276 g_critical ("Invalid color map value: %s\n", rgba
);
279 map
[i
].enabled
= TRUE
;
283 /* Go to next element in map */
284 curr
= scm_cdr (curr
);
286 scm_remember_upto_here_2 (wrong_type_arg_sym
, proc_name
);