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.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   1  <?php
   2  
   3  /**
   4   * SCSSPHP
   5   *
   6   * @copyright 2012-2020 Leaf Corcoran
   7   *
   8   * @license http://opensource.org/licenses/MIT MIT
   9   *
  10   * @link http://scssphp.github.io/scssphp
  11   */
  12  
  13  namespace ScssPhp\ScssPhp;
  14  
  15  /**
  16   * CSS Colors
  17   *
  18   * @author Leaf Corcoran <leafot@gmail.com>
  19   *
  20   * @internal
  21   */
  22  class Colors
  23  {
  24      /**
  25       * CSS Colors
  26       *
  27       * @see http://www.w3.org/TR/css3-color
  28       *
  29       * @var array<string, string>
  30       */
  31      protected static $cssColors = [
  32          'aliceblue' => '240,248,255',
  33          'antiquewhite' => '250,235,215',
  34          'aqua' => '0,255,255',
  35          'cyan' => '0,255,255',
  36          'aquamarine' => '127,255,212',
  37          'azure' => '240,255,255',
  38          'beige' => '245,245,220',
  39          'bisque' => '255,228,196',
  40          'black' => '0,0,0',
  41          'blanchedalmond' => '255,235,205',
  42          'blue' => '0,0,255',
  43          'blueviolet' => '138,43,226',
  44          'brown' => '165,42,42',
  45          'burlywood' => '222,184,135',
  46          'cadetblue' => '95,158,160',
  47          'chartreuse' => '127,255,0',
  48          'chocolate' => '210,105,30',
  49          'coral' => '255,127,80',
  50          'cornflowerblue' => '100,149,237',
  51          'cornsilk' => '255,248,220',
  52          'crimson' => '220,20,60',
  53          'darkblue' => '0,0,139',
  54          'darkcyan' => '0,139,139',
  55          'darkgoldenrod' => '184,134,11',
  56          'darkgray' => '169,169,169',
  57          'darkgrey' => '169,169,169',
  58          'darkgreen' => '0,100,0',
  59          'darkkhaki' => '189,183,107',
  60          'darkmagenta' => '139,0,139',
  61          'darkolivegreen' => '85,107,47',
  62          'darkorange' => '255,140,0',
  63          'darkorchid' => '153,50,204',
  64          'darkred' => '139,0,0',
  65          'darksalmon' => '233,150,122',
  66          'darkseagreen' => '143,188,143',
  67          'darkslateblue' => '72,61,139',
  68          'darkslategray' => '47,79,79',
  69          'darkslategrey' => '47,79,79',
  70          'darkturquoise' => '0,206,209',
  71          'darkviolet' => '148,0,211',
  72          'deeppink' => '255,20,147',
  73          'deepskyblue' => '0,191,255',
  74          'dimgray' => '105,105,105',
  75          'dimgrey' => '105,105,105',
  76          'dodgerblue' => '30,144,255',
  77          'firebrick' => '178,34,34',
  78          'floralwhite' => '255,250,240',
  79          'forestgreen' => '34,139,34',
  80          'fuchsia' => '255,0,255',
  81          'magenta' => '255,0,255',
  82          'gainsboro' => '220,220,220',
  83          'ghostwhite' => '248,248,255',
  84          'gold' => '255,215,0',
  85          'goldenrod' => '218,165,32',
  86          'gray' => '128,128,128',
  87          'grey' => '128,128,128',
  88          'green' => '0,128,0',
  89          'greenyellow' => '173,255,47',
  90          'honeydew' => '240,255,240',
  91          'hotpink' => '255,105,180',
  92          'indianred' => '205,92,92',
  93          'indigo' => '75,0,130',
  94          'ivory' => '255,255,240',
  95          'khaki' => '240,230,140',
  96          'lavender' => '230,230,250',
  97          'lavenderblush' => '255,240,245',
  98          'lawngreen' => '124,252,0',
  99          'lemonchiffon' => '255,250,205',
 100          'lightblue' => '173,216,230',
 101          'lightcoral' => '240,128,128',
 102          'lightcyan' => '224,255,255',
 103          'lightgoldenrodyellow' => '250,250,210',
 104          'lightgray' => '211,211,211',
 105          'lightgrey' => '211,211,211',
 106          'lightgreen' => '144,238,144',
 107          'lightpink' => '255,182,193',
 108          'lightsalmon' => '255,160,122',
 109          'lightseagreen' => '32,178,170',
 110          'lightskyblue' => '135,206,250',
 111          'lightslategray' => '119,136,153',
 112          'lightslategrey' => '119,136,153',
 113          'lightsteelblue' => '176,196,222',
 114          'lightyellow' => '255,255,224',
 115          'lime' => '0,255,0',
 116          'limegreen' => '50,205,50',
 117          'linen' => '250,240,230',
 118          'maroon' => '128,0,0',
 119          'mediumaquamarine' => '102,205,170',
 120          'mediumblue' => '0,0,205',
 121          'mediumorchid' => '186,85,211',
 122          'mediumpurple' => '147,112,219',
 123          'mediumseagreen' => '60,179,113',
 124          'mediumslateblue' => '123,104,238',
 125          'mediumspringgreen' => '0,250,154',
 126          'mediumturquoise' => '72,209,204',
 127          'mediumvioletred' => '199,21,133',
 128          'midnightblue' => '25,25,112',
 129          'mintcream' => '245,255,250',
 130          'mistyrose' => '255,228,225',
 131          'moccasin' => '255,228,181',
 132          'navajowhite' => '255,222,173',
 133          'navy' => '0,0,128',
 134          'oldlace' => '253,245,230',
 135          'olive' => '128,128,0',
 136          'olivedrab' => '107,142,35',
 137          'orange' => '255,165,0',
 138          'orangered' => '255,69,0',
 139          'orchid' => '218,112,214',
 140          'palegoldenrod' => '238,232,170',
 141          'palegreen' => '152,251,152',
 142          'paleturquoise' => '175,238,238',
 143          'palevioletred' => '219,112,147',
 144          'papayawhip' => '255,239,213',
 145          'peachpuff' => '255,218,185',
 146          'peru' => '205,133,63',
 147          'pink' => '255,192,203',
 148          'plum' => '221,160,221',
 149          'powderblue' => '176,224,230',
 150          'purple' => '128,0,128',
 151          'red' => '255,0,0',
 152          'rosybrown' => '188,143,143',
 153          'royalblue' => '65,105,225',
 154          'saddlebrown' => '139,69,19',
 155          'salmon' => '250,128,114',
 156          'sandybrown' => '244,164,96',
 157          'seagreen' => '46,139,87',
 158          'seashell' => '255,245,238',
 159          'sienna' => '160,82,45',
 160          'silver' => '192,192,192',
 161          'skyblue' => '135,206,235',
 162          'slateblue' => '106,90,205',
 163          'slategray' => '112,128,144',
 164          'slategrey' => '112,128,144',
 165          'snow' => '255,250,250',
 166          'springgreen' => '0,255,127',
 167          'steelblue' => '70,130,180',
 168          'tan' => '210,180,140',
 169          'teal' => '0,128,128',
 170          'thistle' => '216,191,216',
 171          'tomato' => '255,99,71',
 172          'turquoise' => '64,224,208',
 173          'violet' => '238,130,238',
 174          'wheat' => '245,222,179',
 175          'white' => '255,255,255',
 176          'whitesmoke' => '245,245,245',
 177          'yellow' => '255,255,0',
 178          'yellowgreen' => '154,205,50',
 179          'rebeccapurple' => '102,51,153',
 180          'transparent' => '0,0,0,0',
 181      ];
 182  
 183      /**
 184       * Convert named color in a [r,g,b[,a]] array
 185       *
 186       * @param string $colorName
 187       *
 188       * @return int[]|null
 189       */
 190      public static function colorNameToRGBa($colorName)
 191      {
 192          if (\is_string($colorName) && isset(static::$cssColors[$colorName])) {
 193              $rgba = explode(',', static::$cssColors[$colorName]);
 194  
 195              // only case with opacity is transparent, with opacity=0, so we can intval on opacity also
 196              $rgba = array_map('intval', $rgba);
 197  
 198              return $rgba;
 199          }
 200  
 201          return null;
 202      }
 203  
 204      /**
 205       * Reverse conversion : from RGBA to a color name if possible
 206       *
 207       * @param int       $r
 208       * @param int       $g
 209       * @param int       $b
 210       * @param int|float $a
 211       *
 212       * @return string|null
 213       */
 214      public static function RGBaToColorName($r, $g, $b, $a = 1)
 215      {
 216          static $reverseColorTable = null;
 217  
 218          if (! is_numeric($r) || ! is_numeric($g) || ! is_numeric($b) || ! is_numeric($a)) {
 219              return null;
 220          }
 221  
 222          if ($a < 1) {
 223              return null;
 224          }
 225  
 226          if (\is_null($reverseColorTable)) {
 227              $reverseColorTable = [];
 228  
 229              foreach (static::$cssColors as $name => $rgb_str) {
 230                  $rgb_str = explode(',', $rgb_str);
 231  
 232                  if (
 233                      \count($rgb_str) == 3 &&
 234                      ! isset($reverseColorTable[\intval($rgb_str[0])][\intval($rgb_str[1])][\intval($rgb_str[2])])
 235                  ) {
 236                      $reverseColorTable[\intval($rgb_str[0])][\intval($rgb_str[1])][\intval($rgb_str[2])] = $name;
 237                  }
 238              }
 239          }
 240  
 241          if (isset($reverseColorTable[\intval($r)][\intval($g)][\intval($b)])) {
 242              return $reverseColorTable[\intval($r)][\intval($g)][\intval($b)];
 243          }
 244  
 245          return null;
 246      }
 247  }