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 310 and 311] [Versions 39 and 311]

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