GRASS 8 Programmer's Manual 8.6.0dev(2026)-b0a6d7703c
Loading...
Searching...
No Matches
vector/Vlib/color_write.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/color_write.c
3
4 \brief Vector Library - write color table for vector map
5
6 SPDX-FileCopyrightText: 2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Martin Landa <landa.martin gmail.com>
10 */
11
12#include <string.h>
13
14#include <grass/gis.h>
15#include <grass/vector.h>
16#include <grass/raster.h>
17#include <grass/glocale.h>
18
19/*!
20 \brief Write color table for vector map
21
22 The color table is written for the vector map <i>name</i> in the
23 specified <i>mapset</i> from the <i>colors</i> structure.
24
25 The <i>colors</i> structure must be created properly, i.e.,
26 Rast_init_colors() to initialize the structure and
27 Rast_add_c_color_rule() to set the category colors. These routines
28 are called by higher level routines which read or create entire
29 color tables, such as Rast_read_colors() or Rast_make_ramp_colors().
30
31 <b>Note:</b> The calling sequence for this function deserves
32 special attention. The <i>mapset</i> parameter seems to imply that
33 it is possible to overwrite the color table for a vector map which
34 is in another mapset. However, this is not what actually
35 happens. It is very useful for users to create their own color
36 tables for vector maps in other mapsets, but without overwriting
37 other users' color tables for the same raster map. If <i>mapset</i>
38 is the current mapset, then the color file for <i>name</i> will be
39 overwritten by the new color table. But if <i>mapset</i> is not the
40 current mapset, then the color table is actually written in the
41 current mapset under the <tt>colr2</tt> element as:
42 <tt>vector/name/colr2</tt>.
43
44 The rules are written out using floating-point format, removing
45 trailing zeros (possibly producing integers). The flag marking the
46 colors as floating-point is <b>not</b> written.
47
48 If the environment variable FORCE_GRASS3_COLORS is set (to anything
49 at all) then the output format is 3.0, even if the structure
50 contains 4.0 rules. This allows users to create 3.0 color files for
51 export to sites which don't yet have 4.0
52
53 \param name vector map name
54 \param mapset mapset name
55 \param colors pointer to structure Colors which holds color info
56
57 \return void
58 */
59void Vect_write_colors(const char *name, const char *mapset,
60 struct Colors *colors)
61{
62 char element[GPATH_MAX];
63 const char *cname;
65 FILE *fd;
66
68 if (strcmp(xmapset, mapset) != 0)
69 G_fatal_error(_("Qualified name <%s> doesn't match mapset <%s>"),
70 name, mapset);
71 name = xname;
72 mapset = xmapset;
73 }
74
75 /*
76 if mapset is current mapset, write original color table
77 else write secondary color table
78 */
79 if (strcmp(mapset, G_mapset()) == 0) {
81 snprintf(element, sizeof(element), "%s/%s", GV_DIRECTORY, name);
82 }
83 else {
84 cname = name;
85 snprintf(element, sizeof(element), "%s/%s", GV_COLR2_DIRECTORY, mapset);
86 }
87
88 if (!(fd = G_fopen_new(element, cname)))
89 G_fatal_error(_("Unable to create <%s> file for map <%s>"), element,
90 name);
91
92 Rast__write_colors(fd, colors);
93 fclose(fd);
94}
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
Definition nme_in_mps.c:34
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
Definition gis/open.c:218
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
void Rast__write_colors(FILE *, struct Colors *)
Write map layer color table.
#define GV_COLR2_DIRECTORY
Name of directory for alternative color tables.
Definition dig_defines.h:30
#define GV_DIRECTORY
Name of vector directory.
Definition dig_defines.h:8
#define GV_COLR_ELEMENT
Color table.
Definition dig_defines.h:28
#define GMAPSET_MAX
Definition gis.h:194
#define GPATH_MAX
Definition gis.h:196
#define GNAME_MAX
Definition gis.h:193
#define _(str)
Definition glocale.h:10
const char * name
Definition named_colr.c:6
Definition gis.h:689
void Vect_write_colors(const char *name, const char *mapset, struct Colors *colors)
Write color table for vector map.