⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colormaps.c

📁 地震波正演和显示模块
💻 C
📖 第 1 页 / 共 2 页
字号:
		
	if (verbose == 1)
		warn (" using : \"cmap=rgb%i\"", c_nr);


if(depth<=8){
	
	/* determine beginning and ending pixels in contiguous range	*/
	bpixel = xGetFirstPixel(dpy);
	epixel = xGetLastPixel(dpy);
	if (epixel<=bpixel) return None;
	
	
	/* create new colormap and allocate all cells read/write */
	cmap = XCreateColormap(dpy,win,DefaultVisualOfScreen(scr),AllocNone);
	ncells = CellsOfScreen(scr);
	XAllocColorCells(dpy,cmap,True,NULL,0,pixel,ncells);
	
	/* copy color cells from window's colormap to new colormap */
	for (i=0; i<ncells; ++i) {
		if (i<bpixel || i>epixel) {
			color.pixel = i;
			XQueryColor(dpy,wcmap,&color);
			XFreeColors(dpy,cmap,&pixel[i],1,0);
			XAllocColor(dpy,cmap,&color);
		}
	}
	
	/* build scale in contiguous cells in new colormap */
	npixels = epixel-bpixel+1;
	half = npixels / 2;
	

	/* Build the 1st ramp						*/
	for (ih = 0; ih < half; ++ih) {
		color.pixel = bpixel + ih;
		color.red   = c_rgb[c_nr][0][0] +
			(c_rgb[c_nr][1][0] - c_rgb[c_nr][0][0]) * ((float) ih)/((float) half);
		color.green = c_rgb[c_nr][0][1] +
			(c_rgb[c_nr][1][1] - c_rgb[c_nr][0][1]) * ((float) ih)/((float) half);
		color.blue  = c_rgb[c_nr][0][2] +
			(c_rgb[c_nr][1][2] - c_rgb[c_nr][0][2]) * ((float) ih)/((float) half);
		
		color.red   *= 257.0;
		color.green *= 257.0;
		color.blue  *= 257.0;
		
		color.flags = DoRed|DoGreen|DoBlue;
		XStoreColor(dpy,cmap,&color);
	}

	/* Build the 2nd ramp						*/
	for (ih=half; ih<npixels; ++ih) {
		color.pixel = bpixel+ih;
		color.red   = c_rgb[c_nr][1][0] +
			(c_rgb[c_nr][2][0] - c_rgb[c_nr][1][0]) * ((float) (ih-half))/((float) half);
		color.green = c_rgb[c_nr][1][1] +
			(c_rgb[c_nr][2][1] - c_rgb[c_nr][1][1]) * ((float) (ih-half))/((float) half);
		color.blue  = c_rgb[c_nr][1][2] +
			(c_rgb[c_nr][2][2] - c_rgb[c_nr][1][2]) * ((float) (ih-half))/((float) half);
		
		color.red   *= 257.0;
		color.green *= 257.0;
		color.blue  *= 257.0;
		
		color.flags = DoRed|DoGreen|DoBlue;
		XStoreColor(dpy,cmap,&color);
	}

	/* return colormap */
	return cmap;
}
else{


        /* Build the 1st ramp                                           */
        for (ih = 0; ih < 128; ++ih) {
                color.red   = c_rgb[c_nr][0][0] +
                        (c_rgb[c_nr][1][0] - c_rgb[c_nr][0][0]) * ((float) ih)/((float) 128);
                color.green = c_rgb[c_nr][0][1] +
                        (c_rgb[c_nr][1][1] - c_rgb[c_nr][0][1]) * ((float) ih)/((float) 128);
                color.blue  = c_rgb[c_nr][0][2] +
                        (c_rgb[c_nr][1][2] - c_rgb[c_nr][0][2]) * ((float) ih)/((float) 128);
                
                color.red   *= 257.0;
                color.green *= 257.0;
                color.blue  *= 257.0;
        
                XAllocColor(dpy,wcmap,&color);
		truecolor_pixel[ih]=(unsigned long)color.pixel;
        }


        /* Build the 2nd ramp                                           */
        for (ih=128; ih<256; ++ih) {
                color.red   = c_rgb[c_nr][1][0] +
                        (c_rgb[c_nr][2][0] - c_rgb[c_nr][1][0]) * ((float) (ih-128))/((float) 128);
                color.green = c_rgb[c_nr][1][1] +
                        (c_rgb[c_nr][2][1] - c_rgb[c_nr][1][1]) * ((float) (ih-128))/((float) 128);
                color.blue  = c_rgb[c_nr][1][2] +
                        (c_rgb[c_nr][2][2] - c_rgb[c_nr][1][2]) * ((float) (ih-128))/((float) 128);
        
                color.red   *= 257.0;
                color.green *= 257.0;
                color.blue  *= 257.0;
 
                XAllocColor(dpy,wcmap,&color);
		truecolor_pixel[ih]=(unsigned long)color.pixel;
        }

	return wcmap;

}


}



