TeeChartPHP
[ class tree: TeeChartPHP ] [ index: TeeChartPHP ] [ all elements ]

Source for file Color.php

Documentation is available at Color.php

  1. <?php
  2.  
  3. /**
  4.  * Color class
  5.  *
  6.  * Description: Color characteristics
  7.  *
  8.  * @author
  9.  * @copyright (c) 1995-2008 by Steema Software SL. All Rights Reserved. <info@steema.com>
  10.  * @version 1.0
  11.  * @package TeeChartPHP
  12.  * @subpackage drawing
  13.  * @link http://www.steema.com
  14.  */
  15.  
  16.  class Color
  17.  {
  18.  
  19.     private $_empty=true;
  20.     private $none=false;
  21.     private $red;
  22.     private $green;
  23.     private $blue;
  24.     private $alpha;
  25.     private $gdColor;
  26.  
  27.     // Interceptors
  28.     function __get$property {
  29.       $method ="get{$property}";
  30.       if method_exists$this$method ) ) {
  31.         return $this->$method();
  32.       }
  33.     }
  34.  
  35.     function __set $property,$value {
  36.       $method ="set{$property}";
  37.       if method_exists$this$method ) ) {
  38.         return $this->$method($value);
  39.       }
  40.     }
  41.  
  42.     /**
  43.     * Creates a new color
  44.     *
  45.     * @access       public
  46.     * @param        integer         red [0,255]
  47.     * @param        integer         green [0,255]
  48.     * @param        integer         blue [0,255]
  49.     * @param        integer         alpha [0,255]
  50.     */
  51.  
  52.     function Color($red=0$green=0$blue=0$alpha 0$_empty=false$none=false)
  53.     {
  54.         // Some Colors
  55.         $this->red = (int)$red;
  56.         $this->green = (int)$green;
  57.         $this->blue = (int)$blue;
  58.  
  59.         $this->alpha = (int)$alpha;
  60. //        $this->alpha = (int)round($alpha * 127.0 / 255);
  61.  
  62.         $this->gdColor null;
  63.         $this->_empty $_empty;
  64.         $this->none $none;
  65.     }
  66.  
  67.     /**
  68.     * Get GD color
  69.     *
  70.     * @access       public
  71.     * @param        $img            GD image resource
  72.     */
  73.  
  74.     function getColor($img)
  75.     {
  76.         // Checks if color has already been allocated
  77.         if(!$this->gdColor)  {
  78.             if($this->alpha == || !function_exists('imagecolorallocatealpha'))
  79.                 $this->gdColor imagecolorallocate($img$this->red$this->green$this->blue);
  80.             else
  81.                 $this->gdColor imagecolorallocatealpha($img$this->red$this->green$this->blue$this->alpha);
  82.         }
  83.  
  84.         // Returns GD color
  85.         return $this->gdColor;
  86.     }
  87.  
  88.     public function isEmpty({
  89.         return $this->_empty;
  90.     }
  91.  
  92.     /**
  93.      * Returns true when the Color equals to EMPTY color. <br>
  94.      * EMPTY Color means objects will not fill drawing elements.
  95.      *
  96.      * @return Color 
  97.      */
  98.     public function getEmpty({
  99.         return $this->_empty;
  100.     }
  101.  
  102.     public function setEmpty($value{
  103.         $this->_empty $value;
  104.     }
  105.  
  106.     public function isNull({
  107.       return $this->none;
  108.     }
  109.  
  110.     public function getNone({
  111.         return $this->none;
  112.     }
  113.  
  114.     public function setNone($value{
  115.         $this->none $value;
  116.     }
  117.  
  118.     static public function fromArgb($a$r$g$b{
  119.         return new Color($r$g$b$a);
  120.     }
  121.  
  122.     static public function EMPTYCOLOR(){
  123.        return new Color(0,0,0,0,true,false);
  124.     }
  125.  
  126.     static public function TRANSPARENT({
  127.         return new Color (0,0,0,127,false,true)// Transparent
  128.     }
  129. /*
  130.     static public Color fromArgb(int value) {
  131.         return new Color(value);
  132.     }
  133.  
  134.     static public Color fromArgb(int value, Color c) {
  135.         return new Color(c.getRed(), c.getGreen(), c.getBlue(), value);
  136.     }
  137.  
  138.     static public fromCode($nm) {
  139.         return new Color(Color.decode(nm).getRed(),
  140.                          Color.decode(nm).getGreen(),Color.decode(nm).getBlue(),Color.decode(nm).getAlpha());
  141.     }
  142. */
  143.     static public function fromRgb($r$g$b{
  144.         return new Color($r$g$b);
  145.     }
  146.  
  147.     /**
  148.      * Creates a new Color instance with random RED, GREEN and BLUE color
  149.      * components.
  150.      *
  151.      *
  152.      *
  153.      *
  154.      * @return Color 
  155.      */
  156.     static public function random({
  157.         srand(time());
  158.  
  159.         return new Color((int) (rand()%255)(int) (rand()%255),
  160.                          (int) (rand()%255));
  161.     }
  162.  
  163.     /**
  164.      * The color black.  In the default sRGB space.
  165.      */
  166.     static public final function getBlack({
  167.       return new Color(0,0,0);
  168.     }
  169.  
  170.     /**
  171.      * The color black.  In the default sRGB space.
  172.      */
  173.     static public final function BLACK({
  174.       return self::getBlack();
  175.     }
  176.  
  177.     /**
  178.      * The color dark gray.  In the default sRGB space.
  179.      */
  180.     static public final function getDarkGray({
  181.       return new Color(169169169);
  182.     }
  183.  
  184.     /**
  185.      * The color dark gray.  In the default sRGB space.
  186.      */
  187.     static public final function DARK_GRAY({
  188.       return self::getDarkGray();
  189.     }
  190.  
  191.     /**
  192.      * The color yellow.  In the default sRGB space.
  193.      */
  194.     static public final function getYellow({
  195.       return new Color(255,255,0);
  196.     }
  197.  
  198.     /**
  199.      * The color yellow.  In the default sRGB space.
  200.      */
  201.     static public final function YELLOW({
  202.       return self::getYellow();
  203.     }
  204.  
  205.     /**
  206.      * The color red.  In the default sRGB space.
  207.      */
  208.     public function getRed({
  209.       return $this->red;
  210.     }
  211.  
  212.     /**
  213.      * The color red.  In the default sRGB space.
  214.      */
  215.     static public final function RED({
  216.       return new Color(255,0,0);
  217.     }
  218.  
  219.     /**
  220.      * The color green.  In the default sRGB space.
  221.      */
  222.     public function getGreen({
  223.       return $this->green;
  224.     }
  225.  
  226.     /**
  227.      * The color green.  In the default sRGB space.
  228.      */
  229.     static public final function GREEN({
  230.       return new Color(0,128,0);
  231.     }
  232.  
  233.     /**
  234.      * The color blue.  In the default sRGB space.
  235.      */
  236.     public function getBlue({
  237.       return $this->blue;
  238.     }
  239.  
  240.     /**
  241.      * The color blue.  In the default sRGB space.
  242.      */
  243.     static public final function BLUE({
  244.       return new Color(0,0,255);
  245.     }
  246.  
  247.     /**
  248.      * The color silver.  In the default sRGB space.
  249.      */
  250.     static public final function getSilver({
  251.       return new Color(192192192);
  252.     }
  253.  
  254.     /**
  255.      * The color silver.  In the default sRGB space.
  256.      */
  257.     static public final function SILVER({
  258.       return self::getSilver();
  259.     }
  260.  
  261.     /**
  262.      * The color gray.  In the default sRGB space.
  263.      */
  264.     static public final function  getGray({
  265.       return new Color(128,128,128);
  266.     }
  267.  
  268.     /**
  269.      * The color gray.  In the default sRGB space.
  270.      */
  271.     static public final function GRAY({
  272.       return self::getGray();
  273.     }
  274.  
  275.     /**
  276.      * The color white.  In the default sRGB space.
  277.      */
  278.     static public final function getWhite({
  279.       return new Color(255,255,255);
  280.     }
  281.  
  282.     /**
  283.      * The color white.  In the default sRGB space.
  284.      */
  285.     static public final function WHITE({
  286.       return self::getWhite();
  287.     }
  288.  
  289.     /**
  290.      * The color white smoke.  In the default sRGB space.
  291.      */
  292.     static public final function getWhiteSmoke({
  293.       return new Color(245245245);
  294.     }
  295.  
  296.     /**
  297.      * The color white smoke.  In the default sRGB space.
  298.      */
  299.     static public final function WHITE_SMOKE({
  300.       return self::getWhiteSmoke();
  301.     }
  302.  
  303.     /**
  304.      * The color navy.  In the default sRGB space.
  305.      */
  306.     static public final function getNavy({
  307.       return new Color(00128);
  308.     }
  309.  
  310.     /**
  311.      * The color navy.  In the default sRGB space.
  312.      */
  313.     static public final function NAVY({
  314.       return self::getNavy();
  315.     }
  316.  
  317.     /**
  318.      * The color gold.  In the default sRGB space.
  319.      */
  320.     static public final function getGold({
  321.       return new Color(2552150);
  322.     }
  323.  
  324.     /**
  325.      * The color gold.  In the default sRGB space.
  326.      */
  327.     static public final function GOLD({
  328.       return self::getGold();
  329.     }
  330.  
  331.     /*
  332.      * The color light yellow.  In the default sRGB space.
  333.      */
  334.     static public final function getLightYellow({
  335.       return new Color(255255224);
  336.     }
  337.  
  338.     /**
  339.      * The color light yellow.  In the default sRGB space.
  340.      */
  341.     static public final function LIGHT_YELLOW({
  342.       return self::getLightYellow();
  343.     }
  344.  
  345.     /**
  346.      * The color fuchsia.  In the default sRGB space.
  347.      */
  348.     static public final function getFuchsia({
  349.       return new Color(2550255);
  350.     }
  351.  
  352.     /**
  353.      * The color fuchsia.  In the default sRGB space.
  354.      */
  355.     static public final function FUCHSIA({
  356.       return self::getFuchsia();
  357.     }
  358.  
  359.     /**
  360.      * The color teal.  In the default sRGB space.
  361.      */
  362.     static public final function getTeal({
  363.       return new Color(0128128);
  364.     }
  365.  
  366.     /**
  367.      * The color teal.  In the default sRGB space.
  368.      */
  369.     static public final function TEAL({
  370.       return self::getTeal();
  371.     }
  372.  
  373.     /**
  374.      * The color maroon.  In the default sRGB space.
  375.      */
  376.     static public final function getMaroon({
  377.       return new Color(12800);
  378.     }
  379.  
  380.     /**
  381.      * The color maroon.  In the default sRGB space.
  382.      */
  383.     static public final function MAROON({
  384.       return self::getMaroon();
  385.     }
  386.  
  387.     /**
  388.      * The color lime.  In the default sRGB space.
  389.      */
  390.     static public final function getLime({
  391.       return new Color(02550);
  392.     }
  393.  
  394.     /**
  395.      * The color lime.  In the default sRGB space.
  396.      */
  397.     static public final function LIME({
  398.       return self::getLime();
  399.     }
  400.  
  401.     /**
  402.      * The color olive.  In the default sRGB space.
  403.      */
  404.     static public final function getOlive({
  405.       return new Color(1281280);
  406.     }
  407.  
  408.     /**
  409.      * The color olive.  In the default sRGB space.
  410.      */
  411.     static public final function OLIVE({
  412.       return self::getOlive();
  413.     }
  414.  
  415.     /**
  416.      * The color purple.  In the default sRGB space.
  417.      */
  418.     static public final function getPurple({
  419.       return new Color(1280128);
  420.     }
  421.  
  422.     /**
  423.      * The color purple.  In the default sRGB space.
  424.      */
  425.     static public final function PURPLE({
  426.       return self::getPurple();
  427.     }
  428.  
  429.     /**
  430.      * The color aqua.  In the default sRGB space.
  431.      */
  432.     static public final function getAqua({
  433.       return new Color(0255255);
  434.     }
  435.  
  436.     /**
  437.      * The color aqua.  In the default sRGB space.
  438.      */
  439.     static public final function AQUA({
  440.       return self::getAqua();
  441.     }
  442.  
  443.     /**
  444.      * The color green yellow.  In the default sRGB space.
  445.      */
  446.     static public final function getGreenYellow({
  447.       return new Color(17325547);
  448.     }
  449.  
  450.     /**
  451.      * The color green yellow.  In the default sRGB space.
  452.      */
  453.     static public final function GREEN_YELLOW({
  454.       return self::getGreenYellow();
  455.     }
  456.  
  457.     /**
  458.      * The color sky blue.  In the default sRGB space.
  459.      */
  460.     static public final function getSkyBlue({
  461.       return new Color(135206235);
  462.     }
  463.  
  464.     /**
  465.      * The color sky blue.  In the default sRGB space.
  466.      */
  467.     static public final function SKY_BLUE({
  468.       return self::getSkyBlue();
  469.     }
  470.  
  471.     /**
  472.      * The color bisque.  In the default sRGB space.
  473.      */
  474.     static public final function getBisque({
  475.       return new Color(255228196);
  476.     }
  477.  
  478.     /**
  479.      * The color bisque.  In the default sRGB space.
  480.      */
  481.     static public final function BISQUE({
  482.       return self::getBisque();
  483.     }
  484.  
  485.     /**
  486.      * The color indigo.  In the default sRGB space.
  487.      */
  488.     static public final function getIndigo({
  489.       return new Color(750130);
  490.     }
  491.  
  492.     /**
  493.      * The color indigo.  In the default sRGB space.
  494.      */
  495.     static public final function INDIGO({
  496.       return self::getIndigo();
  497.     }
  498.  
  499.     /**
  500.      * The color pink.  In the default sRGB space.
  501.      */
  502.     static public final function getPink({
  503.       return new Color(255,0,255);
  504.     }
  505.  
  506.     /**
  507.      * The color pink.  In the default sRGB space.
  508.      */
  509.     static public final function PINK({
  510.       return self::getPink();
  511.     }
  512.  
  513.     /**
  514.      * The color orange.  In the default sRGB space.
  515.      */
  516.     static public final function getOrange({
  517.       return new Color(255,128,64);
  518.     }
  519.  
  520.     /**
  521.      * The color orange.  In the default sRGB space.
  522.      */
  523.     static public final function ORANGE({
  524.       return self::getOrange();
  525.     }
  526.  
  527.     /**
  528.      * The color magenta.  In the default sRGB space.
  529.      */
  530.     static public final function getMagenta({
  531.       return new Color(1680168);
  532.     }
  533.  
  534.     /**
  535.      * The color magenta.  In the default sRGB space.
  536.      */
  537.     static public final function MAGENTA({
  538.       return self::getMagenta();
  539.     }
  540.  
  541.     /**
  542.      * The color cyan.  In the default sRGB space.
  543.      */
  544.     static public final function getCyan({
  545.       return new Color(0128128);
  546.     }
  547.  
  548.     /**
  549.      * The color cyan.  In the default sRGB space.
  550.      */
  551.     static public final function CYAN({
  552.       return self::getCyan();
  553.     }
  554.  
  555.     /**
  556.      * Returns Color with transparency percentage applied.
  557.      *
  558.      * @param value int
  559.      * @return Color with transparency percentage applied.
  560.      */
  561.     public function transparentColor($value{
  562.         return new Color(self::getRed()self::getGreen()self::getBlue(),
  563.                          MathUtils::round(((100 $value2.55)));
  564.     }
  565.  
  566.     /**
  567.      * Returns an integer from 0 to 100 that is the percent of transparency
  568.      * of this color.
  569.      *
  570.      * @param value Color
  571.      * @return int 
  572.      * @see #transparentColor
  573.      * @see java.awt.Color#getAlpha
  574.      */
  575.     static public function transparencyOf($value{
  576.         $a $value->getAlpha();
  577.         return (== 255(== 0100 : (int) (0.39216 (255 a));
  578.     }
  579.  
  580.  
  581.     /**
  582.      * Returns the alpha component in the range 0-255.
  583.      * @return the alpha component.
  584.      * @see #getRGB
  585.      */
  586.  
  587.     public function getAlpha({
  588.         return $this->alpha;
  589.         //return ($this->getRGB() >> 24) & 0xff;
  590.     }
  591.  
  592.     /**
  593.      * Returns the RGB value representing the color in the default sRGB
  594.      * {@link ColorModel}.
  595.      * (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are
  596.      * blue).
  597.      * @return the RGB value of the color in the default sRGB
  598.      *          <code>ColorModel</code>.
  599.      * @see java.awt.image.ColorModel#getRGBdefault
  600.      * @see #getRed
  601.      * @see #getGreen
  602.      * @see #getBlue
  603.      * @since JDK1.0
  604.      */
  605.     public function getRGB({
  606.         return 0/*value*/;   /* TODO review what to return ?? */
  607.     }
  608.  
  609.  
  610.     /**
  611.      * Converts the Color parameter to a darker color.<br> The HowMuch
  612.      * parameter indicates the quantity of dark increment.<br>
  613.      * It is used by Bar Series and Pie Series to calculate the right color t
  614.      * to draw Bar sides and Pie 3D zones.
  615.      *
  616.      * @param howMuch int
  617.      * @return Color 
  618.      */
  619.     public function applyDark($howMuch{
  620.         $r $this->red;
  621.         if ($r $howMuch{
  622.             $r -= $howMuch;
  623.         else {
  624.             $r 0;
  625.         }
  626.  
  627.         $gr $this->green;
  628.         if ($gr $howMuch{
  629.             $gr -= $howMuch;
  630.         else {
  631.             $gr 0;
  632.         }
  633.  
  634.         $b $this->blue;
  635.         if ($b $howMuch{
  636.             $b -= $howMuch;
  637.         else {
  638.             $b 0;
  639.         }
  640.  
  641.         return new Color($r$gr$b$this->alpha);
  642.     }
  643.  
  644.  
  645.     /*
  646.      * Converts the Color parameter to a brighter color.<br>
  647.      * The HowMuch parameter indicates the quantity of bright increment.<br>
  648.      * It is used by Styles.Bar Series and Styles.Pie Series to calculate the
  649.      * right color to draw Bar sides and Pie 3D zones.
  650.      *
  651.      * @param howMuch int
  652.      * @return Color
  653.      */
  654.     public function applyBright($howMuch{
  655.         $r $this->red;
  656.         if (($r $howMuch256{
  657.             $r += $howMuch;
  658.         else {
  659.             $r 255;
  660.         }
  661.  
  662.         $g $this->green;
  663.         if (($g $howMuch256{
  664.             $g += $howMuch;
  665.         else {
  666.             $g 255;
  667.         }
  668.  
  669.         $b $this->blue;
  670.         if (($b $howMuch256{
  671.             $b += $howMuch;
  672.         else {
  673.             $b 255;
  674.         }
  675.  
  676.         return new Color($r$g$b$this->getAlpha());
  677.     }
  678. }
  679.  
  680. ?>

Documentation generated on Wed, 16 Jun 2010 12:04:30 +0200 by phpDocumentor 1.4.1