Browse Source

graphics: vertical gradient pattern in graphics lib

K. Lange 2 years ago
parent
commit
a6a5bc60f5
3 changed files with 30 additions and 27 deletions
  1. 10 0
      base/usr/include/toaru/graphics.h
  2. 4 27
      lib/button.c
  3. 16 0
      lib/graphics.c

+ 10 - 0
base/usr/include/toaru/graphics.h

@@ -117,4 +117,14 @@ extern void draw_line_aa(gfx_context_t * ctx, int x_1, int x_2, int y_1, int y_2
 
 extern void draw_sprite_rotate(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, float rotation, float alpha);
 
+struct gradient_definition {
+	int height;
+	int y;
+	uint32_t top;
+	uint32_t bottom;
+};
+
+extern uint32_t gfx_vertical_gradient_pattern(int32_t x, int32_t y, double alpha, void * extra);
+
+
 _End_C_Header

+ 4 - 27
lib/button.c

@@ -10,29 +10,6 @@
 #include <toaru/sdf.h>
 #include <toaru/icon_cache.h>
 
-struct gradient_definition {
-	int height;
-	int y;
-	uint32_t top;
-	uint32_t bottom;
-};
-
-static uint32_t gradient_pattern(int32_t x, int32_t y, double alpha, void * extra) {
-	struct gradient_definition * gradient = extra;
-	int base_r = _RED(gradient->top), base_g = _GRE(gradient->top), base_b = _BLU(gradient->top);
-	int last_r = _RED(gradient->bottom), last_g = _GRE(gradient->bottom), last_b = _GRE(gradient->bottom);
-	double gradpoint = (double)(y - (gradient->y)) / (double)gradient->height;
-
-	if (alpha > 1.0) alpha = 1.0;
-	if (alpha < 0.0) alpha = 0.0;
-
-	return premultiply(rgba(
-		base_r * (1.0 - gradpoint) + last_r * (gradpoint),
-		base_g * (1.0 - gradpoint) + last_g * (gradpoint),
-		base_b * (1.0 - gradpoint) + last_b * (gradpoint),
-		alpha * 255));
-}
-
 void ttk_button_draw(gfx_context_t * ctx, struct TTKButton * button) {
 	if (button->width == 0) {
 		return;
@@ -44,7 +21,7 @@ void ttk_button_draw(gfx_context_t * ctx, struct TTKButton * button) {
 	/* Dark edge */
 	if (hilight < 3) {
 		struct gradient_definition edge = {button->height, button->y, rgb(166,166,166), rgb(136,136,136)};
-		draw_rounded_rectangle_pattern(ctx, button->x, button->y, button->width, button->height, 4, gradient_pattern, &edge);
+		draw_rounded_rectangle_pattern(ctx, button->x, button->y, button->width, button->height, 4, gfx_vertical_gradient_pattern, &edge);
 	}
 
 	/* Sheen */
@@ -53,14 +30,14 @@ void ttk_button_draw(gfx_context_t * ctx, struct TTKButton * button) {
 	/* Button face - this should normally be a gradient */
 		if (hilight == 1) {
 			struct gradient_definition face = {button->height-3, button->y + 2, rgb(240,240,240), rgb(230,230,230)};
-			draw_rounded_rectangle_pattern(ctx, button->x + 2, button->y + 2, button->width - 4, button->height - 3, 2, gradient_pattern, &face);
+			draw_rounded_rectangle_pattern(ctx, button->x + 2, button->y + 2, button->width - 4, button->height - 3, 2, gfx_vertical_gradient_pattern, &face);
 		} else {
 			struct gradient_definition face = {button->height-3, button->y + 2, rgb(219,219,219), rgb(204,204,204)};
-			draw_rounded_rectangle_pattern(ctx, button->x + 2, button->y + 2, button->width - 4, button->height - 3, 2, gradient_pattern, &face);
+			draw_rounded_rectangle_pattern(ctx, button->x + 2, button->y + 2, button->width - 4, button->height - 3, 2, gfx_vertical_gradient_pattern, &face);
 		}
 	} else if (hilight == 2) {
 		struct gradient_definition face = {button->height-2, button->y + 1, rgb(180,180,180), rgb(160,160,160)};
-		draw_rounded_rectangle_pattern(ctx, button->x + 1, button->y + 1, button->width - 2, button->height - 2, 3, gradient_pattern, &face);
+		draw_rounded_rectangle_pattern(ctx, button->x + 1, button->y + 1, button->width - 2, button->height - 2, 3, gfx_vertical_gradient_pattern, &face);
 	}
 
 	if (button->title[0] != '\033') {

+ 16 - 0
lib/graphics.c

@@ -990,3 +990,19 @@ void draw_sprite_rotate(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32
 	}
 }
 
+uint32_t gfx_vertical_gradient_pattern(int32_t x, int32_t y, double alpha, void * extra) {
+	struct gradient_definition * gradient = extra;
+	int base_r = _RED(gradient->top), base_g = _GRE(gradient->top), base_b = _BLU(gradient->top);
+	int last_r = _RED(gradient->bottom), last_g = _GRE(gradient->bottom), last_b = _GRE(gradient->bottom);
+	double gradpoint = (double)(y - (gradient->y)) / (double)gradient->height;
+
+	if (alpha > 1.0) alpha = 1.0;
+	if (alpha < 0.0) alpha = 0.0;
+
+	return premultiply(rgba(
+		base_r * (1.0 - gradpoint) + last_r * (gradpoint),
+		base_g * (1.0 - gradpoint) + last_g * (gradpoint),
+		base_b * (1.0 - gradpoint) + last_b * (gradpoint),
+		alpha * 255));
+}
+