summaryrefslogtreecommitdiffstats
path: root/Smoke/palette.c
blob: 5a8b9bfffb29f694c672942214bf0470e5b67fe0 (plain)
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
#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#endif


#include <math.h>


#include "funcs.h"

#include "palette.h"


void HSVtoRGB(float *r, float *g, float *b, float h, float s, float v)
{
	int i;
	float f, p, q, t;

	if( s == 0 ) {
		// achromatic (grey)
		*r = *g = *b = v;
		return;
	}

	h /= 60;			// sector 0 to 5
	i = floor(h);
	f = h - i;			// factorial part of h
	p = v * (1 - s);
	q = v * (1 - s * f);
	t = v * (1 - s * (1 - f));

	switch(i) {
		case 0:
			*r = v;
			*g = t;
			*b = p;
			break;
		case 1:
			*r = q;
			*g = v;
			*b = p;
			break;
		case 2:
			*r = p;
			*g = v;
			*b = t;
			break;
		case 3:
			*r = p;
			*g = q;
			*b = v;
			break;
		case 4:
			*r = t;
			*g = p;
			*b = v;
			break;
		default:		// case 5:
			*r = v;
			*g = p;
			*b = q;
			break;
	}

}

// r,g,b values are from 0 to 1
// h = [0,360], s = [0,1], v = [0,1]
//		if s == 0, then h = -1 (undefined)
void RGBtoHSV(float r, float g, float b, float *h, float *s, float *v)
{
  float min, max, delta;

	min = MIN3(r, g, b);
	max = MAX3(r, g, b);
	*v = max;				// v

	delta = max - min;

	if(max != 0) {
		*s = delta / max;		// s
  }	else {
		// r = g = b = 0		// s = 0, v is undefined
		*s = 0;
		*h = -1;
		return;
	}

	if(r == max) {
		*h = ( g - b ) / delta;		// between yellow & magenta
  }	else if(g == max) {
		*h = 2 + (b - r) / delta;	// between cyan & yellow
  } else {
		*h = 4 + (r - g) / delta;	// between magenta & cyan
  }
	*h *= 60;				// degrees
	if(*h < 0) {
		*h += 360;
  }
}


//rainbow: Implements a color palette, mapping the scalar 'value' to a rainbow color RGB
void rainbow(float value, float *R, float *G, float *B)
{                          
   const float dx = 0.8f;

   if (value < 0) value = 0;
   if (value > 1) value = 1;

   value = (6 - 2 * dx) * value + dx;

   *R = (float)max(0.0f, (3 - fabs(value - 4.0f) - fabs(value - 5.0f)) / 2.0f);
   *G = (float)max(0.0f, (4 - fabs(value - 2.0f) - fabs(value - 4.0f)) / 2.0f);
   *B = (float)max(0.0f, (3 - fabs(value - 1.0f) - fabs(value - 2.0f)) / 2.0f);
}

void colormap_fire(float value, float *R, float *G, float *B)
{
	/* Colormap Fire
	 * A fire effect deals with two parts, first a drop from red to yellow (halfway)
	 * during which time the Red component remains full e.g. 1. The Green component is
	 * slowly added to turn the red into orange, and then yellow (R & G =Y). After this
	 * point, the Red and Green component (e.g. yellow) have to drop simulataniously
	 * to go from yellow down to black.
	 */
	*B = 0;
	*G = 0;
	*R = 0;

	if (value <= (0.01)) {
		/* whilst value is 0 - 0.5 both red and green equally change to create yellow */
		*R = *G = value;
	} else {
		/* whilst value is 0.5 - 1 Red is always fully on while the Green component is
		 * added in steps to go from red to orange to yellow.
		 */
		*G = 0.9f - value;
		*R = 0.8f; // not 1, makes red deeper, more intense
	}
}

//set_palette: Sets three different types of colormaps
struct color4f set_palette(int colormap_sort, float value, int num_colors)
{
   float R, G, B;
   struct color4f return_value;
   const int NLEVELS = 7;

   value *= num_colors;
   value = (float)(int)(value);
   value /= num_colors;

   switch (colormap_sort)
   {
     case PALETTE_BLACKWHITE:
       R = G = B = value;
     break;
     case PALETTE_RAINBOW:
       rainbow(value,&R,&G,&B);
     break;
     case PALETTE_BANDS:
       value *= NLEVELS; value = (float)(int)(value); value/= NLEVELS; 
	     rainbow(value,&R,&G,&B);
     break;
     case PALETTE_BLUE_GREEN_RED:
       if  (value < -0.1)
       {
         R = G = 0;
         value -= -0.1f;
         value /= 0.9f;
         B = -value;
       }
       else if (value < 0.1)
       {
         R = B = 0;
         value += 0.1f;
         value /= 0.2f;
         G = value;
       }
       else
       {
         value -= 0.1f;
         value /= 0.9f;
         R = value;
         G = B = 0;
       }
     break;
     case PALETTE_WILRIK:
       colormap_fire(value, &R, &G, &B);
     break;
     case PALETTE_OLIVER:
       rainbow(value, &R, &G, &B);
     break;
     case PALETTE_RED:
      R = 1.0f;
      G = B = 0.0f;
     break;
     case PALETTE_GREEN:
      G = 1.0f;
      R = B = 0.0f;
     break;
     case PALETTE_BLUE:
      B = 1.0f;
      R = G = 0.0f;
     break;
   }

   return_value.r = R;
   return_value.g = G;
   return_value.b = B;

   return return_value;
}