File: sp2_color.c

package info (click to toggle)
garlic 1.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,516 kB
  • sloc: ansic: 52,465; makefile: 2,254
file content (194 lines) | stat: -rw-r--r-- 5,449 bytes parent folder | download | duplicates (5)
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
/* Copyright (C) 2000-2002 Damir Zucic */

/*=============================================================================

				sp2_color.c

Purpose:
	Prepare spacefill color, version 2. In this function white, left,
	middle, right and black color are used.

Input:
	(1) Pointer to AtomS structure.
	(2) Pointer to GUIS structure.
	(3) Cosine of the angle between the vector from sphere center and
	    the current pixel and  the light source unit vector.  It will
	    be used to calculate the angle.

Output:
	(1) Return value.

Return value:
	The pixel (color) value.

Notes:
	(1) This function may be quite slow (a lot of math).

=============================================================================*/

#include <stdio.h>

#include <math.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>

#include "defines.h"
#include "typedefs.h"

/*======spacefill color:=====================================================*/

unsigned long Sp2Color_ (AtomS *curr_atomSP, GUIS *guiSP, double cos_angle)
{
static long double	angle;
static long double	angle1 = 10.0, angle2 = 40.0;
static long double	angle3 = 80.0, angle4 = 130.0;
static long double	delta_angle;
static long double	w0 = 0.100000, w1 = 0.033333;
static long double	w2 = 0.025000, w3 = 0.020000;
static unsigned long	colorID;
static unsigned long	red_mask, green_mask, blue_mask;
static unsigned long	red1, red2, red;
static unsigned long	green1, green2, green;
static unsigned long	blue1, blue2, blue;
static long double	r1, r2, g1, g2, b1, b2;
static long double	r, g, b;

/* Calculate the angle, in degrees: */
if (cos_angle <= -1.0) angle = 180.0;
else if (cos_angle >= 1.0) angle = 0.0;
else angle = RAD_TO_DEG * acos (cos_angle);

/* Copy masks: */
red_mask   = guiSP->visual_infoS.red_mask;
green_mask = guiSP->visual_infoS.green_mask;
blue_mask  = guiSP->visual_infoS.blue_mask;

/* Find sector and prepare color components: */
if (angle >= angle4)
	{
	return (guiSP->black_colorID);
	}

else if (angle >= angle3)
	{
	/* Extract input color components: */
	red1   = curr_atomSP->right_colorID & red_mask;
	red2   = guiSP->black_colorID       & red_mask;
	green1 = curr_atomSP->right_colorID & green_mask;
	green2 = guiSP->black_colorID       & green_mask;
	blue1  = curr_atomSP->right_colorID & blue_mask;
	blue2  = guiSP->black_colorID       & blue_mask;

	/* Convert to doubles: */
	r1 = (long double) red1;
	r2 = (long double) red2;
	g1 = (long double) green1;
	g2 = (long double) green2;
	b1 = (long double) blue1;
	b2 = (long double) blue2;

	/* Calculate new color components: */
	delta_angle = angle - angle3;
	r = r1 + w3 * (r2 - r1) * delta_angle;
	g = g1 + w3 * (g2 - g1) * delta_angle;
	b = b1 + w3 * (b2 - b1) * delta_angle;
	}

else if (angle >= angle2)
	{
	/* Extract input color components: */
	red1   = curr_atomSP->middle_colorID & red_mask;
	red2   = curr_atomSP->right_colorID  & red_mask;
	green1 = curr_atomSP->middle_colorID & green_mask;
	green2 = curr_atomSP->right_colorID  & green_mask;
	blue1  = curr_atomSP->middle_colorID & blue_mask;
	blue2  = curr_atomSP->right_colorID  & blue_mask;

	/* Convert to doubles: */
	r1 = (long double) red1;
	r2 = (long double) red2;
	g1 = (long double) green1;
	g2 = (long double) green2;
	b1 = (long double) blue1;
	b2 = (long double) blue2;

	/* Calculate new color components: */
	delta_angle = angle - angle2;
	r = r1 + w2 * (r2 - r1) * delta_angle;
	g = g1 + w2 * (g2 - g1) * delta_angle;
	b = b1 + w2 * (b2 - b1) * delta_angle;
	}

else if (angle >= angle1)
	{
	/* Extract input color components: */
	red1   = curr_atomSP->left_colorID   & red_mask;
	red2   = curr_atomSP->middle_colorID & red_mask;
	green1 = curr_atomSP->left_colorID   & green_mask;
	green2 = curr_atomSP->middle_colorID & green_mask;
	blue1  = curr_atomSP->left_colorID   & blue_mask;
	blue2  = curr_atomSP->middle_colorID & blue_mask;

	/* Convert to doubles: */
	r1 = (long double) red1;
	r2 = (long double) red2;
	g1 = (long double) green1;
	g2 = (long double) green2;
	b1 = (long double) blue1;
	b2 = (long double) blue2;

	/* Calculate new color components: */
	delta_angle = angle - angle1;
	r = r1 + w1 * (r2 - r1) * delta_angle;
	g = g1 + w1 * (g2 - g1) * delta_angle;
	b = b1 + w1 * (b2 - b1) * delta_angle;
	}

else
	{
	/* Extract input color components: */
	red1   = guiSP->white_colorID      & red_mask;
	red2   = curr_atomSP->left_colorID & red_mask;
	green1 = guiSP->white_colorID      & green_mask;
	green2 = curr_atomSP->left_colorID & green_mask;
	blue1  = guiSP->white_colorID      & blue_mask;
	blue2  = curr_atomSP->left_colorID & blue_mask;

	/* Convert to doubles: */
	r1 = (long double) red1;
	r2 = (long double) red2;
	g1 = (long double) green1;
	g2 = (long double) green2;
	b1 = (long double) blue1;
	b2 = (long double) blue2;

	/* Calculate new color components: */
	delta_angle = angle;
	r = r1 + w0 * (r2 - r1) * delta_angle;
	g = g1 + w0 * (g2 - g1) * delta_angle;
	b = b1 + w0 * (b2 - b1) * delta_angle;
	}

/* Check new color components: */
if (r < 0.0) r = 0.0;
if (g < 0.0) g = 0.0;
if (b < 0.0) b = 0.0;

/* Convert new color components: */
red   = ((unsigned long) r) & red_mask;
green = ((unsigned long) g) & green_mask;
blue  = ((unsigned long) b) & blue_mask;

/* Combine new color components: */
colorID = red | green | blue;

/* Return the pixel value: */
return colorID;
}

/*===========================================================================*/