Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Chart;
   4  
   5  class ChartColor
   6  {
   7      const EXCEL_COLOR_TYPE_STANDARD = 'prstClr';
   8      const EXCEL_COLOR_TYPE_SCHEME = 'schemeClr';
   9      const EXCEL_COLOR_TYPE_RGB = 'srgbClr';
  10      /** @deprecated 1.24 use EXCEL_COLOR_TYPE_RGB instead */
  11      const EXCEL_COLOR_TYPE_ARGB = 'srgbClr';
  12      const EXCEL_COLOR_TYPES = [
  13          self::EXCEL_COLOR_TYPE_ARGB,
  14          self::EXCEL_COLOR_TYPE_SCHEME,
  15          self::EXCEL_COLOR_TYPE_STANDARD,
  16      ];
  17  
  18      /** @var string */
  19      private $value = '';
  20  
  21      /** @var string */
  22      private $type = '';
  23  
  24      /** @var ?int */
  25      private $alpha;
  26  
  27      /** @var ?int */
  28      private $brightness;
  29  
  30      /**
  31       * @param string|string[] $value
  32       */
  33      public function __construct($value = '', ?int $alpha = null, ?string $type = null, ?int $brightness = null)
  34      {
  35          if (is_array($value)) {
  36              $this->setColorPropertiesArray($value);
  37          } else {
  38              $this->setColorProperties($value, $alpha, $type, $brightness);
  39          }
  40      }
  41  
  42      public function getValue(): string
  43      {
  44          return $this->value;
  45      }
  46  
  47      public function setValue(string $value): self
  48      {
  49          $this->value = $value;
  50  
  51          return $this;
  52      }
  53  
  54      public function getType(): string
  55      {
  56          return $this->type;
  57      }
  58  
  59      public function setType(string $type): self
  60      {
  61          $this->type = $type;
  62  
  63          return $this;
  64      }
  65  
  66      public function getAlpha(): ?int
  67      {
  68          return $this->alpha;
  69      }
  70  
  71      public function setAlpha(?int $alpha): self
  72      {
  73          $this->alpha = $alpha;
  74  
  75          return $this;
  76      }
  77  
  78      public function getBrightness(): ?int
  79      {
  80          return $this->brightness;
  81      }
  82  
  83      public function setBrightness(?int $brightness): self
  84      {
  85          $this->brightness = $brightness;
  86  
  87          return $this;
  88      }
  89  
  90      /**
  91       * @param null|float|int|string $alpha
  92       * @param null|float|int|string $brightness
  93       */
  94      public function setColorProperties(?string $color, $alpha = null, ?string $type = null, $brightness = null): self
  95      {
  96          if (empty($type) && !empty($color)) {
  97              if (substr($color, 0, 1) === '*') {
  98                  $type = 'schemeClr';
  99                  $color = substr($color, 1);
 100              } elseif (substr($color, 0, 1) === '/') {
 101                  $type = 'prstClr';
 102                  $color = substr($color, 1);
 103              } elseif (preg_match('/^[0-9A-Fa-f]{6}$/', $color) === 1) {
 104                  $type = 'srgbClr';
 105              }
 106          }
 107          if ($color !== null) {
 108              $this->setValue("$color");
 109          }
 110          if ($type !== null) {
 111              $this->setType($type);
 112          }
 113          if ($alpha === null) {
 114              $this->setAlpha(null);
 115          } elseif (is_numeric($alpha)) {
 116              $this->setAlpha((int) $alpha);
 117          }
 118          if ($brightness === null) {
 119              $this->setBrightness(null);
 120          } elseif (is_numeric($brightness)) {
 121              $this->setBrightness((int) $brightness);
 122          }
 123  
 124          return $this;
 125      }
 126  
 127      public function setColorPropertiesArray(array $color): self
 128      {
 129          return $this->setColorProperties(
 130              $color['value'] ?? '',
 131              $color['alpha'] ?? null,
 132              $color['type'] ?? null,
 133              $color['brightness'] ?? null
 134          );
 135      }
 136  
 137      public function isUsable(): bool
 138      {
 139          return $this->type !== '' && $this->value !== '';
 140      }
 141  
 142      /**
 143       * Get Color Property.
 144       *
 145       * @param string $propertyName
 146       *
 147       * @return null|int|string
 148       */
 149      public function getColorProperty($propertyName)
 150      {
 151          $retVal = null;
 152          if ($propertyName === 'value') {
 153              $retVal = $this->value;
 154          } elseif ($propertyName === 'type') {
 155              $retVal = $this->type;
 156          } elseif ($propertyName === 'alpha') {
 157              $retVal = $this->alpha;
 158          } elseif ($propertyName === 'brightness') {
 159              $retVal = $this->brightness;
 160          }
 161  
 162          return $retVal;
 163      }
 164  
 165      public static function alphaToXml(int $alpha): string
 166      {
 167          return (string) (100 - $alpha) . '000';
 168      }
 169  
 170      /**
 171       * @param float|int|string $alpha
 172       */
 173      public static function alphaFromXml($alpha): int
 174      {
 175          return 100 - ((int) $alpha / 1000);
 176      }
 177  }