Colormap xCreateHSVColormap (Display *dpy, Window win,
			char *str_cmap, int verbose)
{
	Screen *scr=XDefaultScreenOfDisplay(dpy);
	/* Window root=XRootWindowOfScreen(scr); --unused? */
	Colormap cmap,wcmap;
	XColor color;
	XWindowAttributes wa;
	int i,ncells;
	unsigned long npixels;
	unsigned long bpixel,epixel,pixel[4096];



	static int	c_nr = -1;
	unsigned long	max_cmap, half, ih;
	float r,g,b, h,s,v;	/* Red,Green,Blue, Hue,Sat,Value	*/
	unsigned int depth, sr;

        sr=DefaultScreen(dpy);
        depth=(unsigned int)DefaultDepth(dpy,sr);
	

	/* determine window's current colormap */
	XGetWindowAttributes(dpy,win,&wa);
	wcmap = wa.colormap;

	max_cmap = sizeof (c_hsv) / sizeof (float[3][3]);
	
	/* We got the specific number of the cmap from the string	*/
	if (STREQ (str_cmap, "hsv_up"))
		c_nr++;
	else if (STREQ (str_cmap, "hsv_down"))
		c_nr--;
	else	{
		if (strlen (str_cmap) > 3)	{
			str_cmap[0] = str_cmap[1] = str_cmap[2] = ' ';
			c_nr = atoi (str_cmap);
			if (c_nr < 0 || c_nr >= max_cmap)	{
				warn ("\"cmap=hsv%i\" not installed !", c_nr);
				c_nr = 0;
				warn (" using : \"cmap=hsv%i\"", c_nr);
			}
		}
	}

	/* cycle through the cmaps					*/
	while (c_nr < 0)
		c_nr += max_cmap;
		
	while (c_nr >= max_cmap)
		c_nr -= max_cmap;
		
	if (verbose == 1)
		warn (" using : \"cmap=hsv%i\"", c_nr);


if(depth<=8){
	
	/* determine beginning and ending pixels in contiguous range	*/
	bpixel = xGetFirstPixel(dpy);
	epixel = xGetLastPixel(dpy);
	if (epixel<=bpixel) return None;
	
	
	/* create new colormap and allocate all cells read/write */
	cmap = XCreateColormap(dpy,win,DefaultVisualOfScreen(scr),AllocNone);
	ncells = CellsOfScreen(scr);
	XAllocColorCells(dpy,cmap,True,NULL,0,pixel,ncells);
	
	/* copy color cells from window's colormap to new colormap */
	for (i=0; i<ncells; ++i) {
		if (i<bpixel || i>epixel) {
			color.pixel = i;
			XQueryColor(dpy,wcmap,&color);
			XFreeColors(dpy,cmap,&pixel[i],1,0);
			XAllocColor(dpy,cmap,&color);
		}
	}
	
	/* build scale in contiguous cells in new colormap */
	npixels = epixel-bpixel+1;
	half = npixels / 2;
	
	
	/* Build the 1st ramp						*/
	for (ih = 0; ih < half; ++ih) {
		color.pixel = bpixel + ih;
		
		h = c_hsv[c_nr][0][0] +
			(c_hsv[c_nr][1][0] - c_hsv[c_nr][0][0]) * ((float) ih) /((float) half);
		s = c_hsv[c_nr][0][1] +
			(c_hsv[c_nr][1][1] - c_hsv[c_nr][0][1]) * ((float) ih) / ((float) half);
		v = c_hsv[c_nr][0][2] +
			(c_hsv[c_nr][1][2] - c_hsv[c_nr][0][2]) * ((float) ih) / ((float) half);
		
		hsv2rgb (h, s, v, &r, &g, &b);
		color.red   = 65535.0 * r;
		color.green = 65535.0 * g;
		color.blue  = 65535.0 * b;
		
		color.flags = DoRed|DoGreen|DoBlue;
		XStoreColor(dpy,cmap,&color);
	}

	/* Build the 2nd ramp						*/
	for (ih = half; ih < npixels; ++ih) {
		color.pixel = bpixel + ih;
		
		h = c_hsv[c_nr][1][0] +
			(c_hsv[c_nr][2][0] - c_hsv[c_nr][1][0]) * ((float) (ih-half))/((float) half);
		s = c_hsv[c_nr][1][1] +
			(c_hsv[c_nr][2][1] - c_hsv[c_nr][1][1]) * ((float) (ih-half))/((float) half);
		v = c_hsv[c_nr][1][2] +
			(c_hsv[c_nr][2][2] - c_hsv[c_nr][1][2]) * ((float) (ih-half))/((float) half);
		
		hsv2rgb (h, s, v, &r, &g, &b);
		color.red   = 65535.0 * r;
		color.green = 65535.0 * g;
		color.blue  = 65535.0 * b;
		
		color.flags = DoRed|DoGreen|DoBlue;
		XStoreColor(dpy,cmap,&color);
	}
		
	/* return colormap */
	return cmap;

}else{

        /* Build the 1st ramp                                           */
        for (ih = 0; ih < 128; ++ih) {
                h = c_hsv[c_nr][0][0] + 
                        (c_hsv[c_nr][1][0] - c_hsv[c_nr][0][0]) * ((float) ih) /((float) 128);
                s = c_hsv[c_nr][0][1] +
                        (c_hsv[c_nr][1][1] - c_hsv[c_nr][0][1]) * ((float) ih) / ((float) 128);
                v = c_hsv[c_nr][0][2] +
                        (c_hsv[c_nr][1][2] - c_hsv[c_nr][0][2]) * ((float) ih) / ((float) 128);
        
                hsv2rgb (h, s, v, &r, &g, &b);
                color.red   = 65535.0 * r;
                color.green = 65535.0 * g;
                color.blue  = 65535.0 * b;
                XAllocColor(dpy,wcmap,&color);
                truecolor_pixel[ih]=(unsigned long)color.pixel;
        }

        /* Build the 2nd ramp                                           */
        for (ih = 128; ih < 256; ++ih) {
                h = c_hsv[c_nr][1][0] +
                        (c_hsv[c_nr][2][0] - c_hsv[c_nr][1][0]) * ((float) (ih-128))/((float) 128);
                s = c_hsv[c_nr][1][1] +
                        (c_hsv[c_nr][2][1] - c_hsv[c_nr][1][1]) * ((float) (ih-128))/((float) 128);
                v = c_hsv[c_nr][1][2] +
                        (c_hsv[c_nr][2][2] - c_hsv[c_nr][1][2]) * ((float) (ih-128))/((float) 128);
                
                hsv2rgb (h, s, v, &r, &g, &b);
                color.red   = 65535.0 * r;
                color.green = 65535.0 * g;
                color.blue  = 65535.0 * b;

                XAllocColor(dpy,wcmap,&color);
                truecolor_pixel[ih]=(unsigned long)color.pixel;
        }

        /* return colormap */
        return wcmap;


}


}


