Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

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