utility-color.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file Utility functions for color widgets.
  4. */
  5. /**
  6. * Prepares 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. * @param array $action
  14. *
  15. * @return array
  16. */
  17. function imagecache_rgb_form($action) {
  18. if ($action['HEX'] && $deduced = imagecache_actions_hex2rgba($action['HEX'])) {
  19. $action = array_merge($action, $deduced);
  20. // With or without # is valid, but the colorpicker expects it always.
  21. $action['HEX'] = '#' . ltrim($action['HEX'], '#');
  22. }
  23. $form = array(
  24. '#prefix' => '<div class="colorform" >',
  25. '#suffix' => '</div>',
  26. );
  27. $form['colorpicker'] = array(
  28. '#weight' => -1,
  29. '#markup' => '<div class="colorpicker" style="float:right;"></div>',
  30. );
  31. $form['HEX'] = array(
  32. '#type' => 'textfield',
  33. '#title' => t('HEX'),
  34. '#default_value' => $action['HEX'],
  35. '#size' => 7,
  36. '#element_validate' => array('imagecache_rgb_validate'),
  37. '#attributes' => array('class' => array('colorentry')),
  38. );
  39. // Adds the JS that binds the textarea with the farbtastic element.
  40. // Find each colorpicker placeholder:
  41. // initialize it,
  42. // then find the nearby textfield that is of type colorentry
  43. // and attach the colorpicker behavior to it.
  44. // This is so we can support more that one per page if neccessary.
  45. $init_colorpicker_script = "
  46. (function ($) {
  47. Drupal.behaviors.attachcolorpicker = {
  48. attach: function(context) {
  49. $('.colorpicker').each(function () {
  50. // Configure picker to be attached to the nearest colorentry field.
  51. linked_target = $('.colorentry', $(this).closest('.colorform'))
  52. farb = $.farbtastic($(this), linked_target);
  53. });
  54. }
  55. }
  56. })(jQuery);
  57. ";
  58. // Add Farbtastic color picker.
  59. $form['HEX']['#attached'] = array(
  60. 'library' => array(
  61. array('system', 'farbtastic'),
  62. ),
  63. 'js' => array(
  64. array(
  65. 'type' => 'inline',
  66. 'data' => $init_colorpicker_script,
  67. ),
  68. ),
  69. );
  70. return $form;
  71. }
  72. /**
  73. * Element validate handler to ensure a hexadecimal color value.
  74. *
  75. * The core image_effect_color_validate() was not intelligent enough.
  76. * Actually parse the value in order to see if it is parsable.
  77. *
  78. * @param array $element
  79. * param array $form_state
  80. * Not used
  81. */
  82. function imagecache_rgb_validate($element/*, &$form_state*/) {
  83. if ($element['#value'] != '') {
  84. $rgba_value = imagecache_actions_hex2rgba($element['#value']);
  85. if (!$rgba_value) {
  86. // Returning NULL means a parse error.
  87. form_error($element, t('!name must be a hexadecimal color value.', array('!name' => $element['#title'])));
  88. }
  89. }
  90. }
  91. /**
  92. * Displays a chosen color for use in the style summary snippets,
  93. * by actually rendering the color.
  94. *
  95. * @param array $variables
  96. *
  97. * @return string
  98. */
  99. function theme_imagecacheactions_rgb($variables) {
  100. if (!empty($variables['RGB']['HEX'])) {
  101. $hex = ltrim($variables['RGB']['HEX'], '#');
  102. return " <span style=\"width:2em; border:1px solid white; background-color:#{$hex}\" >&nbsp;#{$hex}&nbsp;</span>";
  103. }
  104. return ' ' . t('Transparent');
  105. }