/* internal functions to convert HSV to RGB */
static float rgbvalue (float n1, float n2, float hue)
{
	while (hue > 360.0)
		hue -= 360.0;
	while (hue < 0.0)
		hue += 360.0;
	
	if (hue < 60.0)
		return n1 + (n2 - n1) * hue / 60.0;
	else if (hue<180.0)
		return n2;
	else if (hue < 240.0)
		return n1 + (n2 - n1) * (240.0 - hue) / 60.0;
	else
		return n1;
}

/*
 * variable	range
 * --------	------------
 *	h	0.0 .. 360.0
 *	s	0.0 .. 1.0
 *	v	0.0 .. 1.0
 */
static void hsv2rgb (float h, float s, float v, float *r, float *g, float *b)
{
	float m1,m2;
	/* float rgbvalue (float,float,float);*/
	
	if (v <= 0.5)
		m2 = v * (1.0 + s);
	else
		m2 = v + s - v * s;
	m1 = 2 * v - m2;
	if (s == 0.0) {
                *r = *g = *b = v;
	} else {
		*r = rgbvalue(m1, m2, h + 120.0);
		*g = rgbvalue(m1, m2, h);
		*b = rgbvalue(m1, m2, h - 120.0);

		if (*r > 1.0)
			*r = 1.0;
		if (*g > 1.0)
			*g = 1.0;
		if (*b > 1.0)
			*b = 1.0;
	}
}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -