1
2 #include <math.h>
3 #include "mathlocal.h"
4
5 #define LOGISTIC(a) (1/( 1 + exp(-(a)) ))
6 #define LOGISTIC2(a) (2/( exp(a) + exp(-(a)) ))
7
8 extern int cloudy;
9
10 char *
MakeColorName(buf,r,g,b)11 MakeColorName( buf, r, g, b )
12 char *buf;
13 double r, g, b;
14 {
15 sprintf( buf, "#%02X%02X%02X",
16 (unsigned int)(LOGISTIC(r) * 254)+1,
17 (unsigned int)(LOGISTIC(g) * 254)+1,
18 (unsigned int)(LOGISTIC(b) * 254)+1);
19
20 return buf;
21 }
22
23 void
GetSkyColor(rp,gp,bp,z,h,t,sun_h)24 GetSkyColor( rp, gp, bp, z, h, t, sun_h )
25 double *rp, *gp, *bp;
26 double z; /* 1:Noon ... -1:Midnight */
27 double h; /* 1:Heaven ... 0:Horizon */
28 double t; /* 1:Sunward ... 0: */
29 double sun_h; /* 1:sun at heaven ... 0:at horizon */
30 {
31 double r, g, b, white;
32
33 white = LOGISTIC(25*(z+0.15));
34 white *= z * pow( 1-h, 5.0 ) + (0.3*t+0.7)*LOGISTIC2(3.0*(h-sun_h)) + 2.55;
35 white += -2.50;
36
37 r = g = b = white;
38
39 if( !cloudy ) {
40 double yellow, orange, red, e;
41
42 e = t * LOGISTIC2(3.5*(z+0.00));
43 yellow = e * LOGISTIC2(6.5*(h-0.20));
44 red = e * LOGISTIC2(12.0*(h+0.01));
45
46 g *= 1.10;
47
48 r += (-2.60 + 4.20*yellow + 3.20*red) / 3;
49 g += ( 0.00 - 0.75*yellow - 1.80*red) / 3;
50 b += ( 2.20 - 5.20*yellow - 5.40*red) / 3;
51 }
52
53 if( rp ) *rp = r;
54 if( gp ) *gp = g;
55 if( bp ) *bp = b;
56 }
57
58 char *
GetSkyColorName(buf,z,h,t,sun_h)59 GetSkyColorName( buf, z, h, t, sun_h )
60 char *buf;
61 double z;
62 double h;
63 double t;
64 double sun_h;
65 {
66 double r, g, b;
67
68 if( h>1 || h<0 ) {
69 strcpy( buf, "#000000" );
70 return buf;
71 }
72
73 if( sun_h < 0 )
74 sun_h = 0;
75 else if( sun_h > 1 )
76 sun_h = 1;
77
78 GetSkyColor( &r, &g, &b, z, h, t, sun_h );
79
80 return MakeColorName( buf, r, g, b );
81 }
82 char *
GetSolarColorName(buf,z)83 GetSolarColorName( buf, z )
84 char *buf;
85 double z; /* 1:Noon ... -1:Midnight */
86 {
87 double r,g,b;
88
89 r = 20;
90 g = 21 - 21*LOGISTIC2(2.4*(z+0.02));
91 b = 20 - 25*LOGISTIC2(2.4*(z+0.02));
92
93 return MakeColorName( buf, r, g, b );
94 }
95 char *
GetStarColorName(buf,z)96 GetStarColorName( buf, z )
97 char *buf;
98 double z; /* 1:Noon ... -1:Midnight */
99 {
100 double red, green, blue;
101
102 red = 100;
103 green = 100;
104 blue = 100;
105
106 return MakeColorName( buf, red, green, blue );
107 }
108