You are here

utility-color.inc in ImageCache Actions 6.2

Same filename and directory in other branches
  1. 8 utility-color.inc
  2. 7 utility-color.inc

Utility functions for color widgets

File

utility-color.inc
View source
<?php

/**
 * @file Utility functions for color widgets
 */

/**
 * Prepare a subform for displaying RGB fields
 *
 * Helper function to render a common element.
 *
 * Note that any module that re-uses this form also has to declare the theme
 * function in order to ensure it's in the registry.
 */
function imagecache_rgb_form($action) {
  if ($action['HEX'] && ($deduced = hex_to_rgb($action['HEX']))) {
    $action = array_merge($action, $deduced);
    $action['HEX'] = ltrim($action['HEX'], '#');

    // With or without # is valid, but trim for consistancy
  }
  $form = array(
    '#theme' => 'imagecacheactions_rgb_form',
  );
  $form['farb'] = array(
    '#weight' => -1,
  );

  // Placeholder to get its weight right
  $form['HEX'] = array(
    '#type' => 'textfield',
    '#title' => t('HEX'),
    '#default_value' => $action['HEX'],
    '#size' => 7,
  );
  return $form;
}

/**
 * Render the subform in a table
 */
function theme_imagecacheactions_rgb_form($form) {

  // Add a farb element
  drupal_add_css('misc/farbtastic/farbtastic.css', 'module', 'all', FALSE);
  drupal_add_js('misc/farbtastic/farbtastic.js');

  //  drupal_add_js(drupal_get_path('module', 'imagecache_coloractions') . '/color.js');
  $hex_id = $form['HEX']['#id'];
  $form['farb'] = array(
    '#value' => "<div id=\"{$hex_id}-farb\" style=\"float:right\"></div>",
    '#weight' => -1,
  );

  // Adds the JS that binds the textarea with the farb element
  $js = "\n  \$(document).ready(function() {\n    farbify(\$('#{$hex_id}'), '#{$hex_id}-farb');\n  });\n\n  function farbify(elt, wrapper) {\n    var farb = \$.farbtastic(wrapper);\n    farb.linkTo(function(color) {\n        elt\n          .css('background-color', color)\n          .css('color', this.hsl[2] > 0.5 ? '#000' : '#fff')\n          .val(color.substring(1));\n      });\n    farb.setColor('#' + elt.val());\n    elt.bind('keyup', function(){ updateColor(elt, farb); });\n  }\n  function updateColor(elt, farb) {\n    var text = elt.val();\n    if (text.length == 6)\n      farb.setColor('#' + text);\n  }\n\n  ";
  drupal_add_js($js, 'inline');
  $output = drupal_render($form);
  return $output;
}
function theme_imagecacheactions_rgb($rgb) {
  if ($rgb['HEX']) {
    return " <span style=\"width:2em; border:1px solid white; background-color:#{$rgb['HEX']}\" >&nbsp;#{$rgb['HEX']}&nbsp;</span>";
  }
  else {
    return ' ' . t('Transparent');
  }
}

/**
 * Decode an HTML hex-code into an array of R, G, and B values.
 * accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff
 */
function hex_to_rgb($hex) {
  $hex = trim($hex);

  // remove '#'
  if (substr($hex, 0, 1) == '#') {
    $hex = substr($hex, 1);
  }

  // expand short form ('fff') color
  if (strlen($hex) == 3) {
    $hex = substr($hex, 0, 1) . substr($hex, 0, 1) . substr($hex, 1, 1) . substr($hex, 1, 1) . substr($hex, 2, 1) . substr($hex, 2, 1);
  }
  if (strlen($hex) != 6) {
    trigger_error('Error: Invalid color "' . $hex . '"');
  }

  // convert
  $rgb['red'] = hexdec(substr($hex, 0, 2));
  $rgb['green'] = hexdec(substr($hex, 2, 2));
  $rgb['blue'] = hexdec(substr($hex, 4, 2));
  return $rgb;
}

Functions

Namesort descending Description
hex_to_rgb Decode an HTML hex-code into an array of R, G, and B values. accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff
imagecache_rgb_form Prepare a subform for displaying RGB fields
theme_imagecacheactions_rgb
theme_imagecacheactions_rgb_form Render the subform in a table