1
0
mirror of https://codeberg.org/vyivel/dulcepan/ synced 2025-03-12 18:59:15 +02:00

config: store colors as float arrays

This commit is contained in:
Kirill Primak 2024-06-23 20:59:39 +03:00
parent a2d13cbdf8
commit a7ea928b1c
3 changed files with 35 additions and 27 deletions

View File

@ -10,37 +10,35 @@
#define CONFIG_SUBPATH "dulcepan.cfg" #define CONFIG_SUBPATH "dulcepan.cfg"
#define COLOR_MUL (UINT16_MAX / UINT8_MAX) static void bytes_to_color(uint8_t bytes[static 4], float out[static 4]) {
out[3] = (float)bytes[3] / UINT8_MAX;
static void bytes_to_color(uint32_t bytes[static 4], pixman_color_t *out) { for (size_t i = 0; i < 3; i++) {
out->red = (uint16_t)(bytes[0] * bytes[3] * COLOR_MUL / UINT8_MAX); out[i] = (float)bytes[i] * out[3] / UINT8_MAX;
out->green = (uint16_t)(bytes[1] * bytes[3] * COLOR_MUL / UINT8_MAX); }
out->blue = (uint16_t)(bytes[2] * bytes[3] * COLOR_MUL / UINT8_MAX);
out->alpha = (uint16_t)(bytes[3] * COLOR_MUL);
} }
static void load_color(const char *value, int line_idx, pixman_color_t *out) { static void load_color(const char *value, int line_idx, float out[static 4]) {
size_t len = strlen(value); size_t len = strlen(value);
uint32_t bytes[4] = {0, 0, 0, 0}; uint8_t bytes[4] = {0, 0, 0, 0};
if (len == 6 && len != 8) { if (len == 6) {
bytes[3] = UINT8_MAX; bytes[3] = UINT8_MAX;
} else if (len != 8) { } else if (len != 8) {
goto bad; goto bad;
} }
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
uint32_t digit; uint8_t digit;
if (value[i] >= '0' && value[i] <= '9') { if (value[i] >= '0' && value[i] <= '9') {
digit = (uint32_t)value[i] - '0'; digit = (uint8_t)(value[i] - '0');
} else if (value[i] >= 'A' && value[i] <= 'F') { } else if (value[i] >= 'A' && value[i] <= 'F') {
digit = (uint32_t)value[i] - 'A' + 10; digit = (uint8_t)(value[i] - 'A' + 10);
} else if (value[i] >= 'a' && value[i] <= 'f') { } else if (value[i] >= 'a' && value[i] <= 'f') {
digit = (uint32_t)value[i] - 'a' + 10; digit = (uint8_t)(value[i] - 'a' + 10);
} else { } else {
goto bad; goto bad;
} }
bytes[i / 2] = bytes[i / 2] * 16 + digit; bytes[i / 2] = (uint8_t)(bytes[i / 2] * 16 + digit);
} }
bytes_to_color(bytes, out); bytes_to_color(bytes, out);
@ -88,9 +86,9 @@ void dp_config_load(struct dp_config *config, const char *user_path) {
.png_compression = 6, .png_compression = 6,
.quick_select = false, .quick_select = false,
}; };
bytes_to_color((uint32_t[]){0xff, 0xff, 0xff, 0x40}, &config->unselected_color); bytes_to_color((uint8_t[]){0xff, 0xff, 0xff, 0x40}, config->unselected_color);
bytes_to_color((uint32_t[]){0x00, 0x00, 0x00, 0x00}, &config->selected_color); bytes_to_color((uint8_t[]){0x00, 0x00, 0x00, 0x00}, config->selected_color);
bytes_to_color((uint32_t[]){0xff, 0xff, 0xff, 0xff}, &config->border_color); bytes_to_color((uint8_t[]){0xff, 0xff, 0xff, 0xff}, config->border_color);
if (user_path != NULL) { if (user_path != NULL) {
fp = fopen(user_path, "r"); fp = fopen(user_path, "r");
@ -171,11 +169,11 @@ void dp_config_load(struct dp_config *config, const char *user_path) {
line[i] = '\0'; line[i] = '\0';
if (strcmp(key, "unselected-color") == 0) { if (strcmp(key, "unselected-color") == 0) {
load_color(value, line_idx, &config->unselected_color); load_color(value, line_idx, config->unselected_color);
} else if (strcmp(key, "selected-color") == 0) { } else if (strcmp(key, "selected-color") == 0) {
load_color(value, line_idx, &config->selected_color); load_color(value, line_idx, config->selected_color);
} else if (strcmp(key, "border-color") == 0) { } else if (strcmp(key, "border-color") == 0) {
load_color(value, line_idx, &config->border_color); load_color(value, line_idx, config->border_color);
} else if (strcmp(key, "border-size") == 0) { } else if (strcmp(key, "border-size") == 0) {
load_int(value, line_idx, &config->border_size); load_int(value, line_idx, &config->border_size);
} else if (strcmp(key, "png-compression") == 0) { } else if (strcmp(key, "png-compression") == 0) {

View File

@ -123,9 +123,9 @@ enum dp_file_format {
}; };
struct dp_config { struct dp_config {
pixman_color_t unselected_color; float unselected_color[4];
pixman_color_t selected_color; float selected_color[4];
pixman_color_t border_color; float border_color[4];
xkb_keysym_t quit_key; xkb_keysym_t quit_key;
xkb_keysym_t save_key; xkb_keysym_t save_key;
int border_size; // 0 if disabled int border_size; // 0 if disabled

View File

@ -166,6 +166,16 @@ static void help(const char *prog) {
prog); prog);
} }
static pixman_image_t *make_image(float color[static 4]) {
pixman_color_t pixman_color = {
.red = (uint16_t)(color[0] * UINT16_MAX),
.green = (uint16_t)(color[1] * UINT16_MAX),
.blue = (uint16_t)(color[2] * UINT16_MAX),
.alpha = (uint16_t)(color[3] * UINT16_MAX),
};
return pixman_image_create_solid_fill(&pixman_color);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
struct dp_state state = {0}; struct dp_state state = {0};
@ -223,9 +233,9 @@ int main(int argc, char **argv) {
dp_log_fatal("Failed to connect to a Wayland compositor"); dp_log_fatal("Failed to connect to a Wayland compositor");
} }
state.unselected_fill_image = pixman_image_create_solid_fill(&state.config.unselected_color); state.unselected_fill_image = make_image(state.config.unselected_color);
state.selected_fill_image = pixman_image_create_solid_fill(&state.config.selected_color); state.selected_fill_image = make_image(state.config.selected_color);
state.border_fill_image = pixman_image_create_solid_fill(&state.config.border_color); state.border_fill_image = make_image(state.config.border_color);
run(&state); run(&state);