Moodle release 4.0
[moodle.git] / lib / php-css-parser / Value / Color.php
blobc6ed9b18b1683cb18e4e7e3973077d34cbaced28
1 <?php
3 namespace Sabberworm\CSS\Value;
5 use Sabberworm\CSS\Parsing\ParserState;
7 class Color extends CSSFunction {
9 public function __construct($aColor, $iLineNo = 0) {
10 parent::__construct(implode('', array_keys($aColor)), $aColor, ',', $iLineNo);
13 public static function parse(ParserState $oParserState) {
14 $aColor = array();
15 if ($oParserState->comes('#')) {
16 $oParserState->consume('#');
17 $sValue = $oParserState->parseIdentifier(false);
18 if ($oParserState->strlen($sValue) === 3) {
19 $sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2];
20 } else if ($oParserState->strlen($sValue) === 4) {
21 $sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2] . $sValue[3] . $sValue[3];
24 if ($oParserState->strlen($sValue) === 8) {
25 $aColor = array(
26 'r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()),
27 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()),
28 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()),
29 'a' => new Size(round(self::mapRange(intval($sValue[6] . $sValue[7], 16), 0, 255, 0, 1), 2), null, true, $oParserState->currentLine())
31 } else {
32 $aColor = array(
33 'r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()),
34 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()),
35 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine())
38 } else {
39 $sColorMode = $oParserState->parseIdentifier(true);
40 $oParserState->consumeWhiteSpace();
41 $oParserState->consume('(');
42 $iLength = $oParserState->strlen($sColorMode);
43 for ($i = 0; $i < $iLength; ++$i) {
44 $oParserState->consumeWhiteSpace();
45 $aColor[$sColorMode[$i]] = Size::parse($oParserState, true);
46 $oParserState->consumeWhiteSpace();
47 if ($i < ($iLength - 1)) {
48 $oParserState->consume(',');
51 $oParserState->consume(')');
53 return new Color($aColor, $oParserState->currentLine());
56 private static function mapRange($fVal, $fFromMin, $fFromMax, $fToMin, $fToMax) {
57 $fFromRange = $fFromMax - $fFromMin;
58 $fToRange = $fToMax - $fToMin;
59 $fMultiplier = $fToRange / $fFromRange;
60 $fNewVal = $fVal - $fFromMin;
61 $fNewVal *= $fMultiplier;
62 return $fNewVal + $fToMin;
65 public function getColor() {
66 return $this->aComponents;
69 public function setColor($aColor) {
70 $this->setName(implode('', array_keys($aColor)));
71 $this->aComponents = $aColor;
74 public function getColorDescription() {
75 return $this->getName();
78 public function __toString() {
79 return $this->render(new \Sabberworm\CSS\OutputFormat());
82 public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
83 // Shorthand RGB color values
84 if($oOutputFormat->getRGBHashNotation() && implode('', array_keys($this->aComponents)) === 'rgb') {
85 $sResult = sprintf(
86 '%02x%02x%02x',
87 $this->aComponents['r']->getSize(),
88 $this->aComponents['g']->getSize(),
89 $this->aComponents['b']->getSize()
91 return '#'.(($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5]) ? "$sResult[0]$sResult[2]$sResult[4]" : $sResult);
93 return parent::render($oOutputFormat);