MCPcopy Create free account
hub / github.com/git/git / parse_color

Function parse_color

color.c:133–192  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

131}
132
133static int parse_color(struct color *out, const char *name, int len)
134{
135 char *end;
136 long val;
137
138 /* First try the special word "normal"... */
139 if (match_word(name, len, "normal")) {
140 out->type = COLOR_NORMAL;
141 return 0;
142 }
143
144 /* Try a 24- or 12-bit RGB value prefixed with '#' */
145 if ((len == 7 || len == 4) && name[0] == '#') {
146 int width_per_color = (len == 7) ? 2 : 1;
147 const char *color = name + 1;
148
149 if (!get_hex_color(&color, width_per_color, &out->red) &&
150 !get_hex_color(&color, width_per_color, &out->green) &&
151 !get_hex_color(&color, width_per_color, &out->blue)) {
152 out->type = COLOR_RGB;
153 return 0;
154 }
155 }
156
157 /* Then pick from our human-readable color names... */
158 if (parse_ansi_color(out, name, len) == 0) {
159 return 0;
160 }
161
162 /* And finally try a literal 256-color-mode number */
163 val = strtol(name, &end, 10);
164 if (end - name == len) {
165 /*
166 * Allow "-1" as an alias for "normal", but other negative
167 * numbers are bogus.
168 */
169 if (val < -1)
170 ; /* fall through to error */
171 else if (val < 0) {
172 out->type = COLOR_NORMAL;
173 return 0;
174 /* Rewrite 0-7 as more-portable standard colors. */
175 } else if (val < 8) {
176 out->type = COLOR_ANSI;
177 out->value = val + COLOR_FOREGROUND_ANSI;
178 return 0;
179 /* Rewrite 8-15 as more-portable aixterm colors. */
180 } else if (val < 16) {
181 out->type = COLOR_ANSI;
182 out->value = val - 8 + COLOR_FOREGROUND_BRIGHT_ANSI;
183 return 0;
184 } else if (val < 256) {
185 out->type = COLOR_256;
186 out->value = val;
187 return 0;
188 }
189 }
190

Callers 1

color_parse_mem_1Function · 0.70

Calls 3

match_wordFunction · 0.85
get_hex_colorFunction · 0.85
parse_ansi_colorFunction · 0.85

Tested by

no test coverage detected