Back to index

plt-scheme  4.2.1
Functions
rgb.c File Reference
#include "xpmP.h"
#include <ctype.h>
#include <strings.h>

Go to the source code of this file.

Functions

int xpmReadRgbNames (char *rgb_fname, rgbn)
char * xpmGetRgbName (rgbn, int rgbn_max, int red, int green, int blue)
void xpmFreeRgbNames (rgbn, int rgbn_max)

Function Documentation

void xpmFreeRgbNames ( rgbn  ,
int  rgbn_max 
)

Definition at line 149 of file rgb.c.

{
    int i;
    xpmRgbName *rgb;

    for (i = 0, rgb = rgbn; i < rgbn_max; i++, rgb++)
       XpmFree(rgb->name);
}

Here is the caller graph for this function:

char* xpmGetRgbName ( rgbn  ,
int  rgbn_max,
int  red,
int  green,
int  blue 
)

Definition at line 123 of file rgb.c.

{
    int i;
    xpmRgbName *rgb;

    /*
     * Just perform a dumb linear search over the rgb values of the color
     * mnemonics.  One could speed things up by sorting the rgb values and
     * using a binary search, or building a hash table, etc...
     */
    for (i = 0, rgb = rgbn; i < rgbn_max; i++, rgb++)
       if (red == rgb->r && green == rgb->g && blue == rgb->b)
           return rgb->name;

    /* if not found return NULL */
    return NULL;
}

Here is the caller graph for this function:

int xpmReadRgbNames ( char *  rgb_fname,
rgbn   
)

Definition at line 61 of file rgb.c.

{
    FILE *rgbf;
    int n, items, red, green, blue;
    char line[512], name[512], *rgbname, *s1, *s2;
    xpmRgbName *rgb;

    /* Open the rgb text file.  Abort if error. */
    if ((rgbf = fopen(rgb_fname, "r")) == NULL)
       return 0;

    /* Loop reading each line in the file. */
    n = 0;
    rgb = rgbn; 
    /* Quit if rgb text file has too many entries. */
    while (fgets(line, sizeof(line), rgbf) && n < MAX_RGBNAMES) {

       /* Skip silently if line is bad. */
       items = sscanf(line, "%d %d %d %[^\n]\n", &red, &green, &blue, name);
       if (items != 4)
           continue;

       /*
        * Make sure rgb values are within 0->255 range. Skip silently if
        * bad.
        */
       if (red < 0 || red > 0xFF ||
           green < 0 || green > 0xFF ||
           blue < 0 || blue > 0xFF)
           continue;

       /* Allocate memory for ascii name. If error give up here. */
       if (!(rgbname = (char *) XpmMalloc(strlen(name) + 1)))
           break;

       /* Copy string to ascii name and lowercase it. */
       for (s1 = name, s2 = rgbname; *s1; s1++)
           *s2++ = tolower(*s1);
       *s2 = '\0';

       /* Save the rgb values and ascii name in the array. */
       rgb->r = red * 257;         /* 65535/255 = 257 */
       rgb->g = green * 257;
       rgb->b = blue * 257;
       rgb->name = rgbname;
       rgb++;
       n++;
    }

    fclose(rgbf);

    /* Return the number of read rgb names. */
    return n < 0 ? 0 : n;
}

Here is the caller graph for this function: