File: xcolors.c

package info (click to toggle)
xcolors 1.5a-8
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 72 kB
  • ctags: 36
  • sloc: ansic: 295; makefile: 50
file content (380 lines) | stat: -rw-r--r-- 10,476 bytes parent folder | download | duplicates (5)
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
/* 
 * xcolors - display all X color names and colors
 * Time-stamp: <91/10/04 13:49:11 gildea>
 *
 * Copyright (c) 1989,1991 by Stephen Gildea
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided 
 * that the above copyright notice appears in all copies and that both that 
 * copyright notice and this permission notice appear in supporting 
 * documentation.  The authors make no representations about the 
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * Author:  Stephen Gildea <gildea@expo.lcs.mit.edu>
 *          Orignal program by Paul Vixie.
 */

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Viewport.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Label.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>		/* for sqrt() */

#ifndef RGB_TXT
#define RGB_TXT "/etc/X11/rgb.txt"
#endif

String fallback_resources[] = {
    "*Label.Font: 5x7",
    "*panes.Width: 985",
    "*Text*Height: 50",
    "*viewport.allowVert: on",
    "*sample*String: \
Button 1 sets the text color, button 2 sets the background.\\n\
Press q to quit.  You can also type new text here.",
    "*Label*translations: #override \
	<Btn1Up>: set-foreground()\\n\
	<Btn2Up>: set-background()",
    "*Box*baseTranslations: #override \
	<Key>q: quit()",
    NULL};

struct OptionVals {
    char *start_color;
    char *rgb_file;
    char *near_color;
    int   near_distance;	/* radius, 0 to sqrt(3)*256 */
} opts;

XtResource resources[] = {
    { "startColor", "StartColor", XtRString, sizeof (char *), 
      XtOffsetOf(struct OptionVals, start_color), XtRString, NULL },
    { "rgbFile", "RgbFile", XtRString, sizeof (char *), 
      XtOffsetOf(struct OptionVals, rgb_file), XtRString, RGB_TXT },
    { "nearColor", "NearColor", XtRString, sizeof (char *), 
      XtOffsetOf(struct OptionVals, near_color), XtRString, NULL },
    { "nearDistance", "NearDistance", XtRInt, sizeof (int), 
      XtOffsetOf(struct OptionVals, near_distance),XtRImmediate,(XtPointer)64},
};

XrmOptionDescRec cmd_options[] = {
    {"-start", ".startColor", XrmoptionSepArg, NULL},
    {"-rgbfile", ".rgbFile", XrmoptionSepArg, NULL},
    {"-near", ".nearColor", XrmoptionSepArg, NULL},
    {"-distance", ".nearDistance", XrmoptionSepArg, NULL},
};

String wm_trans =
    "<ClientMessage>WM_PROTOCOLS: quit()\n";

char *program_name;
int n_colors_displayed = 0;	/* number of colors we are showing */
Atom wm_delete_window;
Atom wm_protocols;

Widget sample;			/* the sample text */

void usage()
{
    static char *option_help[] = {
"    -rgbfile filename           rgb.txt color database file to use",
"    -start colorname            first color to show",
"    -near colorname             only show colors similar to this one",
"    -distance int               how far in 0-255 RGB space is similar",
"",
NULL};
    char **cpp;

    fprintf (stderr, "usage: %s [-options ...]\n",
	     program_name);
    fprintf (stderr, " where options include:\n");
    for (cpp = option_help; *cpp; cpp++) {
	fprintf (stderr, "%s\n", *cpp);
    }
}

void
do_set_color(w, colorname, foreground_flag)
    Widget w;
    String colorname;		/* if null, get from w */
    Boolean foreground_flag;
{
    Arg arg[2];
    int n;
    XrmValue namein, pixelout;
    Pixel color;

    if (colorname) {
	namein.addr = colorname;
	namein.size = strlen(colorname) + 1;
	pixelout.size = 0;	/* so we can check for success */
	XtConvert(w, XtRString, &namein, XtRPixel, &pixelout);
	if (pixelout.size == 0) {
	    fprintf(stderr, "%s: no such color as \"%s\" for %s\n",
		    program_name, colorname,
		    foreground_flag ? XtNforeground : XtNbackground);
	    return;
	}
	color = *(Pixel*)(pixelout.addr);
    } else {
	n = 0;
	XtSetArg(arg[n], XtNborderColor, &color); n++;
	XtGetValues(w, arg, n);
    }

    /* set the value in the text sample */
    XtSetArg(arg[0], foreground_flag ? XtNforeground : XtNbackground, color);
    XtSetValues(sample, arg, 1);
}

