From 184c5b7af1b1adc665b4a8f428dcc918cf7efd73 Mon Sep 17 00:00:00 2001 From: markuspg Date: Wed, 27 Mar 2024 23:36:23 +0100 Subject: [PATCH] Rename color handling symbols and move out --- callbacks.c | 56 +++------------------------------ callbacks.h | 22 ------------- meson.build | 1 + screentest_colors.c | 61 +++++++++++++++++++++++++++++++++++ screentest_colors.h | 75 ++++++++++++++++++++++++++++++++++++++++++++ test_basic.c | 24 +++++++------- test_blink.c | 13 ++++---- test_bright_pixels.c | 8 +++-- test_gradient.c | 1 + test_grid.c | 5 +-- test_horizontal.c | 5 +-- test_lcdalign.c | 5 +-- test_text.c | 5 +-- test_vertical.c | 5 +-- 14 files changed, 183 insertions(+), 103 deletions(-) create mode 100644 screentest_colors.c create mode 100644 screentest_colors.h diff --git a/callbacks.c b/callbacks.c index 0423c4a..5bc8ecf 100644 --- a/callbacks.c +++ b/callbacks.c @@ -28,57 +28,20 @@ #include "callbacks.h" #include "gettext.h" #include "main.h" +#include "screentest_colors.h" #define _(String) gettext(String) -GdkRGBA fgcolors[COLOR_MAX]; -GdkRGBA *fg_color, *bg_color; -GdkRGBA grays[GRAYS_MAX]; -int fg_count = COLOR_WHITE; +int fg_count = SCREENTEST_COLORS_WHITE; static GtkWidget *mainwin = NULL; static struct test_ops *current_test = &basic_ops; G_MODULE_EXPORT void on_mainwin_realize(GtkWidget *widget, G_GNUC_UNUSED gpointer user_data) { - gint i; - #ifndef DEBUG gtk_window_fullscreen(GTK_WINDOW(widget)); #endif - memset(fgcolors, 0, COLOR_MAX * sizeof(GdkRGBA)); - - fgcolors[COLOR_WHITE].red = fgcolors[COLOR_WHITE].green = - fgcolors[COLOR_WHITE].blue = fgcolors[COLOR_WHITE].alpha = 1.0; - - fgcolors[COLOR_RED].red = fgcolors[COLOR_RED].alpha = 1.0; - - fgcolors[COLOR_GREEN].green = fgcolors[COLOR_GREEN].alpha = 1.0; - - fgcolors[COLOR_BLUE].blue = fgcolors[COLOR_BLUE].alpha = 1.0; - - fgcolors[COLOR_CYAN].green = 1.0; - fgcolors[COLOR_CYAN].blue = 1.0; - fgcolors[COLOR_CYAN].alpha = 1.0; - - fgcolors[COLOR_MAGENTA].red = 1.0; - fgcolors[COLOR_MAGENTA].blue = 1.0; - fgcolors[COLOR_MAGENTA].alpha = 1.0; - - fgcolors[COLOR_YELLOW].red = 1.0; - fgcolors[COLOR_YELLOW].green = 1.0; - fgcolors[COLOR_YELLOW].alpha = 1.0; - - fgcolors[COLOR_BLACK].alpha = 1.0; // The other fields are 0 already - - fg_color = gdk_rgba_copy(&fgcolors[COLOR_WHITE]); - bg_color = gdk_rgba_copy(&fgcolors[COLOR_BLACK]); - - for (i = 0; i < GRAYS_MAX; i++) { - grays[i].red = grays[i].green = grays[i].blue = i / (float)(GRAYS_MAX - 1); - grays[i].alpha = 1.0; - } - mainwin = widget; if (current_test->init != NULL) @@ -98,10 +61,9 @@ on_mainwin_button_press_event(GtkWidget *widget, GdkEventButton *event, } break; case 2: - if (++fg_count >= COLOR_MAX) - fg_count = COLOR_WHITE; - gdk_rgba_free(fg_color); - fg_color = gdk_rgba_copy(&fgcolors[fg_count]); + if (++fg_count >= SCREENTEST_COLORS_MAX) + fg_count = SCREENTEST_COLORS_WHITE; + *fg_color = fgcolors[fg_count]; gdk_window_invalidate_rect(gtk_widget_get_window(mainwin), NULL, FALSE); break; case 3: @@ -213,11 +175,3 @@ G_MODULE_EXPORT void on_bg_color_activate(G_GNUC_UNUSED GtkMenuItem *menuitem, } gtk_widget_hide(GTK_WIDGET(bg_color_selector)); } - -void set_color_bg(cairo_t *cr) { - cairo_set_source_rgb(cr, bg_color->red, bg_color->green, bg_color->blue); -} - -void set_color_fg(cairo_t *cr) { - cairo_set_source_rgb(cr, fg_color->red, fg_color->green, fg_color->blue); -} diff --git a/callbacks.h b/callbacks.h index faa14a1..104af49 100644 --- a/callbacks.h +++ b/callbacks.h @@ -32,26 +32,4 @@ struct test_ops { extern struct test_ops basic_ops; -enum test_color { - COLOR_WHITE, - COLOR_RED, - COLOR_GREEN, - COLOR_BLUE, - COLOR_CYAN, - COLOR_MAGENTA, - COLOR_YELLOW, - COLOR_BLACK, - COLOR_MAX -}; - -#define GRAYS_MAX COLOR_MAX - -extern GdkRGBA fgcolors[]; -extern GdkRGBA *fg_color; -extern GdkRGBA *bg_color; -extern GdkRGBA grays[]; - -void set_color_bg(cairo_t *cr); -void set_color_fg(cairo_t *cr); - #endif // SCREENTEST_CALLBACKS_H diff --git a/meson.build b/meson.build index cdf7ed9..7ca4a4c 100644 --- a/meson.build +++ b/meson.build @@ -36,6 +36,7 @@ subdir('po') screentest_srcs = [ 'callbacks.c', + 'screentest_colors.c', 'test_basic.c', 'test_blink.c', 'test_bright_pixels.c', diff --git a/screentest_colors.c b/screentest_colors.c new file mode 100644 index 0000000..132dab0 --- /dev/null +++ b/screentest_colors.c @@ -0,0 +1,61 @@ +/* + * Screentest - CRT/LCD monitor testing utility. + * https://tobix.github.io/screentest/ + * Copyright (C) 2001 Jan "Yenya" Kasprzak + * Copyright (C) 2006-2017 Tobias Gruetzmacher + * Copyright (C) 2021 Apr Thorsten Kattanek + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include "screentest_colors.h" + +#define GRAY_VALUE(idx) \ + ((GdkRGBA){idx / ((float)SCREENTEST_GRAYS_MAX - 1), \ + idx / ((float)SCREENTEST_GRAYS_MAX - 1), \ + idx / ((float)SCREENTEST_GRAYS_MAX - 1), 1.0}) + +const GdkRGBA fgcolors[SCREENTEST_COLORS_MAX] = { + // WHITE + {1.0, 1.0, 1.0, 1.0}, + // RED + {1.0, 0.0, 0.0, 1.0}, + // GREEN + {0.0, 1.0, 0.0, 1.0}, + // BLUE + {0.0, 0.0, 1.0, 1.0}, + // CYAN + {0.0, 1.0, 1.0, 1.0}, + // MAGENTA + {1.0, 0.0, 1.0, 1.0}, + // YELLOW + {1.0, 1.0, 0.0, 1.0}, + // BLACK + {0.0, 0.0, 0.0, 1.0}}; +GdkRGBA bg_col = {0.0, 0.0, 0.0, 1.0}; // fgcolors[SCREENTEST_COLORS_BLACK]; +GdkRGBA fg_col = {1.0, 1.0, 1.0, 1.0}; // fgcolors[SCREENTEST_COLORS_WHITE]; +GdkRGBA *const bg_color = &bg_col; +GdkRGBA *const fg_color = &fg_col; +const GdkRGBA grays[SCREENTEST_GRAYS_MAX] = { + GRAY_VALUE(0), GRAY_VALUE(1), GRAY_VALUE(2), GRAY_VALUE(3), + GRAY_VALUE(4), GRAY_VALUE(5), GRAY_VALUE(6), GRAY_VALUE(7), +}; + +void screentest_set_color_bg(cairo_t *cr) { + cairo_set_source_rgb(cr, bg_color->red, bg_color->green, bg_color->blue); +} + +void screentest_set_color_fg(cairo_t *cr) { + cairo_set_source_rgb(cr, fg_color->red, fg_color->green, fg_color->blue); +} diff --git a/screentest_colors.h b/screentest_colors.h new file mode 100644 index 0000000..3cbc8ac --- /dev/null +++ b/screentest_colors.h @@ -0,0 +1,75 @@ +/* + * Screentest - CRT/LCD monitor testing utility. + * https://tobix.github.io/screentest/ + * Copyright (C) 2001 Jan "Yenya" Kasprzak + * Copyright (C) 2006-2017 Tobias Gruetzmacher + * Copyright (C) 2021 Apr Thorsten Kattanek + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#ifndef SCREENTEST_COLORS_H +#define SCREENTEST_COLORS_H + +#include + +/** + * An array representing a predefined set of colors for screentest + */ +enum ScreentestColors { + SCREENTEST_COLORS_WHITE, + SCREENTEST_COLORS_RED, + SCREENTEST_COLORS_GREEN, + SCREENTEST_COLORS_BLUE, + SCREENTEST_COLORS_CYAN, + SCREENTEST_COLORS_MAGENTA, + SCREENTEST_COLORS_YELLOW, + SCREENTEST_COLORS_BLACK, + SCREENTEST_COLORS_MAX +}; + +/** + * One above the highest index of the `grays` array + */ +#define SCREENTEST_GRAYS_MAX SCREENTEST_COLORS_MAX + +/** + * @brief A pointer to the storage location of the background color (on stack) + */ +extern GdkRGBA *const bg_color; +/** + * @brief An array holding all predefined `ScreentestColors` + */ +extern const GdkRGBA fgcolors[SCREENTEST_COLORS_MAX]; +/** + * @brief A pointer to the storage location of the foreground color (on stack) + */ +extern GdkRGBA *const fg_color; +/** + * @brief An array holding all predefined gray value "colors" + */ +extern const GdkRGBA grays[SCREENTEST_GRAYS_MAX]; + +/** + * @brief Set the source pattern of the given Cairo context to an opaque color + * @param[in,out] cr The Cairo context which is used to draw the background + */ +void screentest_set_color_bg(cairo_t *cr); +/** + * @brief Set the source pattern of the given Cairo context to an opaque color + * @param[in,out] cr The Cairo context which is used to draw the foreground + */ +void screentest_set_color_fg(cairo_t *cr); + +#endif // SCREENTEST_COLORS_H diff --git a/test_basic.c b/test_basic.c index 4183020..f3fec05 100644 --- a/test_basic.c +++ b/test_basic.c @@ -26,14 +26,15 @@ #include "callbacks.h" #include "gettext.h" +#include "screentest_colors.h" #define _(String) gettext(String) #define N_(String) gettext_noop(String) #define BASIC_STEP 40 -static void draw_boxes(cairo_t *cr, GdkRGBA *colors, gint ncols, gint x, gint y, - gint d) { - GdkRGBA *col; +static void draw_boxes(cairo_t *cr, const GdkRGBA *colors, gint ncols, gint x, + gint y, gint d) { + const GdkRGBA *col; int i; for (i = 0; i < ncols; i++) { @@ -45,7 +46,7 @@ static void draw_boxes(cairo_t *cr, GdkRGBA *colors, gint ncols, gint x, gint y, x += d; } - set_color_fg(cr); + screentest_set_color_fg(cr); } static void basic_draw(GtkWidget *widget, cairo_t *cr) { @@ -73,10 +74,10 @@ static void basic_draw(GtkWidget *widget, cairo_t *cr) { pl = pango_cairo_create_layout(cr); - set_color_bg(cr); + screentest_set_color_bg(cr); cairo_paint(cr); - set_color_fg(cr); + screentest_set_color_fg(cr); for (i = ((w - 1) % BASIC_STEP) / 2; i < w; i += BASIC_STEP) cairo_rectangle(cr, i, 0, 1, h); @@ -110,14 +111,14 @@ static void basic_draw(GtkWidget *widget, cairo_t *cr) { maxwidth, 4 * maxheight); cairo_stroke(cr); - set_color_bg(cr); + screentest_set_color_bg(cr); cairo_rectangle(cr, (w - maxwidth) / 2 + 1, d / 2 - 2 * maxheight + 1, maxwidth - 1, 5 * maxheight - 1); cairo_rectangle(cr, (w - maxwidth) / 2 + 1, h - d / 2 - 2 * maxheight + 1, maxwidth - 1, 4 * maxheight - 1); cairo_fill(cr); - set_color_fg(cr); + screentest_set_color_fg(cr); cairo_move_to(cr, (w - widths[0]) / 2, d / 2 - 4 * maxheight / 3); pango_layout_set_text(pl, gettext(text[0]), -1); @@ -142,9 +143,10 @@ static void basic_draw(GtkWidget *widget, cairo_t *cr) { pango_cairo_show_layout(cr, pl); b = 7 * d / 4; - draw_boxes(cr, fgcolors, COLOR_MAX, (w - b) / 2, h / 2 - b / COLOR_MAX, - b / COLOR_MAX); - draw_boxes(cr, grays, GRAYS_MAX, (w - b) / 2, h / 2, b / GRAYS_MAX); + draw_boxes(cr, fgcolors, SCREENTEST_COLORS_MAX, (w - b) / 2, + h / 2 - b / SCREENTEST_COLORS_MAX, b / SCREENTEST_COLORS_MAX); + draw_boxes(cr, grays, SCREENTEST_GRAYS_MAX, (w - b) / 2, h / 2, + b / SCREENTEST_GRAYS_MAX); cairo_arc(cr, 0 + d / 2 + 0.5, 0 + d / 2 + 0.5, d / 2, 0, 2 * G_PI); // Upper left diff --git a/test_blink.c b/test_blink.c index c6c7b45..73e079d 100644 --- a/test_blink.c +++ b/test_blink.c @@ -23,6 +23,7 @@ #include #include "callbacks.h" +#include "screentest_colors.h" static guint timeout; @@ -40,17 +41,17 @@ static void blink_draw(GtkWidget *widget, cairo_t *cr) { w = gdk_window_get_width(win); if (blink_step) { - set_color1 = set_color_bg; - set_color2 = set_color_fg; + set_color1 = screentest_set_color_bg; + set_color2 = screentest_set_color_fg; } else { - set_color1 = set_color_fg; - set_color2 = set_color_bg; + set_color1 = screentest_set_color_fg; + set_color2 = screentest_set_color_bg; } - set_color_fg(cr); + screentest_set_color_fg(cr); cairo_paint(cr); - set_color_bg(cr); + screentest_set_color_bg(cr); cairo_rectangle(cr, 1, 1, w - 2, h - 2); cairo_fill(cr); diff --git a/test_bright_pixels.c b/test_bright_pixels.c index c643a63..c0fe12f 100644 --- a/test_bright_pixels.c +++ b/test_bright_pixels.c @@ -24,10 +24,12 @@ #include #include "callbacks.h" +#include "screentest_colors.h" #define COLOR_COUNT 5 -static const int color_cycle[COLOR_COUNT] = {COLOR_RED, COLOR_GREEN, COLOR_BLUE, - COLOR_WHITE, COLOR_BLACK}; +static const int color_cycle[COLOR_COUNT] = { + SCREENTEST_COLORS_RED, SCREENTEST_COLORS_GREEN, SCREENTEST_COLORS_BLUE, + SCREENTEST_COLORS_WHITE, SCREENTEST_COLORS_BLACK}; static int current_color_idx; static void bright_pixels_init(G_GNUC_UNUSED GtkWidget *widget) { @@ -41,7 +43,7 @@ static void bright_pixels_cycle(G_GNUC_UNUSED GtkWidget *widget) { } static void bright_pixels_draw(GtkWidget *widget, cairo_t *cr) { - GdkRGBA *col; + const GdkRGBA *col; col = &fgcolors[color_cycle[current_color_idx]]; cairo_set_source_rgb(cr, col->red, col->green, col->blue); diff --git a/test_gradient.c b/test_gradient.c index de09003..9cc9c03 100644 --- a/test_gradient.c +++ b/test_gradient.c @@ -20,6 +20,7 @@ */ #include "callbacks.h" +#include "screentest_colors.h" enum GRADIENT_ORIENTATION { GO_TOP_LEFT_TO_RIGHT_BOTTOM, diff --git a/test_grid.c b/test_grid.c index cdcb804..10a9a52 100644 --- a/test_grid.c +++ b/test_grid.c @@ -23,6 +23,7 @@ #include #include "callbacks.h" +#include "screentest_colors.h" #define GRID_STEP 64 #define GRID_STEP_MIN 2 @@ -47,10 +48,10 @@ static void grid_draw(GtkWidget *widget, cairo_t *cr) { h = gdk_window_get_height(win); w = gdk_window_get_width(win); - set_color_bg(cr); + screentest_set_color_bg(cr); cairo_paint(cr); - set_color_fg(cr); + screentest_set_color_fg(cr); for (i = ((w - 1) % grid_step) / 2; i < w; i += grid_step) { cairo_rectangle(cr, i, 0, 1, h); } diff --git a/test_horizontal.c b/test_horizontal.c index a4ebb8c..8d7b785 100644 --- a/test_horizontal.c +++ b/test_horizontal.c @@ -23,6 +23,7 @@ #include #include "callbacks.h" +#include "screentest_colors.h" #define GRID_STEP 64 #define GRID_STEP_MIN 2 @@ -47,10 +48,10 @@ static void horizontal_draw(GtkWidget *widget, cairo_t *cr) { h = gdk_window_get_height(win); w = gdk_window_get_width(win); - set_color_bg(cr); + screentest_set_color_bg(cr); cairo_paint(cr); - set_color_fg(cr); + screentest_set_color_fg(cr); for (i = ((h - 1) % horizontal_step) / 2; i < h; i += horizontal_step) { cairo_rectangle(cr, 0, i, w, 1); } diff --git a/test_lcdalign.c b/test_lcdalign.c index b028b07..15bcde7 100644 --- a/test_lcdalign.c +++ b/test_lcdalign.c @@ -22,6 +22,7 @@ #include #include "callbacks.h" +#include "screentest_colors.h" static void lcdalign_draw(GtkWidget *widget, cairo_t *cr) { GdkWindow *win = gtk_widget_get_window(widget); @@ -33,11 +34,11 @@ static void lcdalign_draw(GtkWidget *widget, cairo_t *cr) { w = gdk_window_get_width(win); /* Background/Border */ - set_color_fg(cr); + screentest_set_color_fg(cr); cairo_paint(cr); /* Pattern */ - set_color_bg(cr); + screentest_set_color_bg(cr); cairo_set_line_width(cr, 1.0); cairo_set_dash(cr, d, 1, 0); for (i = 1; i < h - 1; i += 2) { diff --git a/test_text.c b/test_text.c index 02e2e89..08c4091 100644 --- a/test_text.c +++ b/test_text.c @@ -24,6 +24,7 @@ #include #include "callbacks.h" +#include "screentest_colors.h" static gint font_sizes[] = {8, 10, 12, 14, 18, 24}; @@ -58,10 +59,10 @@ static void text_draw(GtkWidget *widget, cairo_t *cr) { pango_layout_set_justify(pl, TRUE); pango_layout_set_width(pl, pango_units_from_double(w)); - set_color_bg(cr); + screentest_set_color_bg(cr); cairo_paint(cr); - set_color_fg(cr); + screentest_set_color_fg(cr); pango_layout_set_text(pl, text, -1); pango_layout_get_extents(pl, &ink_rect, NULL); diff --git a/test_vertical.c b/test_vertical.c index 21d3aa9..88a0031 100644 --- a/test_vertical.c +++ b/test_vertical.c @@ -23,6 +23,7 @@ #include #include "callbacks.h" +#include "screentest_colors.h" #define GRID_STEP 64 #define GRID_STEP_MIN 2 @@ -47,10 +48,10 @@ static void vertical_draw(GtkWidget *widget, cairo_t *cr) { h = gdk_window_get_height(win); w = gdk_window_get_width(win); - set_color_bg(cr); + screentest_set_color_bg(cr); cairo_paint(cr); - set_color_fg(cr); + screentest_set_color_fg(cr); for (i = ((w - 1) % vertical_step) / 2; i < w; i += vertical_step) { cairo_rectangle(cr, i, 0, 1, h); }