home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC Shareware 1999 March
/
PCShareware-3-99.iso
/
IMPLE
/
DJGPP.RAR
/
DJGPP2
/
XLIB-SR0.ZIP
/
SRC
/
XLIBEMU
/
COLOR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-14
|
4KB
|
198 lines
/* $Id: color.c 1.2 1994/01/15 02:13:01 ulrich Exp $ */
/*
* color.c
*
* X library color functions.
* Colormap parameters are ignored (only StdColormap).
*/
#include "Xlibemu.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
Status XAllocColor(
Display* display,
Colormap colormap,
XColor* screen_in_out)
{
int r = screen_in_out->red >> 8;
int g = screen_in_out->green >> 8;
int b = screen_in_out->blue >> 8;
int pixel = GrAllocColor (r, g, b);
if (pixel != GrNOCOLOR) {
screen_in_out->pixel = pixel;
return 1;
}
return 0;
}
static struct _ColorEntry {
unsigned int r:8;
unsigned int g:8;
unsigned int b:8;
char *name;
} _ColorTable[] = {
# include "rgb.h"
};
static int _ColorTableSorted = 0;
static int CompareColorEntry (const void *c1, const void *c2)
{
return strcasecmp (((struct _ColorEntry *) c1)->name,
((struct _ColorEntry *) c2)->name);
}
static void SortColorTable ()
{
if (_ColorTableSorted == 0) {
qsort (_ColorTable,
sizeof(_ColorTable) / sizeof(_ColorTable[0]),
sizeof(_ColorTable[0]),
CompareColorEntry);
_ColorTableSorted = 1;
}
}
static int
_WParseColor (char *name, XColor *color)
{
struct _ColorEntry *entry, key;
if (!name || !color)
return 0; /* not found */;
if (name[0] == '#')
{
unsigned int r, g, b;
if (sscanf (name, "#%4x%4x%4x", &r, &g, &b) == 3)
{
color->red = r;
color->green = g;
color->blue = b;
return 1;
}
if (sscanf (name, "#%2x%2x%2x", &r, &g, &b) == 3)
{
color->red = (r & 0xff) * 257;
color->green = (g & 0xff) * 257;
color->blue = (b & 0xff) * 257;
return 1;
}
return 0; /* not found */;
}
SortColorTable ();
key.name = name;
entry = bsearch (&key, _ColorTable,
sizeof(_ColorTable) / sizeof(_ColorTable[0]),
sizeof(_ColorTable[0]),
CompareColorEntry);
if (entry == NULL)
return 0;
color->red = entry->r * 257;
color->green = entry->g * 257;
color->blue = entry->b * 257;
return 1;
}
Status XParseColor(
Display* display,
Colormap colormap,
_Xconst char* spec,
XColor* exact_def_return)
{
if (_WParseColor ((char *) spec, exact_def_return)) {
exact_def_return->flags = DoRed|DoGreen|DoBlue;
return 1;
}
return 0;
}
int
XQueryColor(
Display* display,
Colormap colormap,
XColor* def_in_out)
{
return XQueryColors (display, colormap, def_in_out, 1);
}
XQueryColors(
Display* display,
Colormap colormap,
XColor* defs_in_out,
int ncolors)
{
int r, g, b;
while (--ncolors >= 0) {
GrQueryColor (defs_in_out->pixel, &r, &g, &b);
defs_in_out->red = r * 257;
defs_in_out->green = g * 257;
defs_in_out->blue = b * 257;
defs_in_out++;
}
return 0;
}
Status XAllocNamedColor(
Display* display,
Colormap colormap,
_Xconst char* color_name,
XColor* screen_def_return,
XColor* exact_def_return)
{
if (XParseColor (display, colormap, color_name, exact_def_return))
{
int status;
*screen_def_return = *exact_def_return;
status = XAllocColor (display, colormap, screen_def_return);
exact_def_return->pixel = screen_def_return->pixel;
return status;
}
return 0;
}
int XFreeColors(
Display* display,
Colormap colormap,
unsigned long* pixels,
int npixels,
unsigned long planes)
{
int i;
for (i = 0; i < npixels; i++)
{
/* if (pixel[i] & planes) */
GrFreeColor (pixels[i]);
}
return 0;
}
Status XLookupColor(
Display* display,
Colormap colormap,
_Xconst char* color_name,
XColor* exact_def_return,
XColor* screen_def_return)
{
if (XParseColor (display, colormap, color_name, exact_def_return))
{
*screen_def_return = *exact_def_return;
if (XAllocColor (display, colormap, screen_def_return)) {
XQueryColor (display, colormap, screen_def_return);
XFreeColors (display, colormap, &screen_def_return->pixel, 1,
AllPlanes);
return 1;
}
}
return 0;
}