/* ARGSUSED */
void
set_foreground(w, event, params, num_params)
    Widget w;
    XEvent *event;		
    String *params;	
    Cardinal *num_params;
{
    do_set_color(w, *num_params ? params[0] : NULL, TRUE);
}

/* ARGSUSED */
void
set_background(w, event, params, num_params)
    Widget w;
    XEvent *event;		
    String *params;	
    Cardinal *num_params;
{
    do_set_color(w, *num_params ? params[0] : NULL, FALSE);
}

/* ARGSUSED */
void
quit_action(w, event, params, num_params)
    Widget w;
    XEvent *event;		
    String *params;	
    Cardinal *num_params;
{
    if(event->type == ClientMessage && event->xclient.type == wm_protocols
       && event->xclient.data.l[0] != wm_delete_window)
	return;

    exit(0);
}

XtActionsRec actionTable[] = {
    {"quit", quit_action},
    {"set-foreground", set_foreground},
    {"set-background", set_background},
};


main(argc, argv)
     int argc;
     char *argv[];
{
    XtAppContext app_context;
    Widget toplevel, panes, viewport, colors;
    Arg arg[10];
    int n;
    Status stat;
    XColor near_rgb, junk_rgb;

    toplevel = XtAppInitialize(&app_context, "Xcolors",
			       cmd_options, XtNumber(cmd_options), &argc, argv,
			       fallback_resources, NULL, 0);

    XtGetApplicationResources (toplevel, (XtPointer) &opts, resources,
			       XtNumber(resources), NULL, 0);

    program_name = argv[0];
    argc--; argv++;

    if (argc != 0) {
	usage();
	exit(3);
    }

    if (opts.near_color) {
	/* User asked to only see colors near this in RGB space.
	   Get the color from the server and scale it for comparison with
	   rgb.txt values */
	stat = XLookupColor(XtDisplay(toplevel),
			    DefaultColormapOfScreen(XtScreen(toplevel)),
			    opts.near_color, &near_rgb, &junk_rgb);
	if (!stat) {
	    fprintf(stderr, "%s: color \"%s\" is not known to the X server\n",
		program_name, opts.near_color);
	    exit(1);
	}
	near_rgb.red >>= 8;
	near_rgb.green >>= 8;
	near_rgb.blue >>= 8;
	near_rgb.flags = DoRed|DoBlue|DoGreen;
    } else
	near_rgb.flags = 0;

    XtAppAddActions(app_context, actionTable, XtNumber(actionTable));

    n = 0;
    panes = XtCreateManagedWidget("panes", panedWidgetClass,
				  toplevel, arg, n);
    
    n = 0;
    XtSetArg(arg[n], XtNeditType, XawtextEdit); n++;
    sample = XtCreateManagedWidget("sample", asciiTextWidgetClass,
				   panes, arg, n);
    
    n = 0;
    viewport = XtCreateManagedWidget("viewport", viewportWidgetClass,
				     panes, arg, n);
    
    n = 0;
    colors = XtCreateManagedWidget("colors", boxWidgetClass,
				   viewport, arg, n);
    
    colordemo(colors, opts.start_color, near_rgb, opts.near_distance);

    if (n_colors_displayed == 0) {
	fprintf(stderr, "%s: no colors to display\n", program_name);
	exit(5);
    }
    
    XtSetMappedWhenManaged(toplevel, FALSE);
    XtRealizeWidget(toplevel);

    /* do WM_DELETE_WINDOW before map */
    XtOverrideTranslations(toplevel, XtParseTranslationTable(wm_trans));
    wm_protocols = XInternAtom(XtDisplay(toplevel), "WM_PROTOCOLS", False);
    wm_delete_window = XInternAtom(XtDisplay(toplevel), "WM_DELETE_WINDOW",
				   False);
    XSetWMProtocols(XtDisplay(toplevel), XtWindow(toplevel),
		    &wm_delete_window, 1);
    
    XtMapWidget(toplevel);
    XtAppMainLoop(app_context);
    /* NOTREACHED */
}

