added preprocessor definitions for BD engine start and end element
[rocksndiamonds.git] / src / game_bd / bd_colors.c
index 57f02ab0dd9294664bce0af4fab304f81d85e42f..7a9a35aa460ff588b4e2a07c8b13de67ad4e7900 100644 (file)
 #include "main_bd.h"
 
 
-static int gd_c64_palette = 0;
-static int gd_c64dtv_palette = 0;
-static int gd_atari_palette = 0;
-
 static char *c64_color_names[] =
 {
   "Black",  "White", "Red",      "Cyan",  "Purple", "Green",      "Blue",      "Yellow",
@@ -86,8 +82,8 @@ static unsigned int c64_colors_rtadash[]={
   0xe66c00, 0x935f00, 0xff7c64, 0x6c6c6c, 0xa1a1a1, 0xafff4d, 0x9778ff, 0xd8d8d8,
 };
 
-/* make sure that pointeres and names match! */
-static unsigned int *c64_palette_pointers[] =
+// pointer array positions must match palette numbers in header file
+static unsigned int *c64_palettes_pointers[] =
 {
     c64_colors_vice_new,
     c64_colors_vice_old,
@@ -440,6 +436,7 @@ static byte XFormer[] =
   190,133,230,198,145,234,202,153,234,214,157,238,218,161
 };
 
+// pointer array positions must match palette numbers in header file
 static const byte *atari_palettes_pointers[] =
 {
   BuiltIn,
@@ -535,6 +532,7 @@ static byte Murray[] =
   39,185,212,57,208,232,80,225,249,89,238,255,107,255,255,126
 };
 
+// pointer array positions must match palette numbers in header file
 static const byte *c64dtv_palettes_pointers[] =
 {
   Spiff,
@@ -555,12 +553,22 @@ GdColor gd_atari_color(int index)
   return (GD_COLOR_TYPE_ATARI << 24) + index;
 }
 
+GdColor gd_atari_color_huesat(int hue, int sat)
+{
+  return gd_atari_color(16 * hue + sat);
+}
+
 // return c64dtv color with index.
 GdColor gd_c64dtv_color(int index)
 {
   return (GD_COLOR_TYPE_C64DTV << 24) + index;
 }
 
+GdColor gd_c64dtv_color_huesat(int hue, int sat)
+{
+  return gd_c64dtv_color(16 * hue + sat);
+}
+
 // return "unknown color"
 static GdColor unknown_color(void)
 {
@@ -582,25 +590,28 @@ GdColor gd_color_get_rgb(GdColor color)
       return color;
 
     case GD_COLOR_TYPE_C64:
-      if (gd_c64_palette < 0 || gd_c64_palette >= ARRAY_SIZE(c64_palette_pointers) - 1)
-       gd_c64_palette = 0;    // silently switch to default, if invalid value
-      c64_pal = c64_palette_pointers[gd_c64_palette];
+      if (setup.bd_palette_c64 < 0 ||
+         setup.bd_palette_c64 >= ARRAY_SIZE(c64_palettes_pointers) - 1)
+       setup.bd_palette_c64 = 0;       // silently switch to default, if invalid value
+      c64_pal = c64_palettes_pointers[setup.bd_palette_c64];
       index = color & 0x0f;
       return c64_pal[index];
 
     case GD_COLOR_TYPE_C64DTV:
-      if (gd_c64dtv_palette < 0 || gd_c64dtv_palette >= ARRAY_SIZE(c64dtv_palettes_pointers) - 1)
-       gd_c64dtv_palette = 0;
-      c64dtv_pal = c64dtv_palettes_pointers[gd_c64dtv_palette];
+      if (setup.bd_palette_c64dtv < 0 ||
+         setup.bd_palette_c64dtv >= ARRAY_SIZE(c64dtv_palettes_pointers) - 1)
+       setup.bd_palette_c64dtv = 0;    // silently switch to default, if invalid value
+      c64dtv_pal = c64dtv_palettes_pointers[setup.bd_palette_c64dtv];
       index = color & 0xff;
       return gd_color_get_from_rgb(c64dtv_pal[index * 3],
                                   c64dtv_pal[index * 3 + 1],
                                   c64dtv_pal[index * 3 + 2]);
 
     case GD_COLOR_TYPE_ATARI:
-      if (gd_atari_palette < 0 || gd_atari_palette >= ARRAY_SIZE(atari_palettes_pointers) - 1)
-       gd_atari_palette = 0;
-      atari_pal = atari_palettes_pointers[gd_atari_palette];
+      if (setup.bd_palette_atari < 0 ||
+         setup.bd_palette_atari >= ARRAY_SIZE(atari_palettes_pointers) - 1)
+       setup.bd_palette_atari = 0;     // silently switch to default, if invalid value
+      atari_pal = atari_palettes_pointers[setup.bd_palette_atari];
       index = color & 0xff;
       return gd_color_get_from_rgb(atari_pal[index * 3],
                                   atari_pal[index * 3 + 1],
@@ -632,6 +643,36 @@ GdColor gd_color_get_from_rgb(int r, int g, int b)
   return (GD_COLOR_TYPE_RGB << 24) + (r << 16) + (g << 8) + b;
 }
 
+// make up GdColor from h,s,v values.
+// h=0..360, s=0..1, v=0..1
+GdColor gd_color_get_from_hsv(double h, double s, double v)
+{
+  int hi = (int)(h / 60) % 6;
+  double f = h / 60 - (int)(h / 60);    // fractional part
+  double p = v * (1 - s);
+  double q = v * (1 - f * s);
+  double t = v * (1 - (1 - f) * s);
+
+  v *= 255;
+  p *= 255;
+  q *= 255;
+  t *= 255;
+
+  switch(hi)
+  {
+    case 0: return gd_color_get_from_rgb(v, t, p);
+    case 1: return gd_color_get_from_rgb(q, v, p);
+    case 2: return gd_color_get_from_rgb(p, v, t);
+    case 3: return gd_color_get_from_rgb(p, q, v);
+    case 4: return gd_color_get_from_rgb(t, p, v);
+    case 5: return gd_color_get_from_rgb(v, p, q);
+  }
+
+  // no way we reach this
+  return gd_color_get_from_rgb(0, 0, 0);
+}
+
+
 GdColor gd_color_get_from_string(const char *color)
 {
   int i, r, g, b;