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
|
/***************************************************************
LanceMan's quickplot --- a fast interactive 2D plotter
Copyright (C) 1998, 1999 Lance Arsenault
This program 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; version 2
of the License.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Or look at http://www.gnu.org/copyleft/gpl.html .
**********************************************************************/
/* $Id: init_colors.c,v 1.5 1999/02/27 04:21:59 lance Exp $ */
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <X11/Xlib.h>
#include <X11/Xcms.h>
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <X11/cursorfont.h>
#include <X11/Xaw/AsciiText.h>
#include <stdlib.h>
#include "data.h"
#include "xwin.h"
/* the max number of colors used with preset colors */
#define NUM_COLORS 12
/* numbers for color levels */
#define V1 0.3
#define V2 0.8
unsigned long MakeRGBColor(Display *dpy, Colormap cmap,
double r, double g,double b)
{
XcmsColor color;
color.format = XcmsRGBiFormat;
color.spec.RGBi.red = r;
color.spec.RGBi.green = g;
color.spec.RGBi.blue = b;
if(XcmsAllocColor(dpy,cmap,&color,XcmsRGBiFormat) == XcmsFailure)
return (unsigned long) NULL;
return color.pixel;
}
/* see /usr/lib/X11/rgb.txt for list of color names
*/
unsigned long GetNamedColor(Display *dpy, Colormap cmap, char *name)
{
XColor alloc, exact;
if( XAllocNamedColor( dpy, cmap, name, &alloc, &exact))
return alloc.pixel;
else
return (unsigned long) NULL;
}
/* returns the number of colors that it got
* n is the number of colors requested
*/
static int SelectNamedColors(Display *dpy, Colormap cmap,
unsigned long *pix, int n)
{
int i;
unsigned long null;
null = (unsigned long) NULL;
i = 0;
if(null == (pix[i] = GetNamedColor(dpy,cmap,"magenta")))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = GetNamedColor(dpy,cmap,"red1")))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = GetNamedColor(dpy,cmap, "green1")))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = GetNamedColor(dpy,cmap,"blue1")))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = GetNamedColor(dpy,cmap,"yellow1")))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = GetNamedColor(dpy,cmap,"cyan")))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = GetNamedColor(dpy,cmap,"coral1")))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = GetNamedColor(dpy,cmap,"DarkOrchid1")))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = GetNamedColor(dpy,cmap,"orange1")))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = GetNamedColor(dpy,cmap,"plum1")))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = GetNamedColor(dpy,cmap,"RosyBrown")))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = GetNamedColor(dpy,cmap,"salmon3")))
return i; /** that's 12 colors **/
i++; if(i == n) return n;
return i;
}
/* returns the number of colors that it got
* n is the number of colors requested
*/
static int SelectColor(Display *dpy, Colormap cmap,
unsigned long *pix, int n)
{
int i;
unsigned long null;
null = (unsigned long) NULL;
i = 0;
if(null == (pix[i] = MakeRGBColor(dpy,cmap,1.0,0.0,1.0)))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = MakeRGBColor(dpy,cmap,0.0,1.0,0.0)))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = MakeRGBColor(dpy,cmap,1.0,0.0,0.0)))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = MakeRGBColor(dpy,cmap,0.0,0.0,1.0)))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = MakeRGBColor(dpy,cmap,1.0,1.0,0.0)))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = MakeRGBColor(dpy,cmap,0.0,1.0,1.0)))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = MakeRGBColor(dpy,cmap,1.0,V1,V1)))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = MakeRGBColor(dpy,cmap,V1,1.0,V1)))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = MakeRGBColor(dpy,cmap,V1,V1,1.0)))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = MakeRGBColor(dpy,cmap,V2,V1,V2)))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = MakeRGBColor(dpy,cmap,V1,V2,V2)))
return i;
i++; if(i == n) return n;
if(null == (pix[i] = MakeRGBColor(dpy,cmap,V2,V2,V1)))
return i; /** that's 12 colors **/
i++; if(i == n) return n;
return i;
}
/* crapy RainBow color function */
/* I need something better */
static double f(double x)
{
long j;
double a;
a = (x>0.0)?x:-x;
j = a;
a -= j;
if(j & 1)
a = (1.0-a);
if(a < .4)
return 0.0;
return a*a;
}
/* returns the number of colors that it got
* n is the number of colors requested
*/
static int RainBow(Display *dpy, Colormap cmap,
unsigned long *pix, int n)
{
int i;
double w,r,g,b;
w = 2.0/n;
for(i=0;i<n;i++)
{
r = f(w*i + 1.0);
g = f(w*i + 0.3333333333*2.0 +1.0);
b = f(w*i + 0.6666666667*2.0 +1.0);
if((unsigned long) NULL == (pix[i] = MakeRGBColor(dpy,cmap,r,g,b)))
return i;
}
return n;
}
/* if this function fails to get color by using XcmsAllocColor()
* it makes the plots black and white. I should get fancyer than
* this and try other X color routines besides XcmsAllocColor();
* but I don't have the hardware/software to test other X color routines.
* No other X color routines work on my machine.
*/
void init_colors(PlotXwins *win, Plot *plot)
{
int i,n;
Pixel pix;
assert((plot->point_color=(unsigned long *)malloc(
sizeof(unsigned long)*plot->num_plots))!=NULL);
assert((plot->line_color=(unsigned long *)malloc(
sizeof(unsigned long)*plot->num_plots))!=NULL);
n=0;
/**** case large number of plots. So large number of colors
***** try to make a rainbow of colors
****/
if((plot->num_plots > NUM_COLORS) && (!(plot->flag & BLACK_WHITE)))
{
if((n = RainBow(win->display,
DefaultColormap(win->display,win->screen_number),
plot->point_color,plot->num_plots)) != plot->num_plots )
{
fprintf(stderr,"quickplot WARNING: Can't allocate %d colors for plots: switching to BLACK and WHITE mode\n",plot->num_plots - n);
plot->flag |= BLACK_WHITE;
}
}
/*** try getting colors using XcmsAllocColor() **************************/
else if((plot->num_plots <= NUM_COLORS) && !(plot->flag & BLACK_WHITE))
{
if((n = SelectColor(win->display,
DefaultColormap(win->display,win->screen_number),
plot->point_color,plot->num_plots)) != plot->num_plots)
if(plot->flag & VERBOSE)
fprintf(stderr,"quickplot WARNING: Can't allocate colors using XcmsAllocColor()\n");
}
/*** try getting colors using XAllocNamedColor() **************************/
else if((plot->num_plots <= NUM_COLORS) &&
(n != plot->num_plots) &&
!(plot->flag & BLACK_WHITE) )
{
if((plot->num_plots-n) != (n += SelectNamedColors(win->display,
DefaultColormap(win->display,win->screen_number),
&((plot->point_color)[n]),plot->num_plots-n)))
if(plot->flag & VERBOSE)
fprintf(stderr,"quickplot WARNING: Can't allocate %d out of %d colors using XAllocNamedColor() for plots: switching to BLACK and WHITE mode\n",plot->num_plots - n,plot->num_plots);
}
/** if we failed to get plot->num_plots colors above then make it BLack and White
* for the rest of the colors
*/
if(n != plot->num_plots) /* it's going to be black and white */
{
pix = win->BWforeground_pix;
for(i=n;i<plot->num_plots;i++)
(plot->point_color)[i] = pix;
}
/* make line colors the same as point colors for now */
for(i=0;i<plot->num_plots;i++)
(plot->line_color)[i] = (plot->point_color)[i];
/*** get a gray color for axies ***/
if((unsigned long) NULL == (win->grey_pix = MakeRGBColor(win->display,
DefaultColormap(win->display,win->screen_number),0.1,0.1,0.1)))
if((unsigned long) NULL == (win->grey_pix = GetNamedColor(win->display,
DefaultColormap(win->display,win->screen_number), "darkgrey")))
/* use black or white foreground if all the above fails */
win->grey_pix = win->BWforeground_pix;
}
|