summaryrefslogtreecommitdiff
path: root/app/wlib/gtklib/gtkcolor.c
blob: cf171990f48a924fbcea199a692c2e4bbcfc91f0 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/** \file gtkcolor.c
 * code for the color selection dialog and color button
 *
 * $Header: /home/dmarkle/xtrkcad-fork-cvs/xtrkcad/app/wlib/gtklib/gtkcolor.c,v 1.3 2007-11-24 19:48:21 tshead Exp $
 */

/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2005 Dave Bullis
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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 <assert.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <unistd.h>
#include <stdlib.h>

#include "gtkint.h"

#include "square10.bmp"

EXPORT wDrawColor wDrawColorWhite;
EXPORT wDrawColor wDrawColorBlack;

#define RGB(R,G,B) ( ((long)((R)&0xFF)<<16) | ((long)((G)&0xFF)<<8) | ((long)((B)&0xFF)) )

#define MAX_COLOR_DISTANCE (3)

typedef struct {
		unsigned char red;
		unsigned char green;
		unsigned char blue;
		GdkColor normalColor;
		GdkColor invertColor;
		long rgb;
		int colorChar;
		} colorMap_t;
static GArray *colorMap_garray = NULL; // Change to use glib array

static colorMap_t colorMap[] = {
		{ 255, 255, 255 },	/* White */
		{   0,   0,   0 },	/* Black */
		{ 255,   0,   0 },	/* Red */
		{   0, 255,   0 },	/* Green */
		{   0,   0, 255 },	/* Blue */
		{ 255, 255,   0 },	/* Yellow */
		{ 255,   0, 255 },	/* Purple */
		{   0, 255, 255 },	/* Aqua */
		{ 128,   0,   0 },	/* Dk. Red */
		{   0, 128,   0 },	/* Dk. Green */
		{   0,   0, 128 },	/* Dk. Blue */
		{ 128, 128,   0 },	/* Dk. Yellow */
		{ 128,   0, 128 },	/* Dk. Purple */
		{   0, 128, 128 },	/* Dk. Aqua */
		{  65, 105, 225 },      /* Royal Blue */
		{   0, 191, 255 },	/* DeepSkyBlue */
		{ 125, 206, 250 },	/* LightSkyBlue */
		{  70, 130, 180 },	/* Steel Blue */
		{ 176, 224, 230 },	/* Powder Blue */
		{ 127, 255, 212 },	/* Aquamarine */
		{  46, 139,  87 },	/* SeaGreen */
		{ 152, 251, 152 },	/* PaleGreen */
		{ 124, 252,   0 },	/* LawnGreen */
		{  50, 205,  50 },	/* LimeGreen */
		{  34, 139,  34 },	/* ForestGreen */
		{ 255, 215,   0 },	/* Gold */
		{ 188, 143, 143 },	/* RosyBrown */
		{ 139, 69, 19 },	/* SaddleBrown */
		{ 245, 245, 220 },	/* Beige */
		{ 210, 180, 140 },	/* Tan */
		{ 210, 105, 30 },	/* Chocolate */
		{ 165, 42, 42 },	/* Brown */
		{ 255, 165, 0 },	/* Orange */
		{ 255, 127, 80 },	/* Coral */
		{ 255, 99, 71 },	/* Tomato */
		{ 255, 105, 180 },	/* HotPink */
		{ 255, 192, 203 },	/* Pink */
		{ 176, 48, 96 },	/* Maroon */
		{ 238, 130, 238 },	/* Violet */
		{ 160, 32, 240 },	/* Purple */
		{  16,  16,  16 },	/* Gray */
		{  32,  32,  32 },	/* Gray */
		{  48,  48,  48 },	/* Gray */
		{  64,  64,  64 },	/* Gray */
		{  80,  80,  80 },	/* Gray */
		{  96,  96,  96 },	/* Gray */
 	    { 112, 112, 122 },	/* Gray */
		{ 128, 128, 128 },	/* Gray */
		{ 144, 144, 144 },	/* Gray */
		{ 160, 160, 160 },	/* Gray */
		{ 176, 176, 176 },	/* Gray */
		{ 192, 192, 192 },	/* Gray */
		{ 208, 208, 208 },	/* Gray */
		{ 224, 224, 224 },	/* Gray */
		{ 240, 240, 240 },	/* Gray */
		{   0,   0,   0 }	/* BlackPixel */
 	};

#define NUM_GRAYS (16)

static GdkColormap * gtkColorMap;

static char lastColorChar = '!';

/*****************************************************************************
 *
 * 
 *
 */


EXPORT wDrawColor wDrawColorGray(
		int percent )
{
	int n;
	long rgb;
	n = (percent * (NUM_GRAYS+1)) / 100;
	if ( n <= 0 )
		return wDrawColorBlack;
	else if ( n > NUM_GRAYS )
		return wDrawColorWhite;
	else {
		n = (n*256)/NUM_GRAYS;
		rgb = RGB( n, n, n );
		return wDrawFindColor( rgb );
	}
}


void gtkGetColorMap( void )
{
	if (gtkColorMap)
		return;
	gtkColorMap = gtk_widget_get_colormap( gtkMainW->widget );
	return;
}

