Color Helper (Random)

By Jeff Brown aka "jefkin"
This is by no means the answer to all your color woes, as it only has one function at this time. But the function can be handy.

Need a way to distinguish one chunk from another? How about easy random background colors that are all within a reasonable range of values that look good with dark text.
This simple Helper is extensible, and I plan to extend it the next time I get a good 'color-ish' type of thing that fits with it.

Now I needed this to set off hundreds of related boxes, from each other. So, that each would be distinct, and that we wouldn't produce any undreadable texts against these backgrounds.

I built the basis of this a long time ago, and use it here in cake (for the moment) to do debuging, but there could certainly be different uses as well, just think of highlighting different threads by random colors, or user posts by colors.

I know, sometimes it's tacky, but you don't have to use this everywhere.

First I created this file app/views/helpers/color.php:

Helper Class:

Download code <?php 

if (!defined('COLOR_HELPER_DEFINITION')){

  
define('COLOR_HELPER_DEFINITION'1);

  class 
ColorHelper extends Helper {

    
/*
    ** the last color rgb values -- intially set to these numbers;
    */
    
var $r 191;
    var 
$g 159;
    var 
$b 107;

    
/*
    ** the next color offset (movements -> offsets set once, and
    **                        cycled through legal values)
    */
    
var $_ri 0;
    var 
$_gi 0;
    var 
$_bi 0;

    
/*
    ** this collects the colors we've generated from random(), in 
    ** order to prevent simple collisions.
    */
    
var $used_colors = array('#ffffff');

    function 
ColorHelper() {

      if (!
$this->_ri) {

        
$this->ri rand(15,  52);
        
$this->gi rand(17,  63);
        
$this->bi rand(19,  74);
      }
      
$this->used_colors[0] = false;
    }

    
/*
    ** The while loop array_search test will always fails the initial
    ** test, as we will never store the value false in the
    ** $this->used_colors array.  Thus garaunteeing a new color the
    ** first time.
    **
    ** The colors are generated by adding a randomly determined amount
    ** to each of the initial r,g,b values.  The generated values are
    ** blocked from overflow, by subtracting fixed amounts once the
    ** values pass a key threshold, these manuvers keep the colors in 
    ** a range that seems quite palatable.  Dark or black text always
    ** shows up well against these colors.
    **
    ** The return value is a css compliant hex color string, ie. 
    ** "#AB93F2".
    */
    
function randomString() {

      
$color false;

      while (
false !== array_search($color$this->used_colors)) {

        
$this->+= $this->ri;
        
$this->+= $this->gi;
        
$this->+= $this->bi;

        if (
255 $this->r) {

          
$this->-= 90;
        }
        if (
223 $this->g) {

          
$this->-= 83;
        }
        if (
191 $this->b) {

          
$this->-= 76;
        }
        
$color sprintf("#%02x%02x%02x",
                         
$this->r$this->g$this->b);
      }
      return 
$color;
    }

    
/*
    ** a primary use, generate random non-repeated color on invocation
    */
    
function random() {

      return 
$this->output($this->randomString());
    }
  } 
// endof class ColorHelper

// endof defined check if for Color

?>


Now in my controllers I do:

Controller Class:

Download code <?php 

class MyController extends AppController {

   var 
$helpers = array('Color','Html', ...);
}

?>


And then when I need the colors set in my views I do this:

View Template:

Download code

<dl>
  <dt style="background:<?= $color->random() ?>;">Id</dt>
  ...
</dl>



Of course this can be applied to any html elements that accept styling. But I've found it works best with listing elements or with divs fieldsets, trs and tds.

But I'm sure you'll have more ideas than I for how to use this. :)

Also as this is my first article here, please let me know what i can do to improve it for you.

Login to Submit a Comment