Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  
   3  namespace Sabberworm\CSS\Value;
   4  
   5  use Sabberworm\CSS\Parsing\ParserState;
   6  
   7  class Color extends CSSFunction {
   8  
   9  	public function __construct($aColor, $iLineNo = 0) {
  10  	 	 parent::__construct(implode('', array_keys($aColor)), $aColor, ',', $iLineNo);
  11  	 }
  12  
  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  
  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  }