colordemo(parent, startcolor, nearcolor, maxdist)
     Widget parent;
     char *startcolor;
     XColor nearcolor;		/* scaled 0-255 */
     int maxdist;
{
    int r, g, b, prev_r, prev_g, prev_b;
    char colorname[50], save_colorname[50];
    FILE *rgb;
    Bool do_color();
    double ddist = maxdist;
    
    rgb = fopen(opts.rgb_file, "r");
    if (rgb == NULL) {
	perror(opts.rgb_file);
	exit(2);
    }
    
    prev_r = prev_g = prev_b = -1;
    save_colorname[0] = '\0';
    while (!feof(rgb)) {
        char str[1024];
        
        fgets(str, 1024L, rgb);
        if (*str != '!') /* comment */ {
          sscanf(str, "%d %d %d %[^\n]\n", &r, &g, &b, colorname);
  	  if (startcolor)
	    if (l_strcasecmp(colorname, startcolor))
	      continue;		/* haven't reached starting point yet */
	    else
	      startcolor = (char *)NULL;
	  if (r != prev_r  ||  g != prev_g  ||  b != prev_b) {
	      if (nearcolor.flags) {
		  double ourdist =
		      sqrt((double)((nearcolor.red-r)*(nearcolor.red-r)
				  + (nearcolor.green-g)*(nearcolor.green-g)
				  + (nearcolor.blue-b)*(nearcolor.blue-b)));
		  if (ourdist > ddist)
		      continue;
	      }
	      if (save_colorname[0] != '\0') /* skip first time through */
	        if (!do_color(parent, save_colorname))
		  return;
	      prev_r = r;
	      prev_g = g;
	      prev_b = b;
	  }
	  strcpy(save_colorname, colorname);
	}
    }
    if (save_colorname[0] != '\0')
      (void)do_color(parent, save_colorname);

    if (startcolor) {		/* never found starting color in the file */
	fprintf(stderr, "%s: \"%s\" not found in %s\n",
		program_name, startcolor, opts.rgb_file);
    }
    
}


/*
 * does one color.
 * Returns a success code.
 */
Bool
do_color(parent, colorname)
     Widget parent;
     char *colorname;
{
    Arg		arg[10];
    int 	n;
    XrmValue	namein, pixelout;
    
    /* convert colorname to a Pixel (a colormap index) */
    namein.addr = colorname;
    namein.size = strlen(colorname) + 1;
    pixelout.size = 0;	/* so we can check for success */
    XtConvert(parent, XtRString, &namein, XtRPixel, &pixelout);
    if (pixelout.size == 0) {
	fprintf(stderr, "%s: Not enough room in color map.\n", program_name);
	fprintf(stderr, "To see colors after this, ");
	fprintf(stderr, "use the -start option to name a starting color.\n");
	return FALSE;
    }
    
    /* create a widget to display the color */
    n = 0;
    XtSetArg(arg[n], XtNborderWidth, 10); n++;
    XtSetArg(arg[n], XtNborderColor, *(Pixel*)(pixelout.addr)); n++;
    XtCreateManagedWidget(colorname, labelWidgetClass, parent, arg, n);
    n_colors_displayed++;
    return TRUE;
}


/*
 * Not all systems have strcasecmp, so we provide our own.
 * Similar to strcmp, but ignores case.
 * Always returns 1 if different.
 */
int
l_strcasecmp(s1, s2)
     char *s1, *s2;
{
    for ( ; *s1 && *s2 ; s1++, s2++)
      if ((isupper(*s1) ? tolower(*s1) : *s1) !=
	  (isupper(*s2) ? tolower(*s2) : *s2))
	return 1;
    if (*s1 || *s2)
      return 1;
    return 0;
}