config: save water color as hex

This commit is contained in:
Marcin Kurczewski 2025-04-12 15:10:15 +02:00
parent 571ac73cb0
commit 1f6f85b0af
3 changed files with 23 additions and 4 deletions

View file

@ -8,6 +8,7 @@
#include "memory.h"
#include "strings.h"
#include <stdio.h>
#include <string.h>
#define EMPTY_ROOT "{}"
@ -248,6 +249,7 @@ void ConfigFile_LoadOptions(JSON_OBJECT *root_obj, const CONFIG_OPTION *options)
break;
case COT_RGB888: {
RGB_888 *const target = (RGB_888 *)opt->target;
JSON_VALUE *const value =
JSON_ObjectGetValue(root_obj, M_ResolveOptionName(opt->name));
bool success = false;
@ -255,11 +257,15 @@ void ConfigFile_LoadOptions(JSON_OBJECT *root_obj, const CONFIG_OPTION *options)
const uint32_t rgb_value =
JSON_ValueGetInt(value, JSON_INVALID_NUMBER);
ASSERT(rgb_value != JSON_INVALID_NUMBER);
RGB_888 *const target = (RGB_888 *)opt->target;
target->r = (rgb_value >> 0) & 0xFF;
target->g = (rgb_value >> 8) & 0xFF;
target->b = (rgb_value >> 16) & 0xFF;
success = true;
} else if (value != nullptr && value->type == JSON_TYPE_STRING) {
const char *str_value =
JSON_ValueGetString(value, JSON_INVALID_STRING);
ASSERT(str_value != JSON_INVALID_STRING);
success = String_ParseRGB888(str_value, target);
}
if (!success) {
*(RGB_888 *)opt->target = *(RGB_888 *)opt->default_value;
@ -307,9 +313,10 @@ void ConfigFile_DumpOptions(JSON_OBJECT *root_obj, const CONFIG_OPTION *options)
case COT_RGB888: {
const RGB_888 *const color = (RGB_888 *)opt->target;
JSON_ObjectAppendInt(
root_obj, M_ResolveOptionName(opt->name),
color->r | (color->g << 8) | (color->b << 16));
char tmp[10];
sprintf(tmp, "#%02X%02X%02X", color->r, color->g, color->b);
JSON_ObjectAppendString(
root_obj, M_ResolveOptionName(opt->name), tmp);
break;
}
}

View file

@ -1,5 +1,6 @@
#pragma once
#include "../colors.h"
#include "../vector.h"
#include <stdint.h>
@ -14,6 +15,7 @@ bool String_IsEmpty(const char *value);
bool String_ParseBool(const char *value, bool *target);
bool String_ParseInteger(const char *value, int32_t *target);
bool String_ParseDecimal(const char *value, float *target);
bool String_ParseRGB888(const char *value, RGB_888 *target);
char *String_ToUpper(const char *text);

View file

@ -174,6 +174,16 @@ bool String_ParseDecimal(const char *const value, float *const target)
return true;
}
bool String_ParseRGB888(const char *value, RGB_888 *const target)
{
if (value[0] == '#') {
value++;
}
return sscanf(
value, "%02hhX%02hhX%02hhX", &target->r, &target->g, &target->b)
== 3;
}
char *String_ToUpper(const char *text)
{
if (text == nullptr) {