GRASS 8 Programmer's Manual 8.6.0dev(2026)-8843f13794
Loading...
Searching...
No Matches
vector/Vlib/color_read.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/color_read.c
3
4 \brief Vector Library - read color table of 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/raster.h>
16#include <grass/vector.h>
17#include <grass/glocale.h>
18
19/*!
20 \brief Read color table of vector map
21
22 The color table for the vector map <i>name</i> in the specified
23 <i>mapset</i> is read into the <i>colors</i> structure.
24
25 Note: If a secondary color file for map name <i>name</i> exists in
26 the current mapset, that color file is read. This allows the user to
27 define their own color lookup tables for vector maps found in other
28 mapsets.
29
30 Warning message is printed if the color file is missing or invalid.
31
32 \param name vector map name
33 \param mapset mapset name ("" for search path)
34 \param[out] colors pointer to Colors structure (can be NULL)
35
36 \return -1 on error
37 \return 0 if color table missing
38 \return 1 on success (color table found)
39 */
40int Vect_read_colors(const char *name, const char *mapset,
41 struct Colors *colors)
42{
43 int ret;
44 char buf[GPATH_MAX];
45 char xname[GNAME_MAX];
46
47 if (colors)
48 Rast_init_colors(colors);
49
50 if (G_strlcpy(xname, name, sizeof(xname)) >= sizeof(xname)) {
51 G_warning(_("Vector map name <%s> is too long"), name);
52 return -1;
53 }
54 mapset = G_find_vector(xname, mapset);
55 if (!mapset)
56 return -1;
57
58 name = xname;
59
60 /* look for secondary color table in current mapset */
61 (void)snprintf(buf, sizeof(buf), "%s/%s", GV_COLR2_DIRECTORY, mapset);
62 if (Rast__read_colors(buf, name, G_mapset(), colors) >= 0)
63 return 1;
64
65 /* look for the regular color table */
66 (void)snprintf(buf, sizeof(buf), "%s/%s", GV_DIRECTORY, name);
67 ret = Rast__read_colors(buf, GV_COLR_ELEMENT, mapset, colors);
68 if (ret >= 0)
69 return 1;
70
71 if (ret == -2)
72 return 0;
73
74 return ret;
75}
void G_warning(const char *,...) __attribute__((format(printf
const char * G_find_vector(char *, const char *)
Finds a vector map.
Definition find_vect.c:39
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
size_t G_strlcpy(char *, const char *, size_t)
Safe string copy function.
Definition strlcpy.c:54
int Rast__read_colors(const char *, const char *, const char *, struct Colors *)
void Rast_init_colors(struct Colors *)
Initialize color structure.
Definition color_init.c:23
#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 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
int Vect_read_colors(const char *name, const char *mapset, struct Colors *colors)
Read color table of vector map.