utility-color.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @file Utility functions for color widgets
  4. */
  5. /**
  6. * Prepare a subform for displaying RGB fields
  7. *
  8. * Helper function to render a common element.
  9. *
  10. * Note that any module that re-uses this form also has to declare the theme
  11. * function in order to ensure it's in the registry.
  12. */
  13. function imagecache_rgb_form($action) {
  14. if ($action['HEX'] && $deduced = imagecache_actions_hex2rgba($action['HEX'])) {
  15. $action = array_merge($action, $deduced);
  16. $action['HEX'] = ltrim($action['HEX'], '#');
  17. // With or without # is valid, but trim for consistancy
  18. }
  19. $form = array('#theme' => 'imagecacheactions_rgb_form');
  20. $form['farb'] = array('#weight' => -1); // Placeholder to get its weight right
  21. $form['HEX'] = array(
  22. '#type' => 'textfield',
  23. '#title' => t('HEX'),
  24. '#default_value' => $action['HEX'],
  25. '#size' => 7,
  26. '#element_validate' => array('imagecache_rgb_validate'),
  27. );
  28. return $form;
  29. }
  30. /**
  31. * Element validate handler to ensure a hexadecimal color value.
  32. *
  33. * The core image_effect_color_validate() was not intelligent enough.
  34. * Actually parse the value in order to see if it is parsable.
  35. */
  36. function imagecache_rgb_validate($element, &$form_state) {
  37. if ($element['#value'] != '') {
  38. $rgba_value = imagecache_actions_hex2rgba($element['#value']);
  39. if (!$rgba_value) {
  40. // Returning NULL means a parse error
  41. form_error($element, t('!name must be a hexadecimal color value.', array('!name' => $element['#title'])));
  42. }
  43. }
  44. }
  45. /**
  46. * Render the subform in a table
  47. */
  48. function theme_imagecacheactions_rgb_form($variables) {
  49. $form = $variables['form'];
  50. // Add a farb element
  51. drupal_add_css('misc/farbtastic/farbtastic.css', array('preprocess' => FALSE));
  52. drupal_add_js('misc/farbtastic/farbtastic.js');
  53. // drupal_add_js(drupal_get_path('module', 'imagecache_coloractions') . '/color.js');
  54. $hex_id = $form['HEX']['#id'];
  55. $form['farb'] = array(
  56. '#value' => "<div id=\"$hex_id-farb\" style=\"float:right\"></div>",
  57. '#weight' => -1,
  58. );
  59. // Adds the JS that binds the textarea with the farb element
  60. $js = "
  61. $(document).ready(function() {
  62. farbify($('#$hex_id'), '#$hex_id-farb');
  63. });
  64. function farbify(elt, wrapper) {
  65. var farb = $.farbtastic(wrapper);
  66. farb.linkTo(function(color) {
  67. elt
  68. .css('background-color', color)
  69. .css('color', this.hsl[2] > 0.5 ? '#000' : '#fff')
  70. .val(color.substring(1));
  71. });
  72. farb.setColor('#' + elt.val());
  73. elt.bind('keyup', function(){ updateColor(elt, farb); });
  74. }
  75. function updateColor(elt, farb) {
  76. var text = elt.val();
  77. if (text.length == 6)
  78. farb.setColor('#' + text);
  79. }
  80. ";
  81. drupal_add_js($js, array('type' => 'inline', 'scope' => JS_DEFAULT));
  82. $output = drupal_render_children($form);
  83. return $output;
  84. }
  85. /**
  86. * @todo Please document this function.
  87. * @see http://drupal.org/node/1354
  88. */
  89. function theme_imagecacheactions_rgb($variables) {
  90. $rgb = $variables['RGB'];
  91. if ($rgb['HEX']) {
  92. return " <span style=\"width:2em; border:1px solid white; background-color:#{$rgb['HEX']}\" >&nbsp;#{$rgb['HEX']}&nbsp;</span>";
  93. }
  94. else {
  95. return ' ' . t('Transparent');
  96. }
  97. }