fix: shrink buffers when unused fraction gets too large

This commit is contained in:
2026-03-10 14:48:55 +00:00
parent 2d6746c99c
commit 143a33558d
9 changed files with 229 additions and 215 deletions

View File

@@ -21,82 +21,76 @@ struct _VektorColorWheel {
G_DEFINE_TYPE(VektorColorWheel, vektor_color_wheel, GTK_TYPE_DRAWING_AREA)
static gboolean point_in_triangle(
double px, double py,
double ax, double ay,
double bx, double by,
double cx, double cy,
double* u, double* v, double* w)
{
double denom =
(by - cy)*(ax - cx) +
(cx - bx)*(ay - cy);
static gboolean point_in_triangle(double px, double py, double ax, double ay,
double bx, double by, double cx, double cy,
double* u, double* v, double* w) {
double denom = (by - cy) * (ax - cx) + (cx - bx) * (ay - cy);
*u =
((by - cy)*(px - cx) +
(cx - bx)*(py - cy)) / denom;
*u = ((by - cy) * (px - cx) + (cx - bx) * (py - cy)) / denom;
*v =
((cy - ay)*(px - cx) +
(ax - cx)*(py - cy)) / denom;
*v = ((cy - ay) * (px - cx) + (ax - cx) * (py - cy)) / denom;
*w = 1 - *u - *v;
return (*u >= 0 && *v >= 0 && *w >= 0);
}
static void closest_point_on_segment(
double px, double py,
double ax, double ay,
double bx, double by,
double *rx, double *ry)
{
static void closest_point_on_segment(double px, double py, double ax, double ay,
double bx, double by, double* rx,
double* ry) {
double abx = bx - ax;
double aby = by - ay;
double apx = px - ax;
double apy = py - ay;
double t = (apx*abx + apy*aby) / (abx*abx + aby*aby);
double t = (apx * abx + apy * aby) / (abx * abx + aby * aby);
if (t < 0) t = 0;
if (t > 1) t = 1;
if (t < 0)
t = 0;
if (t > 1)
t = 1;
*rx = ax + abx * t;
*ry = ay + aby * t;
}
static void closest_point_on_triangle(
double px, double py,
double ax, double ay,
double bx, double by,
double cx, double cy,
double *rx, double *ry)
{
double p1x,p1y;
double p2x,p2y;
double p3x,p3y;
static void closest_point_on_triangle(double px, double py, double ax,
double ay, double bx, double by,
double cx, double cy, double* rx,
double* ry) {
double p1x, p1y;
double p2x, p2y;
double p3x, p3y;
closest_point_on_segment(px,py, ax,ay, bx,by, &p1x,&p1y);
closest_point_on_segment(px,py, bx,by, cx,cy, &p2x,&p2y);
closest_point_on_segment(px,py, cx,cy, ax,ay, &p3x,&p3y);
closest_point_on_segment(px, py, ax, ay, bx, by, &p1x, &p1y);
closest_point_on_segment(px, py, bx, by, cx, cy, &p2x, &p2y);
closest_point_on_segment(px, py, cx, cy, ax, ay, &p3x, &p3y);
double d1 = (px-p1x)*(px-p1x) + (py-p1y)*(py-p1y);
double d2 = (px-p2x)*(px-p2x) + (py-p2y)*(py-p2y);
double d3 = (px-p3x)*(px-p3x) + (py-p3y)*(py-p3y);
double d1 = (px - p1x) * (px - p1x) + (py - p1y) * (py - p1y);
double d2 = (px - p2x) * (px - p2x) + (py - p2y) * (py - p2y);
double d3 = (px - p3x) * (px - p3x) + (py - p3y) * (py - p3y);
if (d1 <= d2 && d1 <= d3) { *rx = p1x; *ry = p1y; }
else if (d2 <= d3) { *rx = p2x; *ry = p2y; }
else { *rx = p3x; *ry = p3y; }
if (d1 <= d2 && d1 <= d3) {
*rx = p1x;
*ry = p1y;
} else if (d2 <= d3) {
*rx = p2x;
*ry = p2y;
} else {
*rx = p3x;
*ry = p3y;
}
}
static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot) {
static void vektor_color_wheel_snapshot(GtkWidget* widget,
GtkSnapshot* snapshot) {
VektorColorWheel* self = VEKTOR_COLOR_WHEEL(widget);
int width = gtk_widget_get_width(widget);
int height = gtk_widget_get_height(widget);
graphene_rect_t bounds = GRAPHENE_RECT_INIT(0,0,width,height);
graphene_rect_t bounds = GRAPHENE_RECT_INIT(0, 0, width, height);
cairo_t* cr = gtk_snapshot_append_cairo(snapshot, &bounds);
double cx = width / 2.0;
@@ -109,8 +103,8 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
double triangle_radius = wheel_radius * 0.75;
// wheel draw
for(int a = 0; a < 360; a++) {
double angle_1 = a*(M_PI / 180.0);
for (int a = 0; a < 360; a++) {
double angle_1 = a * (M_PI / 180.0);
double angle_2 = (a + 1) * (M_PI / 180.0);
cairo_new_path(cr);
@@ -118,7 +112,7 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
cairo_arc_negative(cr, cx, cy, inner_radius, angle_2, angle_1);
cairo_close_path(cr);
float r,g,b;
float r, g, b;
gtk_hsv_to_rgb(a / 360.0, 1.0, 1.0, &r, &g, &b);
cairo_set_source_rgb(cr, r, g, b);
@@ -126,7 +120,7 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
}
// triangle draw
double ax = cx + triangle_radius;
double ax = cx + triangle_radius;
double ay = cy;
double bx = cx - 0.5 * triangle_radius;
@@ -153,16 +147,20 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
// White gradient: from pure hue (right) → white (bottom)
cairo_pattern_t* white = cairo_pattern_create_linear(ax, ay, bx, by);
cairo_pattern_add_color_stop_rgba(white, 0.0, 1,1,1, 0.0); // transparent at pure hue
cairo_pattern_add_color_stop_rgba(white, 1.0, 1,1,1, 1.0); // opaque white at bottom
cairo_pattern_add_color_stop_rgba(white, 0.0, 1, 1, 1,
0.0); // transparent at pure hue
cairo_pattern_add_color_stop_rgba(white, 1.0, 1, 1, 1,
1.0); // opaque white at bottom
cairo_set_source(cr, white);
cairo_paint(cr);
cairo_pattern_destroy(white);
// Black gradient: from pure hue (right) → black (top)
cairo_pattern_t* black = cairo_pattern_create_linear(ax, ay, cx2, cy2);
cairo_pattern_add_color_stop_rgba(black, 0.0, 0,0,0, 0.0); // transparent at pure hue
cairo_pattern_add_color_stop_rgba(black, 1.0, 0,0,0, 1.0); // opaque black at top
cairo_pattern_add_color_stop_rgba(black, 0.0, 0, 0, 0,
0.0); // transparent at pure hue
cairo_pattern_add_color_stop_rgba(black, 1.0, 0, 0, 0,
1.0); // opaque black at top
cairo_set_source(cr, black);
cairo_paint(cr);
cairo_pattern_destroy(black);
@@ -170,26 +168,26 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
cairo_restore(cr);
// triangle outline
cairo_set_source_rgb(cr,.1,.1,.1);
cairo_set_source_rgb(cr, .1, .1, .1);
cairo_move_to(cr, ax, ay);
cairo_line_to(cr, bx, by);
cairo_line_to(cr, cx2, cy2);
cairo_close_path(cr);
cairo_set_source_rgb(cr,.1,.1,.1);
cairo_set_source_rgb(cr, .1, .1, .1);
cairo_stroke(cr);
// selectors draw
// triangle selector
double chroma_weight = self->saturation * self->lightness;
double white_weight = (1.0 - self->saturation) * self->lightness;
double black_weight = 1.0 - self->lightness;
double white_weight = (1.0 - self->saturation) * self->lightness;
double black_weight = 1.0 - self->lightness;
double px = ax * chroma_weight + bx * white_weight + cx2 * black_weight;
double py = ay * chroma_weight + by * white_weight + cy2 * black_weight;
cairo_arc(cr, px, py, 5, 0, 2*M_PI);
cairo_arc(cr, px, py, 5, 0, 2 * M_PI);
float fr, fg, fb;
vektor_color_wheel_get_colorout(self, &fr, &fg, &fb);
@@ -205,17 +203,12 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
cairo_new_path(cr);
cairo_arc(cr,
cx, cy,
wheel_radius,
selector_angle - selector_width,
selector_angle + selector_width);
cairo_arc(cr, cx, cy, wheel_radius, selector_angle - selector_width,
selector_angle + selector_width);
cairo_arc_negative(cr,
cx, cy,
inner_radius,
selector_angle + selector_width,
selector_angle - selector_width);
cairo_arc_negative(cr, cx, cy, inner_radius,
selector_angle + selector_width,
selector_angle - selector_width);
cairo_close_path(cr);
@@ -225,7 +218,8 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
cairo_destroy(cr);
}
static void on_click(GtkGestureClick* gesture, int n_press, double x, double y, gpointer data) {
static void on_click(GtkGestureClick* gesture, int n_press, double x, double y,
gpointer data) {
VektorColorWheel* wheel = VEKTOR_COLOR_WHEEL(data);
GtkWidget* widget = GTK_WIDGET(wheel);
@@ -240,7 +234,7 @@ static void on_click(GtkGestureClick* gesture, int n_press, double x, double y,
double cx = width / 2.0;
double cy = height / 2.0;
double ax = cx + triangle_radius;
double ax = cx + triangle_radius;
double ay = cy;
double bx = cx - 0.5 * triangle_radius;
@@ -249,33 +243,28 @@ static void on_click(GtkGestureClick* gesture, int n_press, double x, double y,
double cx2 = cx - 0.5 * triangle_radius;
double cy2 = cy - 0.866 * triangle_radius;
double u,v,w;
gboolean inside = point_in_triangle(x,y, ax,ay, bx,by, cx2,cy2, &u,&v,&w);
double u, v, w;
gboolean inside =
point_in_triangle(x, y, ax, ay, bx, by, cx2, cy2, &u, &v, &w);
if(wheel->dragging_triangle) {
if (wheel->dragging_triangle) {
if(!inside) { // if outside triangle, snap to its edge
double sx,sy;
if (!inside) { // if outside triangle, snap to its edge
double sx, sy;
closest_point_on_triangle(
x,y,
ax,ay,
bx,by,
cx2,cy2,
&sx,&sy
);
closest_point_on_triangle(x, y, ax, ay, bx, by, cx2, cy2, &sx, &sy);
x = sx;
y = sy;
point_in_triangle(x,y, ax,ay, bx,by, cx2,cy2, &u,&v,&w);
point_in_triangle(x, y, ax, ay, bx, by, cx2, cy2, &u, &v, &w);
}
double denom = u + v;
if (denom > 0.0001) { // avoid div-by-zero at black vertex
if (denom > 0.0001) { // avoid div-by-zero at black vertex
wheel->saturation = u / denom;
} else {
wheel->saturation = 0.0; // arbitrary, since S irrelevant at V=0
wheel->saturation = 0.0; // arbitrary, since S irrelevant at V=0
}
wheel->lightness = denom;
@@ -284,27 +273,30 @@ static void on_click(GtkGestureClick* gesture, int n_press, double x, double y,
double dy = y - cy;
double angle = atan2(dy, dx);
if(angle < 0) { angle += 2 * M_PI; }
if (angle < 0) {
angle += 2 * M_PI;
}
wheel->hue = angle / (2*M_PI);
wheel->hue = angle / (2 * M_PI);
}
g_signal_emit(wheel, signals[COLOR_CHANGED], 0);
gtk_widget_queue_draw(widget);
}
static void on_drag(GtkGestureDrag* gesture, double offset_x, double offset_y, gpointer data) {
double x,y;
gtk_gesture_drag_get_start_point(gesture,&x,&y);
static void on_drag(GtkGestureDrag* gesture, double offset_x, double offset_y,
gpointer data) {
double x, y;
gtk_gesture_drag_get_start_point(gesture, &x, &y);
x += offset_x;
y += offset_y;
on_click(NULL,0,x,y,data);
on_click(NULL, 0, x, y, data);
}
static void on_drag_begin(GtkGestureDrag* gesture, gdouble start_x, gdouble start_y, gpointer user_data) {
static void on_drag_begin(GtkGestureDrag* gesture, gdouble start_x,
gdouble start_y, gpointer user_data) {
// set dragging_wheel or dragging_triangle which are used
// to determine to where the cursor should snap to
@@ -319,13 +311,13 @@ static void on_drag_begin(GtkGestureDrag* gesture, gdouble start_x, gdouble star
double dx = start_x - cx;
double dy = start_y - cy;
double dist = sqrt(dx*dx+dy*dy);
double dist = sqrt(dx * dx + dy * dy);
double outer_radius = MIN(width, height) / 2.0;
double wheel_radius = outer_radius * 0.95;
double inner_radius = wheel_radius * 0.9;
if(dist > inner_radius) {
if (dist > inner_radius) {
wheel->dragging_wheel = TRUE;
wheel->dragging_triangle = FALSE;
} else if (dist < inner_radius) {
@@ -355,17 +347,8 @@ static void vektor_color_wheel_class_init(VektorColorWheelClass* klass) {
widget_class->snapshot = vektor_color_wheel_snapshot;
signals[COLOR_CHANGED] =
g_signal_new(
"color-changed",
G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_FIRST,
0,
NULL,
NULL,
NULL,
G_TYPE_NONE,
0
);
g_signal_new("color-changed", G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
}
GtkWidget* vektor_color_wheel_new(void) {
@@ -373,30 +356,23 @@ GtkWidget* vektor_color_wheel_new(void) {
}
VektorColor vektor_color_wheel_get_color(VektorColorWheel* wheel) {
float r,g,b;
gtk_hsv_to_rgb(wheel->hue,
wheel->saturation,
wheel->lightness,
&r, &g, &b);
return (VektorColor) {
.r = (unsigned char)(r*255),
.g = (unsigned char)(g*255),
.b = (unsigned char)(b*255) ,
.a = 255
};
float r, g, b;
gtk_hsv_to_rgb(wheel->hue, wheel->saturation, wheel->lightness, &r, &g, &b);
return (VektorColor){.r = (unsigned char)(r * 255),
.g = (unsigned char)(g * 255),
.b = (unsigned char)(b * 255),
.a = 255};
}
void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r, float* g, float* b) {
gtk_hsv_to_rgb(wheel->hue,
wheel->saturation,
wheel->lightness,
r, g, b);
void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r,
float* g, float* b) {
gtk_hsv_to_rgb(wheel->hue, wheel->saturation, wheel->lightness, r, g, b);
}
void vektor_color_wheel_set_color(VektorColorWheel* wheel, VektorColor c) {
float h,s,v;
gtk_rgb_to_hsv(c.r/255.0, c.g/255.0, c.b/255.0, &h, &s, &v);
float h, s, v;
gtk_rgb_to_hsv(c.r / 255.0, c.g / 255.0, c.b / 255.0, &h, &s, &v);
wheel->hue = (float)h;
wheel->saturation = (float)s;
wheel->lightness = (float)v;

View File

@@ -5,11 +5,13 @@
#include "src/util/color.h"
#define VEKTOR_TYPE_COLOR_WHEEL vektor_color_wheel_get_type()
G_DECLARE_FINAL_TYPE(VektorColorWheel, vektor_color_wheel, VEKTOR, COLOR_WHEEL, GtkDrawingArea)
G_DECLARE_FINAL_TYPE(VektorColorWheel, vektor_color_wheel, VEKTOR, COLOR_WHEEL,
GtkDrawingArea)
GtkWidget* vektor_color_wheel_new(void);
VektorColor vektor_color_wheel_get_color(VektorColorWheel* wheel);
void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r, float* g, float* b);
void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r,
float* g, float* b);
void vektor_color_wheel_set_color(VektorColorWheel* wheel, VektorColor c);
#endif