/[drupal]/contributions/modules/imagecache_actions/utility.inc
ViewVC logotype

Contents of /contributions/modules/imagecache_actions/utility.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Tue Aug 19 14:33:56 2008 UTC (15 months ago) by dman
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.3: +111 -111 lines
File MIME type: text/x-php
synched up new tag branch
1 <?php
2 /**
3 * Utility form, conversion and rendering functions for image processes
4 */
5
6
7
8 /**
9 * Prepare a subform for displaying RGB fields
10 *
11 * Helper function to render a common element.
12 */
13 function imagecache_rgb_form($action) {
14 if($action['HEX'] && $deduced = hex_to_rgb($action['HEX'])) {
15 $action = array_merge($action, $deduced);
16 }
17 $form = array('#theme' => 'canvasactions_rgb_form');
18 $form['red'] = array( '#type' => 'textfield', '#title' => t('Red'), '#default_value' => $action['red'], '#size' => 3);
19 $form['green'] = array( '#type' => 'textfield', '#title' => t('Green'), '#default_value' => $action['green'], '#size' => 3);
20 $form['blue'] = array( '#type' => 'textfield', '#title' => t('Blue'), '#default_value' => $action['blue'], '#size' => 3);
21 $form['HEX'] = array( '#type' => 'textfield', '#title' => t('HEX'), '#default_value' => $action['HEX'], '#size' => 7);
22
23 return $form;
24 }
25
26 /**
27 * Prepare a subform for displaying positioning fields
28 *
29 * Helper function to render a common element.
30 */
31 function canvasactions_pos_form($action) {
32 $form = array(
33 #'#theme' => 'canvasactions_pos_form',
34 'xpos' => array(
35 '#type' => 'textfield',
36 '#title' => t('X offset'),
37 '#default_value' => isset($action['xpos']) ? $action['xpos'] : 'center',
38 '#size' => 6,
39 '#description' => t('Enter an offset in pixels or use a keyword: <em>left</em>, <em>center</em>, or <em>right</em>.'),
40 ),
41 'ypos' => array(
42 '#type' => 'textfield',
43 '#title' => t('Y offset'),
44 '#default_value' => isset($action['ypos']) ? $action['ypos'] : 'center',
45 '#size' => 6,
46 '#description' => t('Enter an offset in pixels or use a keyword: <em>top</em>, <em>center</em>, or <em>bottom</em>.'),
47 ),
48 );
49 return $form;
50 }
51
52 /**
53 * Render the subform in a table
54 */
55 function theme_canvasactions_rgb_form(&$form) {
56 $header = array();
57 $table = array();
58 foreach (element_children($form) as $key) {
59 $header[$key] = $form[$key]['#title'];
60 unset($form[$key]['#title']);
61 $table['field'][$key] = drupal_render($form[$key]);
62 }
63 $output .= theme('table', $header, $table);
64 $output .= t('Enter colors in decimal, 0-255, or in HEX. If HEX is set, it will take priority.');
65 $output .= drupal_render($form);
66 return $output;
67 }
68
69 function theme_canvasactions_rgb($rgb) {
70 if($rgb['HEX'] && $deduced = hex_to_rgb($rgb['HEX'])) {
71 $rgb = array_merge($rgb, $deduced);
72 }
73 if ($rgb['red'] || $rgb['green'] || $rgb['blue']) {
74 $output .= ' RGB:['. $rgb['red'] .', '. $rgb['green'] .', '. $rgb['blue'] .'] #'. $rgb['HEX'];
75 }
76 else {
77 $output .= ' ' .t('Transparent');
78 }
79 return $output;
80 }
81
82
83
84 /**
85 * Decode an HTML hex-code into an array of R, G, and B values.
86 * accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff
87 */
88 function hex_to_rgb($hex) {
89 $hex = trim($hex);
90 // remove '#'
91 if (substr($hex, 0, 1) == '#')
92 $hex = substr($hex, 1) ;
93
94 // expand short form ('fff') color
95 if (strlen($hex) == 3) {
96 $hex = substr($hex, 0, 1) . substr($hex, 0, 1) .
97 substr($hex, 1, 1) . substr($hex, 1, 1) .
98 substr($hex, 2, 1) . substr($hex, 2, 1) ;
99 }
100
101 if (strlen($hex) != 6)
102 trigger_error('Error: Invalid color "'. $hex .'"') ;
103
104 // convert
105 $rgb['red'] = hexdec(substr($hex, 0, 2)) ;
106 $rgb['green'] = hexdec(substr($hex, 2, 2)) ;
107 $rgb['blue'] = hexdec(substr($hex, 4, 2)) ;
108
109 return $rgb ;
110 }
111

  ViewVC Help
Powered by ViewVC 1.1.2