From 04e259e66b30394a38971df97deead57ef3a3655 Mon Sep 17 00:00:00 2001 From: IoIxD Date: Thu, 16 Oct 2025 22:36:37 +0000 Subject: [PATCH] color wheel: show chosen color in another box git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@382 b9cfdab3-6d41-4d17-bbe4-086880011989 --- examples/color_picker.c | 116 +++++++++++++++++++++++++--------------- 1 file changed, 73 insertions(+), 43 deletions(-) diff --git a/examples/color_picker.c b/examples/color_picker.c index 03ff349..4b26235 100644 --- a/examples/color_picker.c +++ b/examples/color_picker.c @@ -6,7 +6,6 @@ MwWidget vp; #define WIN_SIZE 512 #define PICKER_SIZE 360 #define IMG_POS ((WIN_SIZE - PICKER_SIZE) / 2) -#define IMAGE_SIZE (PICKER_SIZE * PICKER_SIZE * 4) typedef struct { double r; // a fraction between 0 and 1 @@ -54,15 +53,18 @@ static rgb hsv2rgb(hsv in) { } typedef struct { - MwWidget parent; - MwWidget color_wheel_img; - MwWidget slider; - MwLLPixmap pixmap; - double value; + MwWidget parent; + MwWidget color_wheel_img; + MwWidget slider; + MwWidget color_display; + MwLLPixmap color_wheel_pixmap; + MwLLPixmap color_display_pixmap; + double value; + unsigned char* color_wheel_image_data; + unsigned char* color_display_image_data; } color_wheel; -void color_wheel_update(color_wheel* wheel) { - unsigned char* data = malloc(IMAGE_SIZE); +void color_wheel_wheel_image_update(color_wheel* wheel) { for(int y = 0; y < PICKER_SIZE; y++) { for(int x = 0; x < PICKER_SIZE; x++) { int i = ((y * PICKER_SIZE) + x) * 4; @@ -72,10 +74,10 @@ void color_wheel_update(color_wheel* wheel) { double dist = sqrt(_x * _x + _y * _y); if(dist >= 180.) { - data[i] = 0; - data[i + 1] = 0; - data[i + 2] = 0; - data[i + 3] = 0; + wheel->color_wheel_image_data[i] = 0; + wheel->color_wheel_image_data[i + 1] = 0; + wheel->color_wheel_image_data[i + 2] = 0; + wheel->color_wheel_image_data[i + 3] = 0; } else { double xd = (M_PI / 180.) * ((double)_x); double yd = (M_PI / 180.) * ((double)_y); @@ -86,26 +88,58 @@ void color_wheel_update(color_wheel* wheel) { if(hue < 0.0) { hue += 360; } - rgb color = hsv2rgb((hsv){ - .h = hue / 360., - .s = dist / 179.61, - .v = wheel->value, - }); - data[i] = color.r * 255; - data[i + 1] = color.g * 255; - data[i + 2] = color.b * 255; - data[i + 3] = 255; + rgb color = hsv2rgb((hsv){ + .h = hue / 360., + .s = dist / 179.61, + .v = wheel->value, + }); + wheel->color_wheel_image_data[i] = color.r * 255; + wheel->color_wheel_image_data[i + 1] = color.g * 255; + wheel->color_wheel_image_data[i + 2] = color.b * 255; + wheel->color_wheel_image_data[i + 3] = 255; } } } - if(wheel->pixmap != NULL) { - MwLLDestroyPixmap(wheel->pixmap); + if(wheel->color_wheel_pixmap != NULL) { + MwLLDestroyPixmap(wheel->color_wheel_pixmap); } - wheel->pixmap = MwLoadRaw(wheel->parent, data, PICKER_SIZE, PICKER_SIZE); - free(data); - MwVaApply(wheel->color_wheel_img, MwNpixmap, wheel->pixmap, NULL); + wheel->color_wheel_pixmap = MwLoadRaw(wheel->parent, wheel->color_wheel_image_data, PICKER_SIZE, PICKER_SIZE); + MwVaApply(wheel->color_wheel_img, MwNpixmap, wheel->color_wheel_pixmap, NULL); } +void color_wheel_color_display_update(color_wheel* wheel, int r, int g, int b) { + for(int y = 0; y < PICKER_SIZE / 16; y++) { + for(int x = 0; x < PICKER_SIZE; x++) { + int i = ((y * PICKER_SIZE) + x) * 4; + + wheel->color_display_image_data[i] = r; + wheel->color_display_image_data[i + 1] = g; + wheel->color_display_image_data[i + 2] = b; + wheel->color_display_image_data[i + 3] = 255; + } + } + if(wheel->color_display_pixmap != NULL) { + MwLLDestroyPixmap(wheel->color_display_pixmap); + } + wheel->color_display_pixmap = MwLoadRaw(wheel->parent, wheel->color_display_image_data, PICKER_SIZE, (PICKER_SIZE / 16)); + MwVaApply(wheel->color_display, MwNpixmap, wheel->color_display_pixmap, NULL); +} +static void color_wheel_click(MwWidget handle, void* user, void* call) { + color_wheel* wheel = (color_wheel*)user; + MwLLMouse* mouse = (MwLLMouse*)call; + + color_wheel_wheel_image_update(wheel); + + int i = ((mouse->point.y * PICKER_SIZE) + mouse->point.x) * 4; + + int r = wheel->color_wheel_image_data[i]; + int g = wheel->color_wheel_image_data[i + 1]; + int b = wheel->color_wheel_image_data[i + 2]; + + color_wheel_color_display_update(wheel, r, g, b); + + printf("%d %d %d\n", r, g, b); +} static void color_wheel_on_change(MwWidget handle, void* user, void* call) { color_wheel* wheel = (color_wheel*)user; @@ -114,35 +148,31 @@ static void color_wheel_on_change(MwWidget handle, void* user, void* call) { wheel->value = 1.0 - ((double)value / 1024.); - color_wheel_update(wheel); + color_wheel_wheel_image_update(wheel); } void color_wheel_setup(MwWidget parent, color_wheel* wheel) { wheel->parent = parent; - wheel->color_wheel_img = MwVaCreateWidget(MwImageClass, "image", wheel->parent, IMG_POS, IMG_POS, PICKER_SIZE, PICKER_SIZE, NULL); - wheel->pixmap = NULL; - wheel->value = 1; + wheel->color_wheel_img = MwVaCreateWidget(MwImageClass, "image", wheel->parent, IMG_POS, IMG_POS, PICKER_SIZE, PICKER_SIZE, NULL); + wheel->color_wheel_image_data = malloc(PICKER_SIZE * PICKER_SIZE * 4); + wheel->color_display_image_data = malloc((PICKER_SIZE / 16) * PICKER_SIZE * 4); - color_wheel_update(wheel); + wheel->color_wheel_pixmap = NULL; + wheel->color_display_pixmap = NULL; + wheel->value = 1; + + color_wheel_wheel_image_update(wheel); + + MwAddUserHandler(wheel->color_wheel_img, MwNmouseDownHandler, color_wheel_click, wheel); wheel->slider = MwVaCreateWidget(MwScrollBarClass, "scroll", wheel->parent, IMG_POS + PICKER_SIZE, IMG_POS, PICKER_SIZE / 32, PICKER_SIZE, MwNorientation, MwVERTICAL, MwNminValue, 0, MwNmaxValue, 1024, NULL); - MwAddUserHandler(wheel->slider, MwNchangedHandler, color_wheel_on_change, wheel); + + wheel->color_display = MwCreateWidget(MwImageClass, "colorDisplay", wheel->parent, IMG_POS, IMG_POS + PICKER_SIZE + (PICKER_SIZE / 64), PICKER_SIZE, PICKER_SIZE / 16); }; int main() { - for(int h = 0; h < 255; h++) { - for(int s = 0; s < 255; s++) { - for(int v = 0; v < 255; v++) { - rgb col = hsv2rgb((hsv){ - .h = h, - .s = s, - .v = v}); - } - } - } - MwWidget w = MwVaCreateWidget(MwWindowClass, "main", NULL, MwDEFAULT, MwDEFAULT, WIN_SIZE, WIN_SIZE, MwNtitle, "test", NULL); color_wheel wheel;