Index: image_captcha.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/captcha/image_captcha/image_captcha.module,v
retrieving revision 1.1.4.25
diff -u -u -p -r1.1.4.25 image_captcha.module
--- image_captcha.module	27 Nov 2007 23:47:02 -0000	1.1.4.25
+++ image_captcha.module	14 Mar 2008 21:42:29 -0000
@@ -189,6 +189,29 @@ function image_captcha_settings_form() {
     ),
   );
 
+  // color settings
+  $form['image_captcha_color_settings'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Color settings'),
+    '#description' => t('Configuration of the colors of background and foreground in the image CAPTCHA'),
+  );
+  $form['image_captcha_color_settings']['image_captcha_background_color'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Background color'),
+    '#description' => t('Enter the hexadecimal code for the background color.'),
+    '#default_value' => variable_get('image_captcha_background_color', '#ffffff'),
+    '#maxlength' => 7,
+    '#size' => 7,
+  );
+  $form['image_captcha_color_settings']['image_captcha_foreground_color'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Foreground color'),
+    '#description' => t('Enter the hexadecimal code for the foreground color.'),
+    '#default_value' => variable_get('image_captcha_foreground_color', '#000000'),
+    '#maxlength' => 7,
+    '#size' => 7,
+  );
+
   // distortion and noise settings
   $form['image_captcha_distortion_and_noise'] = array(
     '#type' => 'fieldset',
@@ -281,6 +304,13 @@ function image_captcha_settings_form_val
     elseif ($font != 'BUILTIN' && (!is_file($font) || !is_readable($font))) {
       form_set_error('image_captcha_font', t('Font does not exist or is not readable.'));
     }
+    // check color settings
+    if (!preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $form_values['image_captcha_background_color'])){
+      form_set_error('image_captcha_background_color', t('Background color is not a valid hexadecimal color value.'));
+    }
+    if (!preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $form_values['image_captcha_foreground_color'])){
+      form_set_error('image_captcha_foreground_color', t('Foreground color is not a valid hexadecimal color value.'));
+    }
   }
 }
 
@@ -412,6 +442,21 @@ function image_captcha_image($seed=NULL)
   }
 }
 
+/**
+ * small helper function for parsing a hexadecimal color to a RGB tuple
+ */
+function _image_captcha_hex_to_rgb($hex) {
+  // handle #RGB format
+  if (strlen($hex) == 4) {
+    $hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
+  }
+  $c = hexdec($hex);
+  $rgb = array();
+  for ($i = 16; $i >= 0; $i -= 8) {
+    $rgb[] = ($c >> $i) & 0xFF;
+  }
+  return $rgb;
+}
 
 /**
  * base function for generating a image CAPTCHA
@@ -437,8 +482,9 @@ function _image_captcha_generate_image($
     return FALSE;
   }
 
-  // background
-  $background_color = imagecolorallocate($image, 255, 255, 255);
+  // get background color and paint
+  $background_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_background_color', '#ffffff'));
+  $background_color = imagecolorallocate($image, $background_rgb[0], $background_rgb[1], $background_rgb[2]);
   imagefilledrectangle($image, 0, 0, $width, $height, $background_color);
 
   // draw text
@@ -575,7 +621,12 @@ function _image_captcha_image_generator_
   }
 }
 
-function _image_captcha_image_generator_print_string(&$image, $width, $height, $font, $font_size, $text, $light_colors=FALSE) {
+/**
+ * Helper function for drawing text on the image
+ * 
+ * @param $background_mode if the text is for the background of the double vision mode
+ */
+function _image_captcha_image_generator_print_string(&$image, $width, $height, $font, $font_size, $text, $background_mode=FALSE) {
   // get characters
   $characters = _image_captcha_utf8_split($text);
   $character_quantity = count($characters);
@@ -597,6 +648,20 @@ function _image_captcha_image_generator_
   // character jittering
   $jittering_x = .3 * $font_size;
   $jittering_y = .3 * $font_size;
+
+  // get colors
+  $background_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_background_color', '#ffffff'));
+  $background_color = imagecolorallocate($image, $background_rgb[0], $background_rgb[1], $background_rgb[2]);
+  $foreground_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_foreground_color', '#000000'));
+  $foreground_color = imagecolorallocate($image, $foreground_rgb[0], $foreground_rgb[1], $foreground_rgb[2]);
+  
+  if ($background_mode) {
+    $foreground_color = imagecolorallocate($image, .75*$background_rgb[0]+.25*$foreground_rgb[0], .75*$background_rgb[1]+.25*$foreground_rgb[1], .75*$background_rgb[2]+.25*$foreground_rgb[2]);
+  }
+  
+  // set text color
+  $color = $foreground_color;
+
   // start cursor
   $x = $spacing;
   foreach ($characters as $character) {
@@ -608,13 +673,7 @@ function _image_captcha_image_generator_
     }
     // calculate y position
     $y = .5 * ($height - $character_height);
-    // generate random color
-    if ($light_colors) {
-      $color = imagecolorallocate($image, mt_rand(128, 255), mt_rand(128, 255), mt_rand(128, 255));
-    }
-    else {
-      $color = imagecolorallocate($image, mt_rand(0, 127), mt_rand(0, 127), mt_rand(0, 127));
-    }
+
     // add jitter to position
     $pos_x = $x + mt_rand(-$jittering_x, $jittering_x);
     $pos_y = $y + mt_rand(-$jittering_y, $jittering_y);
