diff --git a/core/modules/color/color.module b/core/modules/color/color.module
index 98678f7..56b79b8 100644
--- a/core/modules/color/color.module
+++ b/core/modules/color/color.module
@@ -510,44 +510,50 @@ function _color_rewrite_stylesheet($theme, &$info, &$paths, $palette, $style) {
     list($style, $fixed) = explode($split, $style);
   }
 
-  // Find all colors in the stylesheet and the chunks in between.
-  $style = preg_split('/(#[0-9a-f]{6}|#[0-9a-f]{3})/i', $style, -1, PREG_SPLIT_DELIM_CAPTURE);
-  $is_color = FALSE;
-  $output = '';
-  $base = 'base';
-
-  // Iterate over all the parts.
-  foreach ($style as $chunk) {
-    if ($is_color) {
-      $chunk = Unicode::strtolower($chunk);
-      // Check if this is one of the colors in the default palette.
-      if ($key = array_search($chunk, $default)) {
-        $chunk = $conversion[$key];
-      }
-      // Not a pre-set color. Extrapolate from the base.
-      else {
-        $chunk = _color_shift($palette[$base], $default[$base], $chunk, $info['blend_target']);
-      }
-    }
-    else {
-      // Determine the most suitable base color for the next color.
-
-      // 'a' declarations. Use link.
-      if (preg_match('@[^a-z0-9_-](a)[^a-z0-9_-][^/{]*{[^{]+$@i', $chunk)) {
+  // Iterate over every selector chunk and replace/shift the colors for styles.
+  $output = preg_replace_callback(
+    '/([^\\*\/{]*){([^}]*)}/ims',
+    function ($chunk) use ($conversion, $default, $info, $palette) {
+      $base = 'base';
+      // Use "link" as the base for color shifting on chunks that have the "a"
+      // tag in the selector.
+      if (preg_match('/[^a-z0-9_-](a)[:.,\s\w]+$/i', $chunk[1])) {
         $base = 'link';
       }
-      // 'color:' styles. Use text.
-      elseif (preg_match('/(?<!-)color[^{:]*:[^{#]*$/i', $chunk)) {
-        $base = 'text';
-      }
-      // Reset back to base.
-      else {
-        $base = 'base';
-      }
-    }
-    $output .= $chunk;
-    $is_color = !$is_color;
-  }
+      // Iterate over every style in this chunk.
+      $chunk[2] = preg_replace_callback(
+        '/[^;]*/ims',
+        function ($style) use ($base, $conversion, $default, $info, $palette) {
+          // Use "text" as the base for color shifting on "color:" styles.
+          if ($base !== 'link' && preg_match('/(?<!-)color[^:]*:/i', $style[0])) {
+            $base = 'text';
+          }
+          // Replace all colors found in this style.
+          $style[0] = preg_replace_callback(
+            '/(#[0-9a-f]{6}|#[0-9a-f]{3})/i',
+            function ($colors) use ($base, $conversion, $default, $info, $palette) {
+              $color = Unicode::strtolower($colors[1]);
+              // Determine if this color exists in the default palette.
+              if ($key = array_search($color, $default)) {
+                $color = $conversion[$key];
+              }
+              // If not a default palette color, extrapolate from the base.
+              else {
+                $color = _color_shift($palette[$base], $default[$base], $color, $info['blend_target']);
+              }
+              return $color;
+            },
+            $style[0]
+          );
+          return $style[0];
+        },
+        $chunk[2]
+      );
+      return $chunk[1] . '{' . $chunk[2] . '}';
+    },
+    $style
+  );
+
   // Append fixed colors segment.
   if (isset($fixed)) {
     $output .= $fixed;