void init_colorMapValue( colorMap_t * t) {
    
    t->rgb = RGB( t->red, t->green, t->blue );
    t->normalColor.red = t->red*65535/255;
    t->normalColor.green = t->green*65535/255;
    t->normalColor.blue = t->blue*65535/255;
    gdk_color_alloc( gtkColorMap, &t->normalColor );
    t->invertColor = t->normalColor;
    t->invertColor.pixel ^= g_array_index(colorMap_garray, colorMap_t, wDrawColorWhite).normalColor.pixel;
    t->colorChar = lastColorChar++;
    if (lastColorChar >= 0x7F)
        lastColorChar = '!'+1;
    else if (lastColorChar == '"')
        lastColorChar++;
    
}


void init_colorMap( void )
{
    colorMap_garray = g_array_sized_new(TRUE, TRUE, sizeof(colorMap_t), sizeof(colorMap)/sizeof(colorMap_t));
    g_array_append_vals(colorMap_garray, &colorMap, sizeof(colorMap)/sizeof(colorMap_t));
    
    int gint;
    
    for(gint=0; gint<colorMap_garray->len; gint++) {
        init_colorMapValue(&g_array_index(colorMap_garray, colorMap_t, gint));
    }
}


EXPORT wDrawColor wDrawFindColor(
		long rgb0 )
{
	wDrawColor cc;
	int r0, g0, b0;
	int d0, d1;
	long rgb1;
	colorMap_t * cm_p;

	gtkGetColorMap();

	cc = wDrawColorBlack;
	r0 = (int)(rgb0>>16)&0xFF;
	g0 = (int)(rgb0>>8)&0xFF;
	b0 = (int)(rgb0)&0xFF;
	d0 = 256*3;
    
    // Initialize garray if needed
    if (colorMap_garray == NULL) {
        init_colorMap();
    }
    
    int gint;
    
    // Iterate over entire garray
    for (gint=0; gint<colorMap_garray->len; gint++) {
        cm_p = &g_array_index(colorMap_garray, colorMap_t, gint);
        rgb1 = cm_p->rgb;
        d1 = abs(r0-cm_p->red) + abs(g0-cm_p->green) + abs(b0-cm_p->blue);
        if (d1 == 0)
            return gint;
        if (d1 < d0) {
			d0 = d1;
		  	cc = gint;
        }
	  }
    if (d0 <= MAX_COLOR_DISTANCE) {
        return cc;
    }
    // No good value - so add one
    colorMap_t tempMapValue;
	//DYNARR_APPEND( colorMap_t, colorMap_da, 10 );
	tempMapValue.red = r0;
	tempMapValue.green = g0;
	tempMapValue.blue = b0;
    init_colorMapValue(&tempMapValue);
    g_array_append_val(colorMap_garray,tempMapValue);
	return gint;
}


EXPORT long wDrawGetRGB(
		wDrawColor color )
{
	gtkGetColorMap();
    
    if(colorMap_garray == NULL)
        init_colorMap();

	if (color < 0 || color > colorMap_garray->len)
		abort();
    colorMap_t * colorMap_e;
    colorMap_e = &g_array_index(colorMap_garray, colorMap_t, color);
	return colorMap_e->rgb;
}


EXPORT GdkColor* gtkGetColor(
		wDrawColor color,
		wBool_t normal )
{
	gtkGetColorMap();
    
    if(colorMap_garray == NULL)
        init_colorMap();

	if (color < 0 || color > colorMap_garray->len)
		abort();
    colorMap_t * colorMap_e;
    colorMap_e = &g_array_index(colorMap_garray, colorMap_t, color);
	
    if ( normal )
		return &colorMap_e->normalColor;
	else
		return &colorMap_e->invertColor;
}


EXPORT int gtkGetColorChar(
		wDrawColor color )
{
	/*gtkGetColorMap();*/
    if(colorMap_garray == NULL)
        init_colorMap();

	if (color < 0 || color > colorMap_garray->len)
		abort();
    colorMap_t * colorMap_e;
    colorMap_e = &g_array_index(colorMap_garray, colorMap_t, color);
	return colorMap_e->colorChar;
}


EXPORT int gtkMapPixel( 
		long pixel )
{
	colorMap_t * mapValue;
    int gint;
    
    if(colorMap_garray == NULL)
        init_colorMap();
	
	for (gint=0; gint<colorMap_garray->len; gint++ ) {
        mapValue = &g_array_index(colorMap_garray, colorMap_t, gint);
        if ( mapValue->normalColor.pixel == pixel ) {
			return mapValue->colorChar;
		}
	}
    mapValue = &g_array_index(colorMap_garray, colorMap_t, wDrawColorBlack);
	return mapValue->colorChar;
}


/*
 *****************************************************************************
 *
 * Color Selection Dialog
 *
 *****************************************************************************
 */


static int colorSelectValid;
static int colorSelectOk(
		GtkWidget * widget,
		GtkWidget * * window )
{
	gtkDoModal( NULL, FALSE );
	gtk_widget_hide( GTK_WIDGET(*window) );
	colorSelectValid = TRUE;
	return FALSE;
}


