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
|
#include <stdio.h>
#include "constant.h"
/**** x include files ****/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/keysymdef.h>
#include "extern.h"
/******************************************************************************
int
match_color (red, green, blue)
Given the RGB components (<red>, <green>, <blue>) search through the custom
colors for the color which most closely matches. Return the inverse index.
This routine used for finding out what inverse should be used for X colors.
******************************************************************************/
int
match_color (red, green, blue)
int red, green, blue;
{
int hue,
dist,
minimum, min_index;
minimum = 1000000;
red = red>>8;
green = green>>8;
blue = blue>>8;
for (hue=1; hue<Config->hue_count; hue++)
{
dist = (Config->palette[hue][0] - red)*(Config->palette[hue][0] - red) +
(Config->palette[hue][1] - green)*(Config->palette[hue][1] - green) +
(Config->palette[hue][2] - blue)*(Config->palette[hue][2] - blue);
if (dist < minimum)
{
min_index = hue;
minimum = dist;
}
}
return (Config->hue_to_inverse[min_index]);
}
/******************************************************************************
int
match_color_name (color_name, default_hue)
Match <color_name> with a palette entry and return the index, or just
return <default_hue>.
******************************************************************************/
match_color_name (color_name, default_hue)
char *color_name;
{
int hue;
for (hue=0; hue<Config->hue_count; hue++)
if (!strcmp (color_name, Config->hue_name[hue]))
return (hue);
return (default_hue);
}
/******************************************************************************
matchstr (line, word)
Given a baseline string <word>, return TRUE if <line> is a subset of it.
Used solely to match colors like "red_blue" to custom colors like "red"
without getting confused by the "_blue" part. Probably should just use
strncmp (line, word, strlen(word)).
******************************************************************************/
matchstr (line, word)
char line[],
word[];
{
int i;
for (i=0; i<strlen(word); i++)
if (line[i] == '\0' || line[i] != word[i])
return (FALSE);
return (TRUE);
}
/******************************************************************************
int
get_random (base)
Use rand() to return a a number between 0 and <base>-1.
******************************************************************************/
int
get_random (base)
int base;
{
return (rand() % base);
}
|