MDL-65759 library: Update php-css-parser to 8.3.0
[moodle.git] / lib / php-css-parser / Value / Color.php
CommitLineData
fbe18cc0
FM
1<?php
2
3namespace Sabberworm\CSS\Value;
4
376eb156
MM
5use Sabberworm\CSS\Parsing\ParserState;
6
fbe18cc0
FM
7class Color extends CSSFunction {
8
9 public function __construct($aColor, $iLineNo = 0) {
10 parent::__construct(implode('', array_keys($aColor)), $aColor, ',', $iLineNo);
11 }
12
376eb156
MM
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];
22 }
23
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())
30 );
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())
36 );
37 }
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(',');
49 }
50 }
51 $oParserState->consume(')');
52 }
53 return new Color($aColor, $oParserState->currentLine());
54 }
55
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;
63 }
64
fbe18cc0
FM
65 public function getColor() {
66 return $this->aComponents;
67 }
68
69 public function setColor($aColor) {
70 $this->setName(implode('', array_keys($aColor)));
71 $this->aComponents = $aColor;
72 }
73
74 public function getColorDescription() {
75 return $this->getName();
76 }
77
78 public function __toString() {
79 return $this->render(new \Sabberworm\CSS\OutputFormat());
80 }
81
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()
90 );
91 return '#'.(($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5]) ? "$sResult[0]$sResult[2]$sResult[4]" : $sResult);
92 }
93 return parent::render($oOutputFormat);
94 }
95}