Source of file Color.php
Size: 6,057 Bytes - Last Modified: 2021-12-24T06:41:13+00:00
/var/www/docs.ssmods.com/process/src/code/Field/Color.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 | <?php /** * DBField for Color manipulation and DB storage. * * Color are stored in the DB as RRGGBBAAA: * Hex RGB value + 3 digit Alpha * AAA = [0,100] : 100 = 1 alpha / 001 = 0.01 alpha * * @author Thierry Francois @colymba * * @package SS_ColorField * @subpackage Field */ class SS_Color extends DBField { /** * Color Hexadecimal value * @var string */ protected $hex; // RRGGBB /** * Decimal 0-1 Alpah value * @var float */ protected $alpha; // X.X /** * DBField contrustor * * @param string $name Field name */ public function __construct($name = null) { $this->defaultVal = '000000100'; parent::__construct($name); } /** * (non-PHPdoc) * @see DBField::requireField() */ public function requireField() { $parts = array( 'datatype' => 'varchar', 'precision' => 9, 'character set' => 'utf8', 'collate' => 'utf8_general_ci', 'arrayValue' => $this->arrayValue ); $values = array( 'type' => 'varchar', 'parts' => $parts ); // Add support for both SS DB API 3.2 and <3.2 if (method_exists('DB', 'require_field')) { DB::require_field($this->tableName, $this->name, $values); } else { DB::requireField($this->tableName, $this->name, $values); } } /** * (non-PHPdoc) * @see DBField::scaffoldFormField() */ public function scaffoldFormField($title = null) { $field = new ColorField($this->name, $title); return $field; } /** * Returns the value to be set in the database to blank this field. * * @return null */ public function nullValue() { return null; } /** * Make sure the field's actual value is in an acceptable format * or convert's it. * * @param mixed $value Field's value * @return string Correct field's value */ protected function formatValue($value) { $val = $value; if ($val === null || $val === '' || $val === 0) { $val = $this->defaultVal; } return $val; } /** * Sets the field's value * and sync Hex and Alpah properties. * * @param string $value Field's value (RRGGBBAAA) * @param mixed $record Associated record */ public function setValue($value, $record = null) { $value = $this->formatValue($value); $this->value = $value; $this->hex = $this->extractHex($value); $this->alpha = intval($this->extractAlpha($value)) / 100; } /** * Updates the fiels's value from the Hex and Alpha properties */ protected function syncValue() { $hex = $this->hex; $alpha = str_pad(($this->alpha * 100), 3, '0', STR_PAD_LEFT); $this->value = $hex . $alpha; } /** * Extrac the RGB Hex value from a RRGGBBAAA string * @param string $value RGBA String * @return string Hex RGB */ protected function extractHex($value) { return substr($value, 0, 6); } /** * Extrac the Alpah value from a RRGGBBAAA string * @param string $value RGBA String * @return string Alpha */ protected function extractAlpha($value) { return substr($value, -3); } /** * Sets the field's RGB value in Hex format * @param string $hex RGB or RRGGBB Hex value */ public function setHex($hex) { $this->hex = self::format_hex($hex); $this->syncValue(); return $this; } /** * Return the Hex representation of the color * @return string Hex value */ public function Hex() { return $this->hex; } /** * Sets the field's Alpah value * @param float $alpha Alpha value between 0 and 1 (e.g. 0.55) */ public function setAlpha($alpha) { $this->alpha = $alpha; $this->syncValue(); return $this; } /** * Return the Alpha value or the color * @return float Alpah value */ public function Alpha() { return number_format($this->alpha, 2); } /** * Return an RGB array representation of this color * @return array Associative RGB array */ public function RGB() { return self::hex_to_rgb($this->hex); } /** * Return the decimal Red component of the color * @return integer Red value */ public function R() { $rgb = self::hex_to_rgb($this->hex); return $rgb['R']; } /** * Return the decimal Green component of the color * @return integer Green value */ public function G() { $rgb = self::hex_to_rgb($this->hex); return $rgb['G']; } /** * Return the decimal Blue component of the color * @return integer Blue value */ public function B() { $rgb = self::hex_to_rgb($this->hex); return $rgb['B']; } /** * Return a correctly formatted Hex value RRGGBB * @param string $hex Possible Hex value * @return string RRGGBB hex value */ public static function format_hex($hex) { if (strlen($hex) < 6) { $r = substr($hex, 0, 1); $g = substr($hex, 1, 1); $b = substr($hex, 2, 1); return $r . $r . $g . $g . $b . $b; } else { return $hex; } } /** * Convert and Hex color into RGB decimal components * @param string $hex Hex color value * @return array Associative RGB array */ public static function hex_to_rgb($hex) { $hex = self::format_hex($hex); return array( 'R' => hexdec(substr($hex, 0, 2)), 'G' => hexdec(substr($hex, 2, 2)), 'B' => hexdec(substr($hex, 4, 2)) ); } /** * Convert RGB color component int an Hex color string * @param integer $r Red component * @param integer $g Green component * @param integer $b Blue component * @return string RRGGBB Hex value */ public static function rgb_to_hex($r, $g, $b) { $r = str_pad(dechex($r), 2, '0', STR_PAD_LEFT); $g = str_pad(dechex($g), 2, '0', STR_PAD_LEFT); $b = str_pad(dechex($b), 2, '0', STR_PAD_LEFT); return $r . $g . $b; } } |