Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace OpenSpout\Common\Entity\Style;
   6  
   7  use OpenSpout\Common\Exception\InvalidColorException;
   8  
   9  /**
  10   * This class provides constants and functions to work with colors.
  11   */
  12  final class Color
  13  {
  14      /**
  15       * Standard colors - based on Office Online.
  16       */
  17      public const BLACK = '000000';
  18      public const WHITE = 'FFFFFF';
  19      public const RED = 'FF0000';
  20      public const DARK_RED = 'C00000';
  21      public const ORANGE = 'FFC000';
  22      public const YELLOW = 'FFFF00';
  23      public const LIGHT_GREEN = '92D040';
  24      public const GREEN = '00B050';
  25      public const LIGHT_BLUE = '00B0E0';
  26      public const BLUE = '0070C0';
  27      public const DARK_BLUE = '002060';
  28      public const PURPLE = '7030A0';
  29  
  30      /**
  31       * Returns an RGB color from R, G and B values.
  32       *
  33       * @param int $red   Red component, 0 - 255
  34       * @param int $green Green component, 0 - 255
  35       * @param int $blue  Blue component, 0 - 255
  36       *
  37       * @return string RGB color
  38       */
  39      public static function rgb(int $red, int $green, int $blue): string
  40      {
  41          self::throwIfInvalidColorComponentValue($red);
  42          self::throwIfInvalidColorComponentValue($green);
  43          self::throwIfInvalidColorComponentValue($blue);
  44  
  45          return strtoupper(
  46              self::convertColorComponentToHex($red).
  47              self::convertColorComponentToHex($green).
  48              self::convertColorComponentToHex($blue)
  49          );
  50      }
  51  
  52      /**
  53       * Returns the ARGB color of the given RGB color,
  54       * assuming that alpha value is always 1.
  55       *
  56       * @param string $rgbColor RGB color like "FF08B2"
  57       *
  58       * @return string ARGB color
  59       */
  60      public static function toARGB(string $rgbColor): string
  61      {
  62          return 'FF'.$rgbColor;
  63      }
  64  
  65      /**
  66       * Throws an exception is the color component value is outside of bounds (0 - 255).
  67       *
  68       * @throws \OpenSpout\Common\Exception\InvalidColorException
  69       */
  70      private static function throwIfInvalidColorComponentValue(int $colorComponent): void
  71      {
  72          if ($colorComponent < 0 || $colorComponent > 255) {
  73              throw new InvalidColorException("The RGB components must be between 0 and 255. Received: {$colorComponent}");
  74          }
  75      }
  76  
  77      /**
  78       * Converts the color component to its corresponding hexadecimal value.
  79       *
  80       * @param int $colorComponent Color component, 0 - 255
  81       *
  82       * @return string Corresponding hexadecimal value, with a leading 0 if needed. E.g "0f", "2d"
  83       */
  84      private static function convertColorComponentToHex(int $colorComponent): string
  85      {
  86          return str_pad(dechex($colorComponent), 2, '0', STR_PAD_LEFT);
  87      }
  88  }