aboutsummaryrefslogtreecommitdiff
path: root/src/colwheel.c
blob: 95f551c86cf8af212d95dfc427f86c2922ae852e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * colwheel.c
 *
 * Colour wheel definition and visualisation
 *
 * (c) 2006-2007 Thomas White <taw27@cam.ac.uk>
 *
 *  Synth2D - two-dimensional Fourier synthesis
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include <math.h>
#include <stdlib.h>
#include <fftw3.h>

#include "displaywindow.h"

#define COLWHEEL_WIDTH 256
#define COLWHEEL_SIZE ((COLWHEEL_WIDTH)-1)
#define COLWHEEL_HALF ((COLWHEEL_WIDTH/2)-1)

#define rad2deg(a) ((a)*180.0/M_PI)
#define deg2rad(a) ((a)*M_PI/180.0)

gint colwheel_show(GtkWidget *widget, gpointer data) {

	GtkWidget *colwheel_window;
	GdkPixbuf *colwheel_pixbuf;
	GtkWidget *colwheel_pixmap_widget;
	fftw_complex *colwheel;

	unsigned int x, y;
	double am, ph;

	/* This isn't being transformed, so no need to use fftw_malloc() */
	colwheel = malloc(COLWHEEL_SIZE*COLWHEEL_SIZE*sizeof(fftw_complex));
	for ( x=0; x<COLWHEEL_SIZE; x++ ) {
		for ( y=0; y<COLWHEEL_SIZE; y++ ) {
			colwheel[y + COLWHEEL_SIZE*x][0] = 0;
			colwheel[y + COLWHEEL_SIZE*x][1] = 0;
		}
	}

	/* Plot out a disc containing smoothly varying amplitude and phase */
	for ( am=0.0; am<1.0; am+=(1.0/COLWHEEL_SIZE) ) {
		for ( ph=0.0; ph<2.0*M_PI; ph+=1.0/(2.0*M_PI*(double)COLWHEEL_SIZE) ) {
			x = (unsigned int)(COLWHEEL_HALF + (COLWHEEL_HALF*am*cos(ph)));
			y = (unsigned int)(COLWHEEL_HALF + (COLWHEEL_HALF*am*sin(ph)));
			colwheel[(COLWHEEL_SIZE-1-y) + COLWHEEL_SIZE*(COLWHEEL_SIZE-1-x)][0] = am*cos(ph);
			colwheel[(COLWHEEL_SIZE-1-y) + COLWHEEL_SIZE*(COLWHEEL_SIZE-1-x)][1] = am*sin(ph);
		}
	}

	/* Draw a line to remind the user where zero phase is */
	for ( x=COLWHEEL_HALF+1; x<COLWHEEL_SIZE; x++ ) {
		colwheel[COLWHEEL_HALF + COLWHEEL_SIZE*(COLWHEEL_SIZE-1-x)][0] = 0;
		colwheel[COLWHEEL_HALF + COLWHEEL_SIZE*(COLWHEEL_SIZE-1-x)][1] = 0;
	}

	colwheel_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(colwheel_window), "Colour Wheel");
	colwheel_pixbuf = displaywindow_render_pixbuf(colwheel, 1, COLWHEEL_SIZE, COLWHEEL_SIZE, M_PI_2, 1, 1);
	free(colwheel);
	colwheel_pixmap_widget = gtk_image_new_from_pixbuf(colwheel_pixbuf);
	gtk_container_add(GTK_CONTAINER(colwheel_window), colwheel_pixmap_widget);
	gtk_widget_show_all(colwheel_window);

	return 0;

}

double colwheel_blue(double am, double ph) {

	double f;
	unsigned int hi;
	double d;

	ph = rad2deg(ph);		/* Convert to degrees */
	ph += 180.0;			/* Rotation of colour wheel */
	if ( ph >= 360.0 ) ph = 0.0;
	d = ph / 60.0;

	hi = floor(d);	/* Divide the colour wheel into six sections */
	f = d - hi;	/* Distance into the current section of the colour wheel */
	if ( hi == 6 ) hi = 0;
	switch ( hi ) {
		case 0 : return am;
		case 1 : return am;
		case 2 : return am*(1.0-f);
		case 3 : return 0.0;
		case 4 : return am*f/2.0;
		case 5 : return am/2.0+am*f/2.0;
		default : return 0.0;
	}

}

double colwheel_green(double am, double ph) {

	double f;
	unsigned int hi;
	double d;

	ph = rad2deg(ph);		/* Convert to degrees */
	ph += 180.0;			/* Rotation of colour wheel */
	if ( ph >= 360.0 ) ph = 0.0;
	d = ph / 60.0;

	hi = floor(d);	/* Divide the colour wheel into six sections */
	f = d - hi;	/* Distance into the current section of the colour wheel */
	if ( hi == 6 ) hi = 0;
	switch ( hi ) {
		case 0 : return am*f/2.0;
		case 1 : return am/2.0+am*f/2.0;
		case 2 : return am;
		case 3 : return am*(1.0-f);
		case 4 : return 0.0;
		case 5 : return 0.0;
		default : return 0.0;
	}

}

double colwheel_red(double am, double ph) {

	double f;
	unsigned int hi;
	double d;

	ph = rad2deg(ph);		/* Convert to degrees */
	ph += 180.0;			/* Rotation of colour wheel */
	if ( ph >= 360.0 ) ph = 0.0;
	d = ph / 60.0;

	hi = floor(d);	/* Divide the colour wheel into six sections */
	f = d - hi;	/* Distance into the current section of the colour wheel */
	if ( hi == 6 ) hi = 0;
	switch ( hi ) {
		case 0 : return 0.0;
		case 1 : return 0.0;
		case 2 : return 0.0;
		case 3 : return am*f;
		case 4 : return am;
		case 5 : return am*(1.0-f);
		default : return 0.0;
	}

}