home | blog | art | now | git gpg | email | rss

morph

Generate xresources colors from an image.
git clone https://pollux.codes/git/morph.git
Log | Files | Refs | README | LICENSE
commit 54428e420db18e30273d75d4b9955bba2409a766
parent 68b7a8e7640e3445da89fa3d573e282791c9d57c
Author: Pollux <pollux@pollux.codes>
Date:   Sun, 20 Jul 2025 20:25:22 -0500

fix: Various bug fixes

- Red and blue channels swapped when loading image
- Secondary color scores not stored properly

Signed-off-by: Pollux <pollux@pollux.codes>

Diffstat:
Mmorph.c | 88+++++++++++++++++++++++++++++++++++++++++++------------------------------------
1 file changed, 48 insertions(+), 40 deletions(-)

diff --git a/morph.c b/morph.c @@ -8,7 +8,7 @@ #include <Imlib2.h> -#define PRIMARY_COLOR_COUNT 128 +#define PRIMARY_COLOR_COUNT 32 #define PI 3.1415926535 #define min(a, b) ((a) < (b) ? (a) : (b)) @@ -165,9 +165,9 @@ get_image_pixel_data(const char *path, int *pixel_count) { for(int pixel = 0; pixel < image_width * image_height; pixel++) { DATA32 color = *(image_data + pixel); - image_pixels[pixel].r = (color & 0x000000ff) / 255.0f; + image_pixels[pixel].r = ((color & 0x00ff0000) >> 16) / 255.0f; image_pixels[pixel].g = ((color & 0x0000ff00) >> 8) / 255.0f; - image_pixels[pixel].b = ((color & 0x00ff0000) >> 16) / 255.0f; + image_pixels[pixel].b = (color & 0x000000ff) / 255.0f; } imlib_free_image(); @@ -307,15 +307,24 @@ hue_shift(col_lab_t in, float angle) { } col_lab_t -match_color(col_lab_t *palette, float *palette_weights, size_t palette_size, col_lab_t target_color) { - - float best_score = 0; - col_lab_t best_color; +match_color(col_lab_t *palette, float *palette_weights, size_t palette_size, + col_lab_t target_color) { + + float best_score = 0; + col_lab_t best_color; for(int c = 0; c < palette_size; c++) { - col_lab_t palette_color = palette[c]; - float color_distance = sqrtf((palette_color.l - target_color.l)*(palette_color.l - target_color.l) + (palette_color.a - target_color.a)*(palette_color.a - target_color.a) + (palette_color.b - target_color.b)*(palette_color.b - target_color.b)); - float score = palette_weights[c] / color_distance; + col_lab_t palette_color = palette[c]; + float color_distance = + sqrtf(0.2 * (palette_color.l - target_color.l) * + (palette_color.l - target_color.l) + + (palette_color.a - + target_color.a) * (palette_color.a - + target_color.a) + + (palette_color.b - + target_color.b) * (palette_color.b - + target_color.b)); + float score = palette_weights[c] / color_distance; if(score > best_score) { best_score = score; @@ -327,12 +336,12 @@ match_color(col_lab_t *palette, float *palette_weights, size_t palette_size, col } void -export_color(const char* name, col_lab_t color) { - col_rgb_t rgb = lab_to_rgb(color); +export_color(const char *name, col_lab_t color) { + col_rgb_t rgb = lab_to_rgb(color); - int red = min(1, max(0, rgb.r)) * 255; - int green = min(1, max(0, rgb.g)) * 255; - int blue = min(1, max(0, rgb.b)) * 255; + int red = min(1, max(0, rgb.r)) * 255; + int green = min(1, max(0, rgb.g)) * 255; + int blue = min(1, max(0, rgb.b)) * 255; printf("%s: #%02x%02x%02x\n", name, red, green, blue); } @@ -364,34 +373,33 @@ main(int argc, char **argv) { palette[4 * c] = primary_colors[c]; palette_weights[4 * c] = 1; palette[4 * c + 1] = hue_shift(primary_colors[c], PI); - palette_weights[4 * c] = 0.5; + palette_weights[4 * c + 1] = 0.8; palette[4 * c + 2] = hue_shift(primary_colors[c], PI / 6); - palette_weights[4 * c] = 0.3; + palette_weights[4 * c + 2] = 0.4; palette[4 * c + 3] = hue_shift(primary_colors[c], -PI / 6); - palette_weights[4 * c] = 0.3; + palette_weights[4 * c + 3] = 0.4; } - - col_lab_t color; - - TARGET_COLOR("st.color0" , 0, 0, 0, 0.2) - TARGET_COLOR("st.color1" , 1, 0, 0, 0.7) - TARGET_COLOR("st.color2" , 0, 1, 0, 0.7) - TARGET_COLOR("st.color3" , 1, 1, 0, 0.7) - TARGET_COLOR("st.color4" , 0, 0, 1, 0.7) - TARGET_COLOR("st.color5" , 1, 0, 1, 0.7) - TARGET_COLOR("st.color6" , 0, 1, 1, 0.7) - TARGET_COLOR("st.color7" , 0, 0, 0, 0.9) - TARGET_COLOR("st.color8" , 0, 0, 0, 0.4) - TARGET_COLOR("st.color9" , 1, 0, 0, 0.8) - TARGET_COLOR("st.color10", 0, 1, 0, 0.8) - TARGET_COLOR("st.color11", 1, 1, 0, 0.8) - TARGET_COLOR("st.color12", 0, 0, 1, 0.8) - TARGET_COLOR("st.color13", 1, 0, 1, 0.8) - TARGET_COLOR("st.color14", 0, 1, 1, 0.8) - TARGET_COLOR("st.color15", 0, 0, 0, 1.0) - - TARGET_COLOR("st.surface", 0, 0, 0, 0.1) - TARGET_COLOR("st.on_surface", 0, 0, 0, 1.0) + + col_lab_t color; + + TARGET_COLOR("st.color0", 0, 0, 0, 0.2); + TARGET_COLOR("st.color1", 1, 0, 0, 0.7); + TARGET_COLOR("st.color2", 0, 1, 0, 0.7); + TARGET_COLOR("st.color3", 1, 1, 0, 0.7); + TARGET_COLOR("st.color4", 0, 0, 1, 0.7); + TARGET_COLOR("st.color5", 1, 0, 1, 0.7); + TARGET_COLOR("st.color6", 0, 1, 1, 0.7); + TARGET_COLOR("st.color7", 0, 0, 0, 0.9); + TARGET_COLOR("st.color8", 0, 0, 0, 0.4); + TARGET_COLOR("st.color9", 1, 0, 0, 0.8); + TARGET_COLOR("st.color10", 0, 1, 0, 0.8); + TARGET_COLOR("st.color11", 1, 1, 0, 0.8); + TARGET_COLOR("st.color12", 0, 0, 1, 0.8); + TARGET_COLOR("st.color13", 1, 0, 1, 0.8); + TARGET_COLOR("st.color14", 0, 1, 1, 0.8); + TARGET_COLOR("st.color15", 0, 0, 0, 1.0); + TARGET_COLOR("st.surface", 0, 0, 0, 0.1); + TARGET_COLOR("st.on_surface", 0, 0, 0, 1.0); free(image_pixels); free(image_pixels_lab);