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 File Reference
#include <math.h>

Go to the source code of this file.

Macros

#define UNDEFINED   -99999
 

Functions

double Maximum (double r, double g, double b)
 
double Minimum (double r, double g, double b)
 
void RGB_To_HSV (double r, double g, double b, double *h, double *s, double *v)
 
void HSV_To_RGB (double *r, double *g, double *b, double h, double s, double v)
 
void ShiftPalette (unsigned char Palette[769], double DH, double DS, double DV, double DsH, double DsS, double DsV)
 

Macro Definition Documentation

#define UNDEFINED   -99999

Definition at line 22 of file aux_palette.cpp.

Referenced by HSV_To_RGB(), and RGB_To_HSV().

Function Documentation

void HSV_To_RGB ( double *  r,
double *  g,
double *  b,
double  h,
double  s,
double  v 
)

Definition at line 70 of file aux_palette.cpp.

References f, i, int, q, UNDEFINED, and v.

Referenced by ShiftPalette().

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 }
double Maximum ( double  r,
double  g,
double  b 
)

Definition at line 24 of file aux_palette.cpp.

References b.

Referenced by RGB_To_HSV().

25 {
26  if (r > g && r > b)
27  return r;
28  if (b > r && b > g)
29  return b;
30  return g;
31 }
double Minimum ( double  r,
double  g,
double  b 
)

Definition at line 33 of file aux_palette.cpp.

References b.

Referenced by RGB_To_HSV().

34 {
35  if (r < g && r < b)
36  return r;
37  if (b < r && b < g)
38  return b;
39  return g;
40 }
void RGB_To_HSV ( double  r,
double  g,
double  b,
double *  h,
double *  s,
double *  v 
)

Definition at line 42 of file aux_palette.cpp.

References b, max(), Maximum(), min(), Minimum(), and UNDEFINED.

Referenced by ShiftPalette().

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 }
void ShiftPalette ( unsigned char  Palette[769],
double  DH,
double  DS,
double  DV,
double  DsH,
double  DsS,
double  DsV 
)

Definition at line 134 of file aux_palette.cpp.

References b, g, h, HSV_To_RGB(), i, RGB_To_HSV(), and v.

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 }