Revision [12050]

This is an old revision of ColorAction made by NilsLindenberg on 2005-11-29 13:56:24.

 

Development page for the Color / Colour action




The color action, as of version 1.1.6.0, has some disadvantages:

  1. it does no verification if a color is actually given or if it is valid, therefore produces invalid html
  1. it causes two php-notices
  1. it has problems with some kinds of text
  1. it does not use the standard for action-params

the code below solves this issues and adds support for a background-color, too:

  1. <?php
  2. /**
  3.  * Colors a given text.
  4.  *
  5.  * @package        Actions
  6.  * @name        Color
  7.  *
  8.  * @author      ?, probably Hendrik Mans
  9.  * @author      {@link http://wikka.jsnx.com/NilsLindenberg NilsLindenberg} (modifications)
  10.  *
  11.  * @input            string $c/$color/$colour (mandatory): (html)name of the color for the text;
  12.  * @input                string $h/$hex (mandatory): color for the text as a hex-value;
  13.  * @input                string $b/$bg (mandatory): (html)name or hex-value for the backgroundcolor;
  14.  * @output      colored text
  15.  *
  16.  * @todo            integrate it into the formatter
  17.  */
  18.  
  19. $html_color_names = array ('aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime', 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal', 'yellow', 'white');
  20. $hex_color_regexp = '/^\#[0-9A-F]{6}$/';
  21.  
  22. if (is_array($vars)) {
  23.     foreach ($vars as $param => $value) {
  24.         switch ($param)
  25.             {
  26.                      case 'text':
  27.                     $mytext = $this->htmlspecialchars_ent($value);
  28.                     break;
  29.                  case 'c':
  30.                  case 'color':
  31.                  case 'colour':
  32.                  case 'h':
  33.                  case 'hex':
  34.                     if (in_array(strtolower($value), $html_color_names)) $colorcode = strtolower($value);
  35.                     else if (preg_match($hex_color_regexp, $value)) $colorcode = $value;
  36.                     break;
  37.                  case 'b':
  38.                  case 'bg':
  39.                     if (in_array(strtolower($value), $html_color_names)) $backgroundcolor = strtolower($value);
  40.                     else if (preg_match($hex_color_regexp, $value)) $backgroundcolor = $value;
  41.                 }
  42.     }
  43.     $style = '';
  44.     if (isset($colorcode)) $style .= 'color:'.$colorcode.';';
  45.     if (isset($backgroundcolor)) $style .= 'background-color:'.$backgroundcolor.';';
  46.     if (isset($mytext) && ($style !== '')) echo '<span style="'.$style.'">'.$mytext.'</span>';
  47. }
  48. ?>
There are 3 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki