Skip to content

Instantly share code, notes, and snippets.

@kyletaylored
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyletaylored/a6dfbc1b2325589f3df3 to your computer and use it in GitHub Desktop.
Save kyletaylored/a6dfbc1b2325589f3df3 to your computer and use it in GitHub Desktop.
Adobe Kuler & Drupal: Color theme integration
<?php
// Add this to the bottom of color.inc.
// The color fields will differ on themes, so you'll need to make sure
// to update the field names for the colors.
// ie, brandprimary, brandsecondary, etc.
// Add Kuler colors
$kuler_json = theme_get_setting('kuler_json', variable_get('theme_default'));
$kuler_check = theme_get_setting('kuler_check', variable_get('theme_default'));
if (!empty($kuler_json) && $kuler_check) {
$json = array_unique(get_object_vars(json_decode($kuler_json)));
$kuler_colors = array();
foreach ($json as $key => $value) {
if ($value != '[object Object]' && strlen($key) < 56) {
$colors = explode(',', $value);
$title = trim(str_replace('_', ' ', $key));
if (count($colors) == 5) {
$kuler_colors[$title] = array(
'title' => t(ucwords($key)),
'colors' => array(
'brandprimary' => $colors[0],
'brandsecondary' => $colors[1],
'brandaccent' => $colors[2],
'brandaccent2' => $colors[3],
'brandaccent3' => $colors[4],
),
);
}
}
}
reset($kuler_colors);
}
if (!empty($kuler_colors) && $kuler_check) {
// Add default value in or else it borks.
$kuler_colors = _color_clean_palette($kuler_colors);
$info['schemes'] = $kuler_colors;
}
/*
* Helper function to get the palettes ready for the Color module.
*/
function _color_clean_palette($palette) {
$first_key = key($palette);
$temp = $palette[$first_key];
$palette['default'] = $temp;
unset($palette[$first_key]);
return $palette;
}
jQuery(".collection-assets-item > .content").each(function(){
// set up asset object
var asset = {};
asset.colors = [];
// Get name of theme
asset.name = $(".assets-item-meta .name a",this).text();
// Grab all colors
var $colors = $(".frame > div",this);
// Extract only the hex values
$colors.each(function(){
var color = $(this).attr("style");
color = color.substring(12,19);
asset.colors.push(color);
});
// Add all color palettes to localStorage
localStorage.setItem(asset.name, asset.colors);
});
// Turn object into string that can then be extracted through copy/paste or whatever else.
var kuler_json = JSON.stringify(localStorage);
kuler_json;
<?php
function THEMENAME_form_system_theme_settings_alter(&$form, &$form_state) {
$form['kuler_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Adobe Kuler Settings'),
'#description' => t('Adding in text for JSON Kuler'),
'#weight' => -1,
);
$form['kuler_settings']['kuler_check'] = array(
'#type' => 'select',
'#title' => t('Enable Adobe Kuler'),
'#default_value' => theme_get_setting('kuler_check'),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#description' => t('Checking this will replace the current color schemes with those provided by the Kuler JSON.'),
);
$form['kuler_settings']['kuler_json'] = array(
'#type' => 'textarea',
'#title' => t('Kuler JSON'),
'#default_value' => theme_get_setting('kuler_json'),
'#description' => t('If you have Kuler JSON, you can enter it here to provide theme color options. You can get Kuler JSON by using this gist: !gist', array('!gist' => l('Kuler JSON Gist', 'https://gist.github.com/kyletaylored/1a2edba96509277cfcca', array('attributes' => array('target' => '_blank',))))),
'#states' => array(
'visible' => array(
':input[name="kuler_check"]' => array('value' => 1),
),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment