source-class-Com.Tecnick.Color.Model

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1: <?php
  2: /**
  3:  * Model.php
  4:  *
  5:  * @since       2015-02-21
  6:  * @category    Library
  7:  * @package     Color
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2015-2015 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-color
 12:  *
 13:  * This file is part of tc-lib-color software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Color;
 17: 
 18: /**
 19:  * Com\Tecnick\Color\Model
 20:  *
 21:  * Color Model class
 22:  *
 23:  * @since       2015-02-21
 24:  * @category    Library
 25:  * @package     Color
 26:  * @author      Nicola Asuni <info@tecnick.com>
 27:  * @copyright   2015-2015 Nicola Asuni - Tecnick.com LTD
 28:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 29:  * @link        https://github.com/tecnickcom/tc-lib-color
 30:  */
 31: abstract class Model
 32: {
 33:     /**
 34:      * Color Model type (GRAY, RGB, HSL, CMYK)
 35:      *
 36:      * @var string
 37:      */
 38:     protected $type;
 39:     
 40:     /**
 41:      * Value of the Alpha channel component.
 42:      * Values range between 0.0 (fully transparent) and 1.0 (fully opaque)
 43:      *
 44:      * @var float
 45:      */
 46:     protected $cmp_alpha = 1.0;
 47: 
 48:     /**
 49:      * Initialize a new color object
 50:      *
 51:      * @param array $components Array of color components is the range [0..1]
 52:      */
 53:     public function __construct($components)
 54:     {
 55:         foreach ($components as $color => $value) {
 56:             $property = 'cmp_'.$color;
 57:             if (property_exists($this, $property)) {
 58:                 $this->$property = (max(0, min(1, floatval($value))));
 59:             }
 60:         }
 61:     }
 62: 
 63:     /**
 64:      * Get the color model type (GRAY, RGB, HSL, CMYK)
 65:      *
 66:      * @return string
 67:      */
 68:     public function getType()
 69:     {
 70:         return $this->type;
 71:     }
 72: 
 73:     /**
 74:      * Get the normalized integer value of the specified float fraction
 75:      *
 76:      * @param float $value Fraction value to convert [0..1]
 77:      * @param int   $max   Maximum value to return (reference value)
 78:      *
 79:      * @return integer value [0..$max]
 80:      */
 81:     public function getNormalizedValue($value, $max)
 82:     {
 83:         return round(max(0, min($max, ($max * floatval($value)))));
 84:     }
 85: 
 86:     /**
 87:      * Get the normalized hexadecimal value of the specified float fraction
 88:      *
 89:      * @param float $value Fraction value to convert [0..1]
 90:      * @param int   $max   Maximum value to return (reference value)
 91:      *
 92:      * @return integer value [0..$max]
 93:      */
 94:     public function getHexValue($value, $max)
 95:     {
 96:         return sprintf('%02x', $this->getNormalizedValue($value, $max));
 97:     }
 98: 
 99:     /**
100:      * Get the Hexadecimal representation of the color with alpha channel: #RRGGBBAA
101:      *
102:      * @return string
103:      */
104:     public function getRgbaHexColor()
105:     {
106:         $rgba = $this->toRgbArray();
107:         return '#'
108:             .$this->getHexValue($rgba['red'], 255)
109:             .$this->getHexValue($rgba['green'], 255)
110:             .$this->getHexValue($rgba['blue'], 255)
111:             .$this->getHexValue($rgba['alpha'], 255);
112:     }
113: 
114:     /**
115:      * Get the Hexadecimal representation of the color: #RRGGBB
116:      *
117:      * @return string
118:      */
119:     public function getRgbHexColor()
120:     {
121:         $rgba = $this->toRgbArray();
122:         return '#'
123:             .$this->getHexValue($rgba['red'], 255)
124:             .$this->getHexValue($rgba['green'], 255)
125:             .$this->getHexValue($rgba['blue'], 255);
126:     }
127: }
128: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy