Skip to content

Commit

Permalink
Merge pull request #16 from markuspg/markuspg/refactor_color_handling
Browse files Browse the repository at this point in the history
Move colors and their handling in own translation unit and refactor them slightly
  • Loading branch information
TobiX committed Apr 17, 2024
2 parents a32410e + 184c5b7 commit f30e3fc
Show file tree
Hide file tree
Showing 16 changed files with 2,999 additions and 103 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/build-aux/
/config.*
/configure
/doc/html/
/libtool
/po/POTFILES
Makefile
Expand Down
2,815 changes: 2,815 additions & 0 deletions Doxyfile

Large diffs are not rendered by default.

56 changes: 5 additions & 51 deletions callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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);
}
22 changes: 0 additions & 22 deletions callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
61 changes: 61 additions & 0 deletions screentest_colors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Screentest - CRT/LCD monitor testing utility.
* https://tobix.github.io/screentest/
* Copyright (C) 2001 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
* Copyright (C) 2006-2017 Tobias Gruetzmacher <tobias-screentest@23.gs>
* Copyright (C) 2021 Apr Thorsten Kattanek <thorsten.kattanek@gmx.de>
*
* 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);
}
75 changes: 75 additions & 0 deletions screentest_colors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Screentest - CRT/LCD monitor testing utility.
* https://tobix.github.io/screentest/
* Copyright (C) 2001 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
* Copyright (C) 2006-2017 Tobias Gruetzmacher <tobias-screentest@23.gs>
* Copyright (C) 2021 Apr Thorsten Kattanek <thorsten.kattanek@gmx.de>
*
* 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 <gdk/gdk.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 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
24 changes: 13 additions & 11 deletions test_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
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
8 changes: 5 additions & 3 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 All @@ -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);
Expand Down
Loading

0 comments on commit f30e3fc

Please sign in to comment.