Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
aux_palette.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 #include <math.h>
22 #define UNDEFINED -99999
23 
24 double Maximum( double r, double g, double b )
25 {
26  if (r > g && r > b)
27  return r;
28  if (b > r && b > g)
29  return b;
30  return g;
31 }
32 
33 double Minimum( double r, double g, double b )
34 {
35  if (r < g && r < b)
36  return r;
37  if (b < r && b < g)
38  return b;
39  return g;
40 }
41 
42 void RGB_To_HSV( double r, double g, double b, double *h, double *s, double *v )
43 {
44 /*
45  * given: rgb, each in [0,1]
46  * desired: h in [0,360), s and v in [0,1] except if s = 0 then h = undefined
47  */
48  double max = Maximum( r, g, b );
49  double min = Minimum( r, g, b );
50  *v = max; /*This is the value v. */
51  /* Next calculate saturation, s. Saturation is 0 if red, green and blue are all 0 */
52  *s = (max != 0.0) ? ( (max-min)/max ) : 0.0;
53  if (*s == 0.0) {
54  *h = UNDEFINED;
55  } else {
56  /*Chromatic case: Saturation is not 0*/
57  double delta = max-min; /*Determine Hue*/
58  if (r == max)
59  *h = (g-b)/delta; /* resulting color is between yellow and magenta */
60  else if (g == max)
61  *h = 2.0+(b-r)/delta; /* resulting color is between cyan and yellow */
62  else if (b == max)
63  *h = 4.0+(r-g)/delta; /* resulting color is between magenta and cyan */
64  *h *= 60.0; /*convert hue to degrees*/
65  if (*h < 0.0)
66  *h += 360.0; /*make sure its not negative*/
67  }
68 }
69 
70 void HSV_To_RGB( double *r, double *g, double *b, double h, double s, double v )
71 {
72  if (s == 0.0) {
73  /*
74  * the color is on the black and white center line.
75  *******************************achromatic color: there is no hue.
76  */
77  if (h == UNDEFINED) {
78  *r = v; /* this is the achromatic case. */
79  *g = v;
80  *b = v;
81  } else {
82  *r = v; /* this is the achromatic case. */
83  *g = v;
84  *b = v;
85  }
86  } else {
87  double f, p, q, t; /*chromatic color: s != 0, so there is a hue*/
88  int i;
89  if (h == 360.0) /*360 degrees is equivalent to 0 degrees. */
90  h = 0.0;
91  h /= 60.0; /* h is now in [0,6).*/
92  i = (int) (h); /*Floor returns the greatest integer <= h*/
93  f = h-i; /*f is the fractional part of h.*/
94  p = v*(1.0-s);
95  q = v*( 1.0-(s*f) );
96  t = v*( 1.0-( s*(1.0-f) ) );
97  switch (i)
98  {
99  case 0:
100  *r = v;
101  *g = t;
102  *b = p;
103  break;
104  case 1:
105  *r = q;
106  *g = v;
107  *b = p;
108  break;
109  case 2:
110  *r = p;
111  *g = v;
112  *b = t;
113  break;
114  case 3:
115  *r = p;
116  *g = q;
117  *b = v;
118  break;
119  case 4:
120  *r = t;
121  *g = p;
122  *b = v;
123  break;
124  case 5:
125  *r = v;
126  *g = p;
127  *b = q;
128  break;
129  }
130  } /*Chromatic case */
131 }
132 
133 //desired: h in [0,360), s and v in [0,1] except if s = 0 then h = undefined
134 void ShiftPalette( unsigned char Palette[769], double DH, double DS, double DV, double DsH, double DsS, double DsV )
135 {
136  double r, g, b, h, s, v;
137  int i;
138  for (i = 192; i < 224; i++) {
139  r = ( (double) .003921568627 )*Palette[i*3];
140  g = ( (double) .003921568627 )*Palette[i*3+1];
141  b = ( (double) .003921568627 )*Palette[i*3+2];
142  RGB_To_HSV( r, g, b, &h, &s, &v );
143  h += DH;
144  s += DS;
145  v += DV;
146  if (h >= 360)
147  h -= 360;
148  if (h < 0)
149  h += 360;
150  if (s > 1)
151  s -= 1;
152  if (s < 0)
153  s += 1;
154  if (s == 0)
155  s += .01;
156  if (v > 1)
157  v -= 1;
158  if (v < 0)
159  v += 1;
160  HSV_To_RGB( &r, &g, &b, h, s, v );
161  Palette[i*3] = (unsigned char) (r*255);
162  Palette[i*3+1] = (unsigned char) (g*255);
163  Palette[i*3+2] = (unsigned char) (b*255);
164  }
165  for (i = 224; i < 256; i++) {
166  r = ( (double) .003921568627 )*Palette[i*3];
167  g = ( (double) .003921568627 )*Palette[i*3+1];
168  b = ( (double) .003921568627 )*Palette[i*3+2];
169  RGB_To_HSV( r, g, b, &h, &s, &v );
170  h += DsH;
171  s += DsS;
172  v += DsV;
173  if (h >= 360)
174  h -= 360;
175  if (h < 0)
176  h += 360;
177  if (s > 1)
178  s -= 1;
179  if (s < 0)
180  s += 1;
181  if (s == 0)
182  s += .01;
183  if (v > 1)
184  v -= 1;
185  if (v < 0)
186  v += 1;
187  HSV_To_RGB( &r, &g, &b, h, s, v );
188  Palette[i*3] = (unsigned char) (r*255);
189  Palette[i*3+1] = (unsigned char) (g*255);
190  Palette[i*3+2] = (unsigned char) (b*255);
191  }
192 }
193