Index: designkit.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/designkit/designkit.admin.inc,v
retrieving revision 1.1.2.5
diff -u -p -r1.1.2.5 designkit.admin.inc
--- designkit.admin.inc	12 Aug 2010 19:43:27 -0000	1.1.2.5
+++ designkit.admin.inc	28 Oct 2010 10:45:41 -0000
@@ -5,6 +5,7 @@
  */
 function _designkit_form_alter(&$form, &$form_state) {
   $info = designkit_get_info();
+  $theme = $form['#parameters'][2];
 
   // Allow uploads
   $form['#attributes'] = array('enctype' => 'multipart/form-data');
@@ -87,15 +88,26 @@ function _designkit_form_alter(&$form, &
       );
     }
   }
+
+  $stylesheet = variable_get('designkit_stylesheet', array());
+  $form['designkit_stylesheet'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Save colors and images into a linked stylesheet'),
+    '#default_value' => isset($stylesheet[$theme]),
+  );
 }
 
 /**
  * Submit handler for system_theme_settings.
  */
 function _designkit_system_theme_settings_submit(&$form, &$form_state) {
+  $stylesheet = variable_get('designkit_stylesheet', array());
+  $theme = $form['#parameters'][2];
+
   if ($form_state['values']['op'] == t('Reset to defaults')) {
     variable_del('designkit_image');
     variable_del('designkit_color');
+    unset($stylesheet[$theme]);
   }
   else {
     if (isset($form_state['values']['designkit_image'])) {
@@ -104,7 +116,16 @@ function _designkit_system_theme_setting
     if (isset($form_state['values']['designkit_color'])) {
       variable_set('designkit_color', $form_state['values']['designkit_color']);
     }
+    if (!empty($form_state['values']['designkit_stylesheet'])) {
+      $stylesheet[$theme] = TRUE;
+    }
+    else {
+      unset($stylesheet[$theme]);
+    }
   }
+
+  variable_set('designkit_stylesheet', $stylesheet);
+  drupal_clear_css_cache();
 }
 
 /**
Index: designkit.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/designkit/designkit.module,v
retrieving revision 1.1.2.8
diff -u -p -r1.1.2.8 designkit.module
--- designkit.module	12 Aug 2010 19:03:51 -0000	1.1.2.8
+++ designkit.module	28 Oct 2010 10:53:54 -0000
@@ -20,45 +20,114 @@ function designkit_theme() {
 }
 
 /**
- * Turn design choices into theme variables.
+ * Implementation of hook_theme_registry_alter().
+ *
+ * Add the stylesheet after all the theme ones but before $vars['styles'] has
+ * been populated.
  */
-function designkit_preprocess_page(&$vars) {
-  $info = designkit_get_info();
-  $color = !empty($info['designkit']['color']) ? variable_get('designkit_color', array()) : array();
-  $image = !empty($info['designkit']['image']) ? variable_get('designkit_image', array()) : array();
+function designkit_theme_registry_alter(&$theme_registry) {
+  array_unshift($theme_registry['page']['preprocess functions'], 'designkit_stylesheet_add');
+}
 
-  // Clear out stale values for image keys. This prevents themes from
-  // getting unexpected values if no images have been set.
-  if (!empty($info['designkit']['image'])) {
-    foreach (array_keys($info['designkit']['image']) as $name) {
-      if (isset($vars[$name])) {
-        unset($vars[$name]);
-      }
+/**
+ * Add the stylesheet for the current theme.
+ */
+function designkit_stylesheet_add() {
+  global $theme;
+  $stylesheet = variable_get('designkit_stylesheet', array());
+
+  if (isset($stylesheet[$theme])) {
+    $filepath = file_create_path(file_directory_path() ."/designkit/designkit-$theme.css");
+    drupal_add_css($filepath, 'theme');
+
+    // Rebuild the stylesheet if needed.
+    if (!empty($stylesheet[$theme])) {
+      list($color, $image) = designkit_processed_info();
+      designkit_stylesheet_save($color, $image);
+      // Mark the stylesheet as rebuilt.
+      $stylesheet[$theme] = FALSE;
+      variable_set('designkit_stylesheet', $stylesheet);
     }
   }
+}
 
-  // Process images array into an array of filepaths & add processed
-  // version to page template.
+/**
+ * Save the stylesheet for the given colors and images.
+ */
+function designkit_stylesheet_save($color, $image) {
+  global $theme;
+  $directory = file_create_path(file_directory_path()) .'/designkit';
+  file_check_directory($directory, FILE_CREATE_DIRECTORY);
+  $filename = "designkit-$theme.css";
+  $styles = strip_tags(theme('designkit', $color, $image));
+  file_put_contents("$directory/$filename", $styles);
+}
+
+/**
+ * Return the raw info, the processed info, and the themed images.
+ */
+function designkit_processed_info() {
+  static $imagefiles;
+
+  $info = designkit_get_info();
+  $color = !empty($info['designkit']['color']) ? variable_get('designkit_color', array()) : array();
+  $image = !empty($info['designkit']['image']) ? variable_get('designkit_image', array()) : array();
+  $output = array();
+
+  // Process images array into an array of filepaths and theme the processed
+  // version.
   foreach ($image as $name => $fid) {
-    $file = db_fetch_object(db_query('SELECT * FROM {files} f WHERE f.fid = %d', $fid));
-    if ($file && $file->filepath && file_exists($file->filepath)) {
+    if (!isset($imagefiles[$fid])) {
+      $file = db_fetch_object(db_query('SELECT * FROM {files} f WHERE f.fid = %d', $fid));
+      $imagefiles[$fid] = $file && $file->filepath && file_exists($file->filepath) ? $file : FALSE;
+    }
+    if ($file = $imagefiles[$fid]) {
       $image[$name] = $file->filepath;
-      $vars[$name] = theme('designkit_image', $name, $file->filepath);
+      $output[$name] = theme('designkit_image', $name, $file->filepath);
     }
     else {
       unset($image[$name]);
     }
   }
 
+  return array($color, $image, $info, $output);
+}
+
+/**
+ * Turn design choices into theme variables.
+ */
+function designkit_preprocess_page(&$vars) {
+  list($color, $image, $info, $output) = designkit_processed_info();
+
+  // Merge in the themed images.
+  $vars = array_merge($vars, $output);
+  
+  // Clear out stale values for image keys. This prevents themes from getting
+  // unexpected values if no images have been set.
+  if (!empty($info['designkit']['image'])) {
+    foreach (array_keys($info['designkit']['image']) as $name) {
+      if (isset($vars[$name])) {
+        unset($vars[$name]);
+      }
+    }
+  }
+
   // Generate CSS styles.
   if ($image || array_filter($color, 'designkit_valid_color')) {
+    global $theme;
+    $stylesheet = variable_get('designkit_stylesheet', array());
+
     // Add in designkit styles.
     $vars['body_classes'] .= " designkit";
-    // Add styles.
-    $styles = theme('designkit', $color, $image);
-    // Provide in separate variable for themes that reset or blow away styles.
-    $vars['styles'] .= $styles;
-    $vars['designkit'] = $styles;
+
+    // If we do not have a stylesheet add styles directly into the HTML head.
+    if (!isset($stylesheet[$theme])) {
+      // Add styles.
+      $styles = theme('designkit', $color, $image);
+      // Provide in separate variable for themes that reset or blow away styles.
+      $vars['styles'] .= $styles;
+      $vars['designkit'] = $styles;
+    }
   }
 }
 
