Skip to content

Commit

Permalink
Rename color handling symbols and move out
Browse files Browse the repository at this point in the history
  • Loading branch information
markuspg committed Mar 27, 2024
1 parent 5dc1890 commit 8fa66b7
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 97 deletions.
56 changes: 6 additions & 50 deletions callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,56 +28,21 @@
#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;
}
screentest_colors_init();

mainwin = widget;

Expand All @@ -98,10 +63,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:
Expand Down Expand Up @@ -213,11 +177,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);
}
21 changes: 0 additions & 21 deletions callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +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 grays[];

void set_color_bg(cairo_t *cr);
void set_color_fg(cairo_t *cr);

#endif // SCREENTEST_CALLBACKS_H
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ subdir('po')

screentest_srcs = [
'callbacks.c',
'screentest_colors.c',
'test_basic.c',
'test_blink.c',
'test_bright_pixels.c',
Expand Down
79 changes: 79 additions & 0 deletions screentest_colors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Screentest - CRT/LCD monitor testing utility.
* https://tobix.github.io/screentest/
* Copyright (C) 2001 Jan "Yenya" Kasprzak <[email protected]>
* Copyright (C) 2006-2017 Tobias Gruetzmacher <[email protected]>
* Copyright (C) 2021 Apr Thorsten Kattanek <[email protected]>
*
* 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"

GdkRGBA bg_col = {0};
GdkRGBA fg_col = {0};
GdkRGBA fgcolors[SCREENTEST_COLORS_MAX] = {0};
GdkRGBA *const bg_color = &bg_col;
GdkRGBA *const fg_color = &fg_col;
GdkRGBA grays[SCREENTEST_GRAYS_MAX] = {0};

void screentest_colors_init() {
gint i;

fgcolors[SCREENTEST_COLORS_WHITE].red =
fgcolors[SCREENTEST_COLORS_WHITE].green =
fgcolors[SCREENTEST_COLORS_WHITE].blue =
fgcolors[SCREENTEST_COLORS_WHITE].alpha = 1.0;

fgcolors[SCREENTEST_COLORS_RED].red = fgcolors[SCREENTEST_COLORS_RED].alpha =
1.0;

fgcolors[SCREENTEST_COLORS_GREEN].green =
fgcolors[SCREENTEST_COLORS_GREEN].alpha = 1.0;

fgcolors[SCREENTEST_COLORS_BLUE].blue =
fgcolors[SCREENTEST_COLORS_BLUE].alpha = 1.0;

fgcolors[SCREENTEST_COLORS_CYAN].green = 1.0;
fgcolors[SCREENTEST_COLORS_CYAN].blue = 1.0;
fgcolors[SCREENTEST_COLORS_CYAN].alpha = 1.0;

fgcolors[SCREENTEST_COLORS_MAGENTA].red = 1.0;
fgcolors[SCREENTEST_COLORS_MAGENTA].blue = 1.0;
fgcolors[SCREENTEST_COLORS_MAGENTA].alpha = 1.0;

fgcolors[SCREENTEST_COLORS_YELLOW].red = 1.0;
fgcolors[SCREENTEST_COLORS_YELLOW].green = 1.0;
fgcolors[SCREENTEST_COLORS_YELLOW].alpha = 1.0;

fgcolors[SCREENTEST_COLORS_BLACK].alpha =
1.0; // The other fields are 0 already

*fg_color = fgcolors[SCREENTEST_COLORS_WHITE];
*bg_color = fgcolors[SCREENTEST_COLORS_BLACK];

for (i = 0; i < SCREENTEST_GRAYS_MAX; i++) {
grays[i].red = grays[i].green = grays[i].blue =
i / (float)(SCREENTEST_GRAYS_MAX - 1);
grays[i].alpha = 1.0;
}
}

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);
}
80 changes: 80 additions & 0 deletions screentest_colors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Screentest - CRT/LCD monitor testing utility.
* https://tobix.github.io/screentest/
* Copyright (C) 2001 Jan "Yenya" Kasprzak <[email protected]>
* Copyright (C) 2006-2017 Tobias Gruetzmacher <[email protected]>
* Copyright (C) 2021 Apr Thorsten Kattanek <[email protected]>
*
* 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 <gtk/gtk.h>

/**
* 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 GdkRGBA fgcolors[];
/**
* @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 GdkRGBA grays[];

/**
* @brief Initialize the color storage locations and their default values
*/
void screentest_colors_init(void);

/**
* @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
18 changes: 10 additions & 8 deletions test_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "callbacks.h"
#include "gettext.h"
#include "screentest_colors.h"
#define _(String) gettext(String)
#define N_(String) gettext_noop(String)

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
13 changes: 7 additions & 6 deletions test_blink.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <gtk/gtk.h>

#include "callbacks.h"
#include "screentest_colors.h"

static guint timeout;

Expand All @@ -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);

Expand Down
6 changes: 4 additions & 2 deletions test_bright_pixels.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
#include <stdio.h>

#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) {
Expand Down
Loading

0 comments on commit 8fa66b7

Please sign in to comment.