summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/color_palette.c42
1 files changed, 34 insertions, 8 deletions
diff --git a/src/util/color_palette.c b/src/util/color_palette.c
index 886ba8e..02af36f 100644
--- a/src/util/color_palette.c
+++ b/src/util/color_palette.c
@@ -1,3 +1,5 @@
+#include <al/log.h>
+
#include "color_palette.h"
struct camu_color_palette global_color_palette = { 0 };
@@ -33,7 +35,7 @@ static char *normal_colors[16] = {
bool camu_color_palette_init(str *path)
{
struct nn_file file;
- if (!nn_file_open(&file, path, 0)) {
+ if (!nn_file_open(&file, path, NNWT_FILE_READONLY)) {
return false;
}
str s;
@@ -41,30 +43,54 @@ bool camu_color_palette_init(str *path)
nn_file_close(&file);
json_error_t error;
json_t *root = json_loadb(s.data, s.length, 0, &error);
- if (!root) return false;
+ if (!root) {
+ al_log_error("color_palette", "Failed to load %.*s as a json (%s).",
+ al_str_fmt(path), error.text);
+ return false;
+ }
json_t *special = json_object_get(root, "special");
+ if (!special) {
+ al_log_error("color_palette", "\"special\" field missing from the color palette.");
+ return false;
+ }
json_t *colors = json_object_get(root, "colors");
- if (!special || !colors) return false;
+ if (!colors) {
+ al_log_error("color_palette", "\"colors\" field missing from the color palette.");
+ return false;
+ }
json_t *object;
- bool to_long_error;
const char *color;
u32 value;
+ u32 index;
+ bool to_long_error;
for (u32 i = 0; i < ARRAY_SIZE(special_colors); i++) {
object = json_object_get(special, special_colors[i]);
- if (!object) return false;
+ if (!object) {
+ al_log_error("color_palette", "Couldn't find special color: %s.", special_colors[i]);
+ return false;
+ }
color = json_string_value(object);
- value = (u32)al_str_to_long(&al_str_w((char *)color, 1, 6), 16, &to_long_error);
+ index = color[0] == '#' ? 1 : 0;
+ value = (u32)al_str_to_long(&al_str_w((char *)color, index, 6), 16, &to_long_error);
if (!to_long_error) {
global_color_palette.colors[i] = value;
+ } else {
+ al_log_warn("color_palette", "%s is not a valid hex color.", normal_colors[i]);
}
}
for (u32 i = 0; i < ARRAY_SIZE(normal_colors); i++) {
object = json_object_get(colors, normal_colors[i]);
- if (!object) return false;
+ if (!object) {
+ al_log_error("color_palette", "Couldn't find color: %s.", normal_colors[i]);
+ return false;
+ }
color = json_string_value(object);
- value = (u32)al_str_to_long(&al_str_w((char *)color, 1, 6), 16, &to_long_error);
+ index = color[0] == '#' ? 1 : 0;
+ value = (u32)al_str_to_long(&al_str_w((char *)color, index, 6), 16, &to_long_error);
if (!to_long_error) {
global_color_palette.colors[i + 2] = value;
+ } else {
+ al_log_warn("color_palette", "%s is not a valid hex color.", normal_colors[i]);
}
}
return true;