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->r += $this->ri; $this->g += $this->gi; $this->b += $this->bi; if (255 < $this->r) { $this->r -= 90; } if (223 < $this->g) { $this->g -= 83; } if (191 < $this->b) { $this->b -= 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 ?>