split common code for colour conversion into a separate function
This commit is contained in:
2023-12-08 12:36:32 +01:00
committed by Sander Speetjens
parent 86f0a3bedc
commit 2a4de4fe34

View File

@@ -46,6 +46,38 @@ static char* shift_args(int* argc, char*** argv) {
return arg;
}
/**
* @brief This function converts a string to a color
* @param argc The number of arguments
* @param argv The arguments
* @param color The color to write to
* @return Returns 0 if successful, 1 if not
*/
static uint32_t str_to_color(int argc, char** argv, uint32_t* color) {
if (argc == 1) {
*color = (uint32_t)strtoul(argv[0], NULL, 16);
*color ^= 0xff000000;
return 0;
}
if (argc == 3) {
*color = 0xff000000;
*color |= (uint32_t)strtoul(argv[0], NULL, 10) << 16;
*color |= (uint32_t)strtoul(argv[1], NULL, 10) << 8;
*color |= (uint32_t)strtoul(argv[2], NULL, 10);
return 0;
}
if (argc == 4) {
*color = 0xff000000;
*color ^= (uint32_t)strtoul(argv[0], NULL, 10) << 24;
*color |= (uint32_t)strtoul(argv[1], NULL, 10) << 16;
*color |= (uint32_t)strtoul(argv[2], NULL, 10) << 8;
*color |= (uint32_t)strtoul(argv[3], NULL, 10);
return 0;
}
return 1;
}
/**
* @brief This function is a wrapper for tcp_write and tcp_output
* @param pcb The tcp_pcb struct to write to
@@ -130,27 +162,8 @@ static void tcp_cmd_text(struct tcp_pcb* pcb, int argc, char** argv) {
}
static void tcp_cmd_bg_color(struct tcp_pcb* pcb, int argc, char** argv) {
if (argc == 1) {
LOG_INFO(TAG, "Setting background color to %s", argv[0]);
color_bg = (uint32_t)strtoul(argv[0], NULL, 16);
color_bg ^= 0xff000000;
return;
}
if (argc == 3) {
LOG_INFO(TAG, "Setting background color to %s %s %s", argv[0], argv[1], argv[2]);
color_bg = 0xff000000;
color_bg |= (uint32_t)strtoul(argv[0], NULL, 10) << 16;
color_bg |= (uint32_t)strtoul(argv[1], NULL, 10) << 8;
color_bg |= (uint32_t)strtoul(argv[2], NULL, 10);
return;
}
if (argc == 4) {
LOG_INFO(TAG, "Setting background color to %s %s %s %s", argv[0], argv[1], argv[2], argv[3]);
color_bg = 0xff000000;
color_bg ^= (uint32_t)strtoul(argv[0], NULL, 10) << 24;
color_bg |= (uint32_t)strtoul(argv[1], NULL, 10) << 16;
color_bg |= (uint32_t)strtoul(argv[2], NULL, 10) << 8;
color_bg |= (uint32_t)strtoul(argv[3], NULL, 10);
if (!str_to_color(argc, argv, &color_bg)) {
LOG_INFO(TAG, "Setting background color to %08lX", color_bg);
return;
}
LOG_WARN(TAG, "Bad usage of bgcolor");
@@ -161,27 +174,8 @@ static void tcp_cmd_bg_color(struct tcp_pcb* pcb, int argc, char** argv) {
}
static void tcp_cmd_color(struct tcp_pcb* pcb, int argc, char** argv) {
if (argc == 1) {
LOG_INFO(TAG, "Setting text color to %s", argv[0]);
color_txt = (uint32_t)strtoul(argv[0], NULL, 16);
color_txt ^= 0xff000000;
return;
}
if (argc == 3) {
LOG_INFO(TAG, "Setting text color to %s %s %s", argv[0], argv[1], argv[2]);
color_txt = 0xff000000;
color_txt |= (uint32_t)strtoul(argv[0], NULL, 10) << 16;
color_txt |= (uint32_t)strtoul(argv[1], NULL, 10) << 8;
color_txt |= (uint32_t)strtoul(argv[2], NULL, 10);
return;
}
if (argc == 4) {
LOG_INFO(TAG, "Setting text color to %s %s %s %s", argv[0], argv[1], argv[2], argv[3]);
color_txt = 0xff000000;
color_txt ^= (uint32_t)strtoul(argv[0], NULL, 10) << 24;
color_txt |= (uint32_t)strtoul(argv[1], NULL, 10) << 16;
color_txt |= (uint32_t)strtoul(argv[2], NULL, 10) << 8;
color_txt |= (uint32_t)strtoul(argv[3], NULL, 10);
if (!str_to_color(argc, argv, &color_txt)) {
LOG_INFO(TAG, "Setting color to %08lX", color_txt);
return;
}
LOG_WARN(TAG, "Bad usage of color");