Drupal - replace colors in buttons with color.module

Drupal Color.module allows the admin to change the color scheme of a theme completely. Color module basically fill viewport with 5 choosen colors, layout transparent PNG on it and then makes image slices and update CSS with this colors.
I would like to share some code that customize color.module further. After color scheme is choosen specified images would be copied and colors updated to reflect current color scheme. This is especially useful for creating buttons that should be color aware.
/**
* Form alter hook
*/
function mycolormodule_form_alter($form_id, &$form) {
if ($form_id == 'system_theme_settings') {
$form['#submit']['mycolormodule_system_theme_settings'] = array();
}
}
/**
* Copy PNG images and change color in images with color from palette.
*
* Settings in color.inc $info array:
*
* 'change_color' => array(
* 'images/form_buttons.png' => array("#3CC6F5" => "bottom"), // Color #3CC6F5 will be updated with color of "header bottom"
* ),
*/
function mycolormodule_system_theme_settings($form_id, $values) {
$theme = $values['theme'];
$target = variable_get('color_'. $theme .'_stylesheet', NULL);
if (!$target) return;
$files = variable_get('color_'. $theme .'_files', array());
$target = dirname($target) . "/";
$info = $values['info'];
$source = drupal_get_path('theme', $theme) .'/';
foreach ($info['change_color'] as $file => $replace_array) {
$base = basename($file);
$source = $source . $file;
// NOTE: change color
$im = imagecreatefrompng($source);
foreach ($replace_array as $old => $new) {
$old_arr = _color_unpack($old);
$index = imagecolorexact($im, $old_arr[0], $old_arr[1], $old_arr[2]);
$new_arr = _color_unpack($values['palette'][$new]);
imagecolorset($im, $index ,$new_arr[0], $new_arr[1], $new_arr[2]);
$files[] = $target . $base;
}
imagepng($im, $target . $base);
}
variable_set('color_'. $theme .'_files', $files);
}
Buttons from above image are based on Wii buttons demo. Using color.module is described in details here.
I am Bojan Mihelac and this blog is dedicated to share code, thoughts, tools and advices I came up with while working in