Add color options, bump minor version
This commit is contained in:
55
lib/options.c
Normal file
55
lib/options.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "options.h"
|
||||
|
||||
mazeoptions_t* mazemaker_options_new() {
|
||||
mazeoptions_t* result = malloc(sizeof(mazeoptions_t));
|
||||
|
||||
// set defaults
|
||||
memset(result->wall_color, 0, 3);
|
||||
memset(result->background_color, 0xff, 3);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void mazemaker_options_free(mazeoptions_t* options) {
|
||||
free(options);
|
||||
}
|
||||
|
||||
static int stringToColor(char const* color_desc, rgb_color_t dst) {
|
||||
if (color_desc[0] != '#') return -1; // bad format
|
||||
|
||||
size_t l;
|
||||
for (l = 1; color_desc[l] != '\0'; l++) if (!isxdigit(color_desc[l])) return -1; // bad format
|
||||
|
||||
if ((l != 4) && (l != 7)) return -1; // bad format
|
||||
|
||||
char x2[3] = { 0, 0, 0};
|
||||
if (l == 4) {
|
||||
x2[0] = x2[1] = color_desc[1];
|
||||
sscanf(x2, "%hhx", &dst[0]);
|
||||
x2[0] = x2[1] = color_desc[2];
|
||||
sscanf(x2, "%hhx", &dst[1]);
|
||||
x2[0] = x2[1] = color_desc[3];
|
||||
sscanf(x2, "%hhx", &dst[2]);
|
||||
} else {
|
||||
strncpy(x2, color_desc + 1, 2);
|
||||
sscanf(x2, "%hhx", &dst[0]);
|
||||
strncpy(x2, color_desc + 3, 2);
|
||||
sscanf(x2, "%hhx", &dst[1]);
|
||||
strncpy(x2, color_desc + 5, 2);
|
||||
sscanf(x2, "%hhx", &dst[2]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mazemaker_options_set_wall_color(mazeoptions_t* options, char const* color_desc) {
|
||||
return stringToColor(color_desc, options->wall_color);
|
||||
}
|
||||
|
||||
int mazemaker_options_set_background_color(mazeoptions_t* options, char const* color_desc) {
|
||||
return stringToColor(color_desc, options->background_color);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user