MCPcopy Create free account
hub / github.com/CalcProgrammer1/OpenRGB / ParseColors

Function ParseColors

cli.cpp:234–313  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

232};
233
234bool ParseColors(std::string colors_string, DeviceOptions *options)
235{
236 while (colors_string.length() > 0)
237 {
238 size_t rgb_end = colors_string.find_first_of(',');
239 std::string color = colors_string.substr(0, rgb_end);
240 int32_t rgb = 0;
241
242 bool parsed = false;
243
244 if (color.length() <= 0)
245 break;
246
247 /*-----------------------------------------------------------------*\
248 | This will set correct colour mode for modes with a |
249 | MODE_COLORS_RANDOM else generate a random colour from the |
250 | human_colors list above |
251 \*-----------------------------------------------------------------*/
252 if (color == "random")
253 {
254 options->random_colors = true;
255 srand((unsigned int)time(NULL));
256 int index = rand() % (sizeof(human_colors) / sizeof(human_colors[0])) + 1; //Anything other than black
257 rgb = human_colors[index].rgb;
258 parsed = true;
259 }
260 else
261 {
262 /* swy: (A) try interpreting it as text; as human keywords, otherwise strtoul() will pick up 'darkgreen' as 0xDA */
263 for (const struct HumanColors *hc = human_colors; hc->keyword != NULL; hc++)
264 {
265 if (strcasecmp(hc->keyword, color.c_str()) != 0)
266 continue;
267
268 rgb = hc->rgb; parsed = true;
269
270 break;
271 }
272 }
273
274 /* swy: (B) no luck, try interpreting it as an hexadecimal number instead */
275 if (!parsed)
276 {
277 if (color.length() == 6)
278 {
279 const char *colorptr = color.c_str(); char *endptr = NULL;
280
281 rgb = strtoul(colorptr, &endptr, 16);
282
283 /* swy: check that strtoul() has advanced the read pointer until the end (NULL terminator);
284 that means it has read the whole thing */
285 if (colorptr != endptr && endptr && *endptr == '\0')
286 parsed = true;
287 }
288 }
289
290 /* swy: we got it, save the 32-bit integer as a tuple of three RGB bytes */
291 if (parsed)

Callers 1

CheckColorFunction · 0.85

Calls 2

push_backMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected