~ubuntu-branches/ubuntu/natty/sawfish/natty

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
/* colors.c -- Colour handling
   $Id$

   Copyright (C) 1999 John Harper <john@dcs.warwick.ac.uk>

   This file is part of sawfish.

   sawfish 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, or (at your option)
   any later version.

   sawfish 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 sawfish; see the file COPYING.   If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */

#include "sawfish.h"

static Lisp_Color *color_list;
int color_type;

DEFSYM(default_foreground, "default-foreground");

DEFUN("get-color-rgb", Fget_color_rgb, Sget_color_rgb,
      (repv red, repv green, repv blue, repv alpha), rep_Subr4) /*
::doc:sawfish.wm.colors#get-color-rgb::
get-color-rgb RED GREEN BLUE [ALPHA]
::end:: */
{
    Lisp_Color *f;

    rep_DECLARE1(red, rep_INTP);
    rep_DECLARE2(green, rep_INTP);
    rep_DECLARE3(blue, rep_INTP);
    if (!rep_INTP (alpha))
	alpha = rep_MAKE_INT (65535);

    if (dpy == 0)
	return Qnil;

    f = color_list;
    while (f != 0)
    {
	if (f->red == rep_INT(red)
	    && f->green == rep_INT(green)
	    && f->blue == rep_INT(blue)
	    && f->alpha == rep_INT(alpha))
	{
	    break;
	}
	f = f->next;
    }
    if (f == 0)
    {
	int pixel = best_color_match (rep_INT(red) / 256,
				      rep_INT(green) / 256,
				      rep_INT(blue) / 256);

	f = rep_ALLOC_CELL(sizeof(Lisp_Color));
	rep_data_after_gc += sizeof (Lisp_Color);
	f->car = color_type;
	f->next = color_list;
	color_list = f;

	f->red = rep_INT(red);
	f->green = rep_INT(green);
	f->blue = rep_INT(blue);
	f->alpha = rep_INT(alpha);
	f->pixel = pixel;
    }
    return rep_VAL(f);
}
    
DEFUN("get-color", Fget_color, Sget_color, (repv name, repv alpha), rep_Subr2) /*
::doc:sawfish.wm.colors#get-color::
get-color NAME [ALPHA]

Return the color object representing the color named NAME, a standard
X11 color specifier.
::end:: */
{
    XColor exact_col;
    rep_DECLARE1(name, rep_STRINGP);

    if (dpy == 0)
	return Qnil;

    if (XParseColor (dpy, image_cmap, rep_STR(name), &exact_col) != 0)
    {
	return Fget_color_rgb (rep_MAKE_INT(exact_col.red),
			       rep_MAKE_INT(exact_col.green),
			       rep_MAKE_INT(exact_col.blue),
			       alpha);
    }
    else
    {
	return Fsignal (Qerror,
			rep_list_2 (rep_string_dup("no such color"),
				    name));
    }
}

DEFUN("color-name", Fcolor_name, Scolor_name, (repv color), rep_Subr1) /*
::doc:sawfish.wm.colors#color-name::
color-name COLOR

Return the name of the color represented by the color object COLOR.
::end:: */
{
    char buf[32];
    rep_DECLARE1(color, COLORP);
    sprintf (buf, "#%04x%04x%04x",
	     VCOLOR(color)->red, VCOLOR(color)->green, VCOLOR(color)->blue);
    return rep_string_dup (buf);
}

DEFUN("color-rgb", Fcolor_rgb, Scolor_rgb, (repv color), rep_Subr1) /*
::doc:sawfish.wm.colors#color-rgb::
color-rgb COLOR

Returns a list of integers (RED GREEN BLUE ALPHA) representing the
actual color values of the color represented by object COLOR. The
individual values range from zero to 65535.
::end:: */
{
    rep_DECLARE1(color, COLORP);
    return rep_list_4 (rep_MAKE_INT(VCOLOR(color)->red),
		       rep_MAKE_INT(VCOLOR(color)->green),
		       rep_MAKE_INT(VCOLOR(color)->blue),
		       rep_MAKE_INT(VCOLOR(color)->alpha));
}

DEFUN("color-rgb-8", Fcolor_rgb_8, Scolor_rgb_8, (repv color), rep_Subr1) /*
::doc:sawfish.wm.colors#color-rgb::
color-rgb-8 COLOR

Returns a list of integers (RED GREEN BLUE ALPHA) representing the
actual color values of the color represented by object COLOR. The
individual values range from zero to 255.
::end:: */
{
    rep_DECLARE1(color, COLORP);
    return rep_list_4 (rep_MAKE_INT(VCOLOR(color)->red / 256),
		       rep_MAKE_INT(VCOLOR(color)->green / 256),
		       rep_MAKE_INT(VCOLOR(color)->blue / 256),
		       rep_MAKE_INT(VCOLOR(color)->alpha / 256));
}

DEFUN("colorp", Fcolorp, Scolorp, (repv win), rep_Subr1) /*
::doc:sawfish.wm.colors#colorp::
colorp ARG

Returns t if ARG is a color object.
::end:: */
{
    return COLORP(win) ? Qt : Qnil;
}

/* type hooks */

static int
color_cmp (repv w1, repv w2)
{
    return w1 != w2;
}

static void
color_prin (repv stream, repv obj)
{
    char buf[256];
    sprintf (buf, "#<color #%04x%04x%04x>",
	     VCOLOR(obj)->red, VCOLOR(obj)->green, VCOLOR(obj)->blue);
    rep_stream_puts (stream, buf, -1, FALSE);
}

static void
color_sweep (void)
{
    Lisp_Color *w = color_list;
    color_list = 0;
    while (w != 0)
    {
	Lisp_Color *next = w->next;
	if (!rep_GC_CELL_MARKEDP(rep_VAL(w)))
	    rep_FREE_CELL(w);
	else
	{
	    rep_GC_CLR_CELL(rep_VAL(w));
	    w->next = color_list;
	    color_list = w;
	}
	w = next;
    }
}

/* initialisation */

void
colors_init (void)
{
    repv tem = rep_push_structure ("sawfish.wm.colors");
    color_type = rep_register_new_type ("color", color_cmp, color_prin,
					color_prin, color_sweep, 0,
					0, 0, 0, 0, 0, 0, 0);
    rep_ADD_SUBR(Sget_color_rgb);
    rep_ADD_SUBR(Sget_color);
    rep_ADD_SUBR(Scolor_name);
    rep_ADD_SUBR(Scolor_rgb);
    rep_ADD_SUBR(Scolor_rgb_8);
    rep_ADD_SUBR(Scolorp);
    rep_INTERN_SPECIAL(default_foreground);
    if (!batch_mode_p ())
    {
	DEFSTRING (name, "#000000");
	repv black = Fget_color (rep_VAL (&name), Qnil);
	if (black == rep_NULL)
	    black = Qnil;
	Fset (Qdefault_foreground, black);
    }
    rep_pop_structure (tem);
}

void
colors_kill (void)
{
}