I've encountered the same "Cannot read property 'farbtastic' of undefined" error and the root cause is that the color picker is not still ready/loaded when the farbtastic's markups need to be injected to that element. My solution is to enclose the farbtastic code to ajaxComplete() event:
your_module.module
$form['marker_color'] = array('#type' => 'textfield',
'#title' => t('Marker Color'),
'#default_value' => '#18d',
'#size' => 7,
'#attached' => array(
'library' => array(
array('system', 'farbtastic'),
),
'js' => array(drupal_get_path('module', 'your_module') . '/js/custom.js'),
),
'#attributes' => array('id' => 'color-picker-target-textfield'),
'#suffix' => '<div id="color-picker-container"></div>',
);
js/custom.js
(function ($) {
Drupal.behaviors.custom_farbtastic = {
attach : function(context, settings) {
// Wait the HEX input field to be rendered completely to prevent
// referencing errors. Pass the context information to speed up
// DOM traversal/searching.
$('#color-picker-target-textfield', context).ajaxComplete(function() {
// Prevent multiple processing.
$('#color-picker-target-textfield', context).once('color-picker-target-processed', function() {
// Create a farbtastic colorpicker in the given DIV container.
var colorPicker = $.farbtastic('color-picker-container');
// Retrieve the target text field that contains the HEX color strings.
var colorPickerTarget = $('#color-picker-target-textfield');
// Make the color picker auto-update the contents of the HEX text field.
colorPicker.linkTo(colorPickerTarget);
});
});
},
}
};
})(jQuery);