# Color.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2004-2007 by Dmitry E. Demidov                          *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00015     final class Color implements Stringable
    00016     {
    00017         private $red    = 0;
    00018         private $green  = 0;
    00019         private $blue   = 0;
    00020
    00024         public static function create($rgb)
    00025         {
    00026             static $flyweightColors = array();
    00027
    00028             if (isset($flyweightColors[$rgb]))
    00029                 return $flyweightColors[$rgb];
    00030
    00031             $result = new self($rgb);
    00032
    00033             $flyweightColors[$rgb] = $result;
    00034
    00035             return $result;
    00036         }
    00037
    00038         // valid values: #AABBCC, DDEEFF, A15B, etc.
    00039         public function __construct($rgb)
    00040         {
    00041             $length = strlen($rgb);
    00042
    00043             Assert::isTrue($length <= 7, 'color must be #XXXXXX');
    00044
    00045             if ($rgb[0] == '#')
    00046                 $rgb = substr($rgb, 1);
    00047
    00048             if ($length < 6)
    00049                 $rgb = str_pad($rgb, 6, '0', STR_PAD_LEFT);
    00050
    00051             $this->red      = hexdec($rgb[0] . $rgb[1]);
    00052             $this->green    = hexdec($rgb[2] . $rgb[3]);
    00053             $this->blue     = hexdec($rgb[4] . $rgb[5]);
    00054         }
    00055
    00059         public function setRed($red)
    00060         {
    00061             $this->red = $red;
    00062
    00063             return $this;
    00064         }
    00065
    00066         public function getRed()
    00067         {
    00068             return $this->red;
    00069         }
    00070
    00074         public function setGreen($green)
    00075         {
    00076             $this->green = $green;
    00077
    00078             return $this;
    00079         }
    00080
    00081         public function getGreen()
    00082         {
    00083             return $this->green;
    00084         }
    00085
    00089         public function setBlue($blue)
    00090         {
    00091             $this->blue = $blue;
    00092
    00093             return $this;
    00094         }
    00095
    00096         public function getBlue()
    00097         {
    00098             return $this->blue;
    00099         }
    00100
    00104         public function invertColor()
    00105         {
    00106             $this->setRed(255 - $this->getRed());
    00107             $this->setBlue(255 - $this->getBlue());
    00108             $this->setGreen(255 - $this->getGreen());
    00109
    00110             return $this;
    00111         }
    00112
    00113         public function toString()
    00114         {
    00115             return sprintf('%02X%02X%02X', $this->red, $this->green, $this->blue);
    00116         }
    00117     }
    00118 ?>