diff --git field_example/field_example.module field_example/field_example.module
index dd9ea68..e26e277 100644
--- field_example/field_example.module
+++ field_example/field_example.module
@@ -103,11 +103,8 @@ function field_example_field_formatter_info() {
 /**
  * Implements hook_field_formatter_view().
  *
- * Two formatters are implemented.
- * - field_example_simple_text just outputs markup indicating the color that
- *   was entered and uses an inline style to set the text color to that value.
- * - field_example_color_background does the same but also changes the
- *   background color of div.region-content.
+ * Two formatters are implemented. Each formatter uses its own theme function
+ * to generate #markup. These theme functions are registered using hook_theme.
  */
 function field_example_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
   $element = array();
@@ -116,7 +113,7 @@ function field_example_field_formatter_view($entity_type, $entity, $field, $inst
     // This formatter simply outputs the field as text and with a color.
     case 'field_example_simple_text':
       foreach ($items as $delta => $item) {
-        $element[$delta]['#markup'] = '<p style="color: ' . $item['rgb'] . '">' . t('The color code in this field is @code', array('@code' => $item['rgb'])) . '</p>';
+        $element[$delta]['#markup'] = theme('field_example_simple_text', array('rgb' => $item['rgb']));
       }
       break;
 
@@ -124,8 +121,7 @@ function field_example_field_formatter_view($entity_type, $entity, $field, $inst
     // background color. If there are many fields, the last one will win.
     case 'field_example_color_background':
       foreach ($items as $delta => $item) {
-        drupal_add_css('div.region-content { background-color:' . $item['rgb'] . ';}', array('type' => 'inline') );
-        $element[$delta]['#markup'] = '<p>' . t('The content area color has been changed to @code', array('@code' => $item['rgb'])) . '</p>';
+        $element[$delta]['#markup'] = theme('field_example_color_background', array('rgb' => $item['rgb']));
       }
       break;
   }
@@ -319,3 +315,40 @@ function field_example_menu() {
 function _field_example_page() {
   return t("The Field Example provides a field composed of an HTML RGB value, like #ff00ff. To use it, add the field to a content type.");
 }
+
+/**
+ * Implements hook_theme().
+ */
+function field_example_theme() {
+  return array(
+    'field_example_simple_text' => array(
+      'arguments' => array('rgb' => NULL),
+    ),
+    'field_example_color_background' => array(
+      'arguments' => array('rgb' => NULL),
+    ),
+  );
+}
+
+/*
+ * Theme function used by field_example_simple_text formatter to generate
+ * #markup.
+ *
+ * Outputs markup indicating the color that was entered and uses an inline
+ * style to set the text color to that value.
+ */
+function theme_field_example_simple_text($variables) {
+  return '<p style="color: ' . $variables['rgb'] . '">' . t('The color code in this field is @code', array('@code' => $variables['rgb'])) . '</p>';
+}
+
+/*
+ * Theme function used by field_example_color_background formatter to generate
+ * #markup.
+ *
+ * Does the same as above, except changes the background color of
+ * div.region-content instead of the text color.
+ */
+function theme_field_example_color_background($variables) {
+  drupal_add_css('div.region-content { background-color:' . $variables['rgb'] . ';}', array('type' => 'inline') );
+  return '<p>' . t('The content area color has been changed to @code', array('@code' => $variables['rgb'])) . '</p>';
+}
\ No newline at end of file