static int colorSelectCancel(
		GtkWidget * widget,
		GtkWidget * * window )
{
	gtkDoModal( NULL, FALSE );
	gtk_widget_hide( GTK_WIDGET(*window) );
	colorSelectValid = FALSE;
	if (widget == *window)
		/* Called by destroy event, window is gone */
		*window = NULL;
	return FALSE;
}


EXPORT wBool_t wColorSelect(
		const char * title,
		wDrawColor * color )
{
	static GtkWidget * colorSelectD = NULL;
	long rgb;
	gdouble colors[4];  // Remember opacity!

	if (colorSelectD == NULL) {
		colorSelectD = gtk_color_selection_dialog_new( title );
		gtk_signal_connect( GTK_OBJECT(GTK_COLOR_SELECTION_DIALOG(colorSelectD)->ok_button), "clicked", (GtkSignalFunc)colorSelectOk, (gpointer)&colorSelectD );
		gtk_signal_connect( GTK_OBJECT(GTK_COLOR_SELECTION_DIALOG(colorSelectD)->cancel_button), "clicked", (GtkSignalFunc)colorSelectCancel, (gpointer)&colorSelectD );
		gtk_signal_connect( GTK_OBJECT(colorSelectD), "destroy", (GtkSignalFunc)colorSelectCancel, (gpointer)&colorSelectD );
	} else {
		gtk_window_set_title( GTK_WINDOW(colorSelectD), title );
	}
    
    colorMap_t * colorMap_e;
    
    if (!colorMap_garray) {
        init_colorMap();
    }
    
    colorMap_e = &g_array_index(colorMap_garray, colorMap_t, *color);
    colors[0] = colorMap_e->red/255.0;
	colors[1] = colorMap_e->green/255.0;
	colors[2] = colorMap_e->blue/255.0;
    colors[3] = 1.0;                   // Override to Fully opaque
	gtk_color_selection_set_color( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorSelectD)->colorsel), colors );
	gtk_widget_show( colorSelectD );
	gtkDoModal( NULL, TRUE );
	if (colorSelectValid) {
		gtk_color_selection_get_color( GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(colorSelectD)->colorsel), colors );
		rgb = RGB( (int)(colors[0]*255), (int)(colors[1]*255), (int)(colors[2]*255) );
        * color = wDrawFindColor( rgb );
		return TRUE;
	}
	return FALSE;
}


/*
 *****************************************************************************
 *
 * Color Selection Button
 *
 *****************************************************************************
 */

typedef struct {
		wDrawColor * valueP;
		wColorSelectButtonCallBack_p action;
		const char * labelStr;
		void * data;
		wDrawColor color;
		wButton_p button;
		} colorData_t;

static void doColorButton(
		void * data )
{
	colorData_t * cd = (colorData_t *)data; 
	wDrawColor newColor;

	newColor = cd->color;
	if (wColorSelect( cd->labelStr, &newColor )) {
		cd->color = newColor; 
		wColorSelectButtonSetColor( cd->button, newColor );
		if (cd->valueP)
			*(cd->valueP) = newColor;
		if (cd->action)
			cd->action( cd->data, newColor ); 
	}
}


void wColorSelectButtonSetColor(
		wButton_p bb,
		wDrawColor color )
{
	wIcon_p bm;
	bm = wIconCreateBitMap( square10_width, square10_height, square10_bits, color );
	wButtonSetLabel( bb, (const char*)bm );
	((colorData_t*)((wControl_p)bb)->data)->color = color;
}


wDrawColor wColorSelectButtonGetColor(
		wButton_p bb )
{
	return ((colorData_t*)((wControl_p)bb)->data)->color;
}

/** Create the button showing the current paint color and starting the color selection dialog. 
 * \param IN parent parent window
 * \param IN x x coordinate
 * \param IN Y y coordinate
 * \param IN helpStr balloon help string 
 * \param IN labelStr Button label ???
 * \param IN option
 * \param IN width
 * \param IN valueP Current color ??? 
 * \param IN action Button callback procedure
 * \param IN data ???
 * \return bb handle for created button
 */
 
wButton_p wColorSelectButtonCreate(
		wWin_p	parent,
		wPos_t	x,
		wPos_t	y,
		const char 	* helpStr,
		const char	* labelStr,
		long 	option,
		wPos_t 	width,
		wDrawColor *valueP,
		wColorSelectButtonCallBack_p action,
		void 	* data )
{
	wButton_p bb;
	wIcon_p bm;
	colorData_t * cd;
	bm = wIconCreateBitMap( square10_width, square10_height, square10_bits, (valueP?*valueP:0) );
	cd = malloc( sizeof( colorData_t ));
	cd->valueP = valueP;
	cd->action = action;
	cd->data = data;
	cd->labelStr = labelStr;
	cd->color = (valueP?*valueP:0);
	bb = wButtonCreate( parent, x, y, helpStr, (const char*)bm, option|BO_ICON, width, doColorButton, cd );
	cd->button = bb;
	if (labelStr)
		((wControl_p)bb)->labelW = gtkAddLabel( (wControl_p)bb, labelStr );
	return bb;
}