aboutsummaryrefslogtreecommitdiff
path: root/src/color.c
blob: 1ed527ee1bd7b76cffa956c7460cdb76d9beee4a (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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/****************************************************************************
 * Functions to handle loading colors.
 * Copyright (C) 2004 Joe Wingbermuehle
 ****************************************************************************/

#include "jwm.h"
#include "main.h"
#include "color.h"
#include "error.h"
#include "misc.h"

typedef struct {
	ColorType type;
	const char *value;
} DefaultColorNode;

static const float COLOR_DELTA = 0.45;

unsigned long colors[COLOR_COUNT];
static unsigned long rgbColors[COLOR_COUNT];

static unsigned long *map;

#ifdef USE_XFT
static XftColor *xftColors[COLOR_COUNT] = { NULL };
#endif

static DefaultColorNode DEFAULT_COLORS[] = {
	{ COLOR_BORDER_BG,          "gray"    },
	{ COLOR_BORDER_FG,          "black"   },
	{ COLOR_BORDER_ACTIVE_BG,   "red"     },
	{ COLOR_BORDER_ACTIVE_FG,   "white"   },
	{ COLOR_TRAY_BG,            "gray"    },
	{ COLOR_TRAY_FG,            "black"   },
	{ COLOR_TASK_BG,            "gray"    },
	{ COLOR_TASK_FG,            "black"   },
	{ COLOR_TASK_ACTIVE_BG,     "red"     },
	{ COLOR_TASK_ACTIVE_FG,     "white"   },
	{ COLOR_PAGER_BG,           "black"   },
	{ COLOR_PAGER_FG,           "gray"    },
	{ COLOR_PAGER_ACTIVE_BG,    "red"     },
	{ COLOR_PAGER_ACTIVE_FG,    "red"     },
	{ COLOR_PAGER_OUTLINE,      "black"   },
	{ COLOR_MENU_BG,            "gray"    },
	{ COLOR_MENU_FG,            "black"   },
	{ COLOR_MENU_ACTIVE_BG,     "red"     },
	{ COLOR_MENU_ACTIVE_FG,     "white"   },
	{ COLOR_POPUP_BG,           "yellow"  },
	{ COLOR_POPUP_FG,           "black"   },
	{ COLOR_POPUP_OUTLINE,      "black"   },
	{ COLOR_TRAYBUTTON_FG,      "black"   },
	{ COLOR_TRAYBUTTON_BG,      "gray"    },
	{ COLOR_CLOCK_FG,           "black"   },
	{ COLOR_CLOCK_BG,           "gray"    },
	{ COLOR_COUNT,              NULL      }
};

static char **names = NULL;

static unsigned long redShift;
static unsigned long greenShift;
static unsigned long blueShift;
static unsigned long redMask;
static unsigned long greenMask;
static unsigned long blueMask;

static void ComputeShiftMask(unsigned long maskIn,
	unsigned long *shiftOut, unsigned long *maskOut);

static void GetDirectPixel(XColor *c);
static void GetMappedPixel(XColor *c);

static int ParseColor(ColorType type, const char *value);
static void SetDefaultColor(ColorType type); 

static unsigned long ReadHex(const char *hex);

static unsigned long GetRGBFromXColor(const XColor *c);
static XColor GetXColorFromRGB(unsigned long rgb);

static int GetColorByName(const char *str, XColor *c);
static void InitializeNames();

static void LightenColor(ColorType oldColor, ColorType newColor);
static void DarkenColor(ColorType oldColor, ColorType newColor);

/****************************************************************************
 ****************************************************************************/
void
InitializeColors()
{

}

/****************************************************************************
 ****************************************************************************/
void
StartupColors()
{

	int x;
	int red, green, blue;
	XColor c;

	/* Determine how to convert between RGB triples and pixels. */
	Assert(rootVisual);
	switch(rootVisual->class) {
	case DirectColor:
	case TrueColor:
		ComputeShiftMask(rootVisual->red_mask, &redShift, &redMask);
		ComputeShiftMask(rootVisual->green_mask, &greenShift, &greenMask);
		ComputeShiftMask(rootVisual->blue_mask, &blueShift, &blueMask);
		map = NULL;
		break;
	default:

		/* Attempt to get 256 colors, pretend it worked. */
		redMask = 0xE0;
		greenMask = 0x1C;
		blueMask = 0x03;
		ComputeShiftMask(redMask, &redShift, &redMask);
		ComputeShiftMask(greenMask, &greenShift, &greenMask);
		ComputeShiftMask(blueMask, &blueShift, &blueMask);
		map = Allocate(sizeof(unsigned long) * 256);

		/* RGB: 3, 3, 2 */
		x = 0;
		for(red = 0; red < 8; red++) {
			for(green = 0; green < 8; green++) {
				for(blue = 0; blue < 4; blue++) {
					c.red = 74898 * red / 8;
					c.green = 74898 * green / 8;
					c.blue = 87381 * blue / 4;
					c.flags = DoRed | DoGreen | DoBlue;
					JXAllocColor(display, rootColormap, &c);
					map[x] = c.pixel;
					++x;
				}
			}
		}

		break;
	}

	/* Inherit unset colors from the tray for tray items. */
	if(names) {
		if(!names[COLOR_TASK_BG]) {
			names[COLOR_TASK_BG] = CopyString(names[COLOR_TRAY_BG]);
		}
		if(!names[COLOR_TRAYBUTTON_BG]) {
			names[COLOR_TRAYBUTTON_BG] = CopyString(names[COLOR_TRAY_BG]);
		}
		if(!names[COLOR_CLOCK_BG]) {
			names[COLOR_CLOCK_BG] = CopyString(names[COLOR_TRAY_BG]);
		}
		if(!names[COLOR_TASK_FG]) {
			names[COLOR_TASK_FG] = CopyString(names[COLOR_TRAY_FG]);
		}
		if(!names[COLOR_TRAYBUTTON_FG]) {
			names[COLOR_TRAYBUTTON_FG] = CopyString(names[COLOR_TRAY_FG]);
		}
		if(!names[COLOR_CLOCK_FG]) {
			names[COLOR_CLOCK_FG] = CopyString(names[COLOR_TRAY_FG]);
		}
	}

	/* Get color information used for JWM stuff. */
	for(x = 0; x < COLOR_COUNT; x++) {
		if(names && names[x]) {
			if(!ParseColor(x, names[x])) {
				SetDefaultColor(x);
			}
		} else {
			SetDefaultColor(x);
		}
	}

	if(names) {
		for(x = 0; x < COLOR_COUNT; x++) {
			if(names[x]) {
				Release(names[x]);
			}
		}
		Release(names);
		names = NULL;
	}

	LightenColor(COLOR_BORDER_BG, COLOR_BORDER_UP);
	DarkenColor(COLOR_BORDER_BG, COLOR_BORDER_DOWN);

	LightenColor(COLOR_BORDER_ACTIVE_BG, COLOR_BORDER_ACTIVE_UP);
	DarkenColor(COLOR_BORDER_ACTIVE_BG, COLOR_BORDER_ACTIVE_DOWN);

	LightenColor(COLOR_TRAY_BG, COLOR_TRAY_UP);
	DarkenColor(COLOR_TRAY_BG, COLOR_TRAY_DOWN);

	LightenColor(COLOR_TASK_BG, COLOR_TASK_UP);
	DarkenColor(COLOR_TASK_BG, COLOR_TASK_DOWN);

	LightenColor(COLOR_TASK_ACTIVE_BG, COLOR_TASK_ACTIVE_UP);
	DarkenColor(COLOR_TASK_ACTIVE_BG, COLOR_TASK_ACTIVE_DOWN);

	LightenColor(COLOR_MENU_BG, COLOR_MENU_UP);
	DarkenColor(COLOR_MENU_BG, COLOR_MENU_DOWN);

	LightenColor(COLOR_MENU_ACTIVE_BG, COLOR_MENU_ACTIVE_UP);
	DarkenColor(COLOR_MENU_ACTIVE_BG, COLOR_MENU_ACTIVE_DOWN);

}

/****************************************************************************
 ****************************************************************************/
void
ShutdownColors()
{

#ifdef USE_XFT

	int x;

	for(x = 0; x < COLOR_COUNT; x++) {
		if(xftColors[x]) {
			JXftColorFree(display, rootVisual, rootColormap, xftColors[x]);
			Release(xftColors[x]);
			xftColors[x] = NULL;
		}
	}

#endif

	if(map != NULL) {
		JXFreeColors(display, rootColormap, map, 256, 0);
		Release(map);
		map = NULL;
	}

}

/****************************************************************************
 ****************************************************************************/
void
DestroyColors()
{

	int x;

	if(names) {
		for(x = 0; x < COLOR_COUNT; x++) {
			if(names[x]) {
				Release(names[x]);
			}
		}
		Release(names);
		names = NULL;
	}

}

/****************************************************************************
 ****************************************************************************/
void
ComputeShiftMask(unsigned long maskIn,
	unsigned long *shiftOut, unsigned long *maskOut)
{

	int shift;

	Assert(shiftOut);
	Assert(maskOut);

	/* Components are stored in 16 bits.
	 * When computing pixels, we'll first shift left 16 bits
	 * so to the shift will be an offset from that 32 bit entity.
	 * shift = 16 - <shift-to-ones> + <shift-to-zeros>
	 */

	shift = 0;
	*maskOut = maskIn;
	while(maskIn && (maskIn & (1 << 31)) == 0) {
		++shift;
		maskIn <<= 1;
	}
	*shiftOut = shift;

}

/****************************************************************************
 ****************************************************************************/
unsigned long
GetRGBFromXColor(const XColor *c)
{

	float red, green, blue;
	unsigned long rgb;

	Assert(c);

	red = (float)c->red / 65535.0;
	green = (float)c->green / 65535.0;
	blue = (float)c->blue / 65535.0;

	rgb = (unsigned long)(red * 255.0) << 16;
	rgb |= (unsigned long)(green * 255.0) << 8;
	rgb |= (unsigned long)(blue * 255.0);

	return rgb;

}

/****************************************************************************
 ****************************************************************************/
XColor
GetXColorFromRGB(unsigned long rgb)
{

	XColor ret = { 0 };

	ret.flags = DoRed | DoGreen | DoBlue;
	ret.red = (unsigned short)(((rgb >> 16) & 0xFF) * 257);
	ret.green = (unsigned short)(((rgb >> 8) & 0xFF) * 257);
	ret.blue = (unsigned short)((rgb & 0xFF) * 257);

	return ret;

}

/****************************************************************************
 ****************************************************************************/
void
SetColor(ColorType c, const char *value)
{

	if(!value) {
		Warning("empty color tag");
		return;
	}

	InitializeNames();

	if(names[c]) {
		Release(names[c]);
	}

	names[c] = CopyString(value);

}

/****************************************************************************
 ****************************************************************************/
int
ParseColor(ColorType type, const char *value)
{

	XColor temp;
	unsigned long rgb;

	if(!value) {
		return 0;
	}

	if(value[0] == '#' && strlen(value) == 7) {
		rgb = ReadHex(value + 1);
		temp.red = ((rgb >> 16) & 0xFF) * 257;
		temp.green = ((rgb >> 8) & 0xFF) * 257;
		temp.blue = (rgb & 0xFF) * 257;
		temp.flags = DoRed | DoGreen | DoBlue;
		GetColor(&temp);
	} else {
		if(!GetColorByName(value, &temp)) {
			Warning("bad color: \"%s\"", value);
			return 0;
		}
	}
	colors[type] = temp.pixel;
	rgbColors[type] = GetRGBFromXColor(&temp);

	return 1;

}

/****************************************************************************
 ****************************************************************************/
void
SetDefaultColor(ColorType type)
{

	int x;

	for(x = 0; DEFAULT_COLORS[x].value; x++) {
		if(DEFAULT_COLORS[x].type == type) {
			ParseColor(type, DEFAULT_COLORS[x].value);
			return;
		}
	}

}

/****************************************************************************
 ****************************************************************************/
void
InitializeNames()
{

	int x;

	if(names == NULL) {
		names = Allocate(sizeof(char*) * COLOR_COUNT);
		for(x = 0; x < COLOR_COUNT; x++) {
			names[x] = NULL;
		}
	}

}

/****************************************************************************
 ****************************************************************************/
unsigned long
ReadHex(const char *hex)
{

	unsigned long value = 0;
	int x;

	Assert(hex);

	for(x = 0; hex[x]; x++) {
		value *= 16;
		if(hex[x] >= '0' && hex[x] <= '9') {
			value += hex[x] - '0';
		} else if(hex[x] >= 'A' && hex[x] <= 'F') {
			value += hex[x] - 'A' + 10;
		} else if(hex[x] >= 'a' && hex[x] <= 'f') {
			value += hex[x] - 'a' + 10;
		}
	}

	return value;

}

/****************************************************************************
 ****************************************************************************/
void
LightenColor(ColorType oldColor, ColorType newColor)
{

	XColor temp;
	float red, green, blue;
	float delta = 1.0 + COLOR_DELTA;

	temp = GetXColorFromRGB(rgbColors[oldColor]);

	red = (float)temp.red / 65535.0;
	green = (float)temp.green / 65535.0;
	blue = (float)temp.blue / 65535.0;

	red = Min(delta * red, 1.0);
	green = Min(delta * green, 1.0);
	blue = Min(delta * blue, 1.0);

	temp.red = red * 65535.0;
	temp.green = green * 65535.0;
	temp.blue = blue * 65535.0;

	GetColor(&temp);
	colors[newColor] = temp.pixel;
	rgbColors[newColor] = GetRGBFromXColor(&temp);

}

/****************************************************************************
 ****************************************************************************/
void
DarkenColor(ColorType oldColor, ColorType newColor)
{

	XColor temp;
	float red, green, blue;
	float delta = 1.0 - COLOR_DELTA;

	temp = GetXColorFromRGB(rgbColors[oldColor]);

	red = (float)temp.red / 65535.0;
	green = (float)temp.green / 65535.0;
	blue = (float)temp.blue / 65535.0;

	red = delta * red;
	green = delta * green;
	blue = delta * blue;

	temp.red = red * 65535.0;
	temp.green = green * 65535.0;
	temp.blue = blue * 65535.0;

	GetColor(&temp);
	colors[newColor] = temp.pixel;
	rgbColors[newColor] = GetRGBFromXColor(&temp);

}

/***************************************************************************
 ***************************************************************************/
int
GetColorByName(const char *str, XColor *c)
{

	Assert(str);
	Assert(c);

	if(!JXParseColor(display, rootColormap, str, c)) {
		return 0;
	}

	GetColor(c);

	return 1;

}

/***************************************************************************
 * Compute the RGB components from an index into our RGB colormap.
 ***************************************************************************/
void
GetColorFromIndex(XColor *c)
{

	unsigned long red;
	unsigned long green;
	unsigned long blue;

	Assert(c);

	red = (c->pixel & redMask) << redShift;
	green = (c->pixel & greenMask) << greenShift;
	blue = (c->pixel & blueMask) << blueShift;

	c->red = red >> 16;
	c->green = green >> 16;
	c->blue = blue >> 16;

}

/***************************************************************************
 * Compute the pixel value from RGB components.
 ***************************************************************************/
void
GetDirectPixel(XColor *c)
{

	unsigned long red;
	unsigned long green;
	unsigned long blue;

	Assert(c);

	/* Normalize. */
	red = c->red << 16;
	green = c->green << 16;
	blue = c->blue << 16;

	/* Shift to the correct offsets and mask. */
	red = (red >> redShift) & redMask;
	green = (green >> greenShift) & greenMask;
	blue = (blue >> blueShift) & blueMask;

	/* Combine. */
	c->pixel = red | green | blue;

}

/***************************************************************************
 * Compute the pixel value from RGB components.
 ***************************************************************************/
void
GetMappedPixel(XColor *c)
{

	Assert(c);

	GetDirectPixel(c);
	c->pixel = map[c->pixel];

}

/***************************************************************************
 * Compute the pixel value from RGB components.
 ***************************************************************************/
void
GetColor(XColor *c)
{

	Assert(c);
	Assert(rootVisual);

	switch(rootVisual->class) {
	case DirectColor:
	case TrueColor:
		GetDirectPixel(c);
		return;
	default:
		GetMappedPixel(c);
		return;
	}

}

/***************************************************************************
 * When loading images from external sources, we need to know the color
 * components even if running with a colormap. So here we pretend
 * we have a linear RGB colormap even if we don't.
 * This prevents calls to XQueryColor later.
 ***************************************************************************/
void
GetColorIndex(XColor *c)
{

	Assert(c);

	GetDirectPixel(c);

}

/****************************************************************************
 ****************************************************************************/
#ifdef USE_XFT
XftColor *GetXftColor(ColorType type) {

	unsigned long rgb;
	XRenderColor rcolor;

	if(!xftColors[type]) {
		rgb = rgbColors[type];
		xftColors[type] = Allocate(sizeof(XftColor));
		rcolor.alpha = 65535;
		rcolor.red = ((rgb >> 16) & 0xFF) * 257;
		rcolor.green = ((rgb >> 8) & 0xFF) * 257;
		rcolor.blue = (rgb & 0xFF) * 257;
		JXftColorAllocValue(display, rootVisual, rootColormap, &rcolor,
			xftColors[type]);
	}

	return xftColors[type];

}
#endif