a3_lessc

The less compiler and parser.

Defined (1)

The class is defined in the following location(s).

/admin/less/lib/lessc.inc.php  
  1. class a3_lessc { 
  2. static public $VERSION = "v0.4.0"; 
  3. static protected $TRUE = array("keyword", "true"); 
  4. static protected $FALSE = array("keyword", "false"); 
  5.  
  6. protected $libFunctions = array(); 
  7. protected $registeredVars = array(); 
  8. protected $preserveComments = false; 
  9.  
  10. public $vPrefix = '@'; // prefix of abstract properties 
  11. public $mPrefix = '$'; // prefix of abstract blocks 
  12. public $parentSelector = '&'; 
  13.  
  14. public $importDisabled = false; 
  15. public $importDir = ''; 
  16.  
  17. protected $numberPrecision = null; 
  18.  
  19. protected $allParsedFiles = array(); 
  20.  
  21. // set to the parser that generated the current line when compiling 
  22. // so we know how to create error messages 
  23. protected $sourceParser = null; 
  24. protected $sourceLoc = null; 
  25.  
  26. static public $defaultValue = array("keyword", ""); 
  27.  
  28. static protected $nextImportId = 0; // uniquely identify imports 
  29.  
  30. // attempts to find the path of an import url, returns null for css files 
  31. protected function findImport($url) { 
  32. foreach ((array)$this->importDir as $dir) { 
  33. $full = $dir.(substr($dir, -1) != '/' ? '/' : '').$url; 
  34. if ($this->fileExists($file = $full.'.less') || $this->fileExists($file = $full)) { 
  35. return $file; 
  36.  
  37. return null; 
  38.  
  39. protected function fileExists($name) { 
  40. return is_file($name); 
  41.  
  42. static public function compressList($items, $delim) { 
  43. if (!isset($items[1]) && isset($items[0])) return $items[0]; 
  44. else return array('list', $delim, $items); 
  45.  
  46. static public function preg_quote($what) { 
  47. return preg_quote($what, '/'); 
  48.  
  49. protected function tryImport($importPath, $parentBlock, $out) { 
  50. if ($importPath[0] == "function" && $importPath[1] == "url") { 
  51. $importPath = $this->flattenList($importPath[2]); 
  52.  
  53. $str = $this->coerceString($importPath); 
  54. if ($str === null) return false; 
  55.  
  56. $url = $this->compileValue($this->lib_e($str)); 
  57.  
  58. // don't import if it ends in css 
  59. if (substr_compare($url, '.css', -4, 4) === 0) return false; 
  60.  
  61. $realPath = $this->findImport($url); 
  62.  
  63. if ($realPath === null) return false; 
  64.  
  65. if ($this->importDisabled) { 
  66. return array(false, "/* import disabled */"); 
  67.  
  68. if (isset($this->allParsedFiles[realpath($realPath)])) { 
  69. return array(false, null); 
  70.  
  71. $this->addParsedFile($realPath); 
  72. $parser = $this->makeParser($realPath); 
  73. $root = $parser->parse(file_get_contents($realPath)); 
  74.  
  75. // set the parents of all the block props 
  76. foreach ($root->props as $prop) { 
  77. if ($prop[0] == "block") { 
  78. $prop[1]->parent = $parentBlock; 
  79.  
  80. // copy mixins into scope, set their parents 
  81. // bring blocks from import into current block 
  82. // TODO: need to mark the source parser these came from this file 
  83. foreach ($root->children as $childName => $child) { 
  84. if (isset($parentBlock->children[$childName])) { 
  85. $parentBlock->children[$childName] = array_merge( 
  86. $parentBlock->children[$childName],  
  87. $child); 
  88. } else { 
  89. $parentBlock->children[$childName] = $child; 
  90.  
  91. $pi = pathinfo($realPath); 
  92. $dir = $pi["dirname"]; 
  93.  
  94. list($top, $bottom) = $this->sortProps($root->props, true); 
  95. $this->compileImportedProps($top, $parentBlock, $out, $parser, $dir); 
  96.  
  97. return array(true, $bottom, $parser, $dir); 
  98.  
  99. protected function compileImportedProps($props, $block, $out, $sourceParser, $importDir) { 
  100. $oldSourceParser = $this->sourceParser; 
  101.  
  102. $oldImport = $this->importDir; 
  103.  
  104. // TODO: this is because the importDir api is stupid 
  105. $this->importDir = (array)$this->importDir; 
  106. array_unshift($this->importDir, $importDir); 
  107.  
  108. foreach ($props as $prop) { 
  109. $this->compileProp($prop, $block, $out); 
  110.  
  111. $this->importDir = $oldImport; 
  112. $this->sourceParser = $oldSourceParser; 
  113.  
  114. /** 
  115. * Recursively compiles a block. 
  116. * A block is analogous to a CSS block in most cases. A single LESS document 
  117. * is encapsulated in a block when parsed, but it does not have parent tags 
  118. * so all of it's children appear on the root level when compiled. 
  119. * Blocks are made up of props and children. 
  120. * Props are property instructions, array tuples which describe an action 
  121. * to be taken, eg. write a property, set a variable, mixin a block. 
  122. * The children of a block are just all the blocks that are defined within. 
  123. * This is used to look up mixins when performing a mixin. 
  124. * Compiling the block involves pushing a fresh environment on the stack,  
  125. * and iterating through the props, compiling each one. 
  126. * See a3_lessc::compileProp() 
  127. */ 
  128. protected function compileBlock($block) { 
  129. switch ($block->type) { 
  130. case "root": 
  131. $this->compileRoot($block); 
  132. break; 
  133. case null: 
  134. $this->compileCSSBlock($block); 
  135. break; 
  136. case "media": 
  137. $this->compileMedia($block); 
  138. break; 
  139. case "directive": 
  140. $name = "@" . $block->name; 
  141. if (!empty($block->value)) { 
  142. $name .= " " . $this->compileValue($this->reduce($block->value)); 
  143.  
  144. $this->compileNestedBlock($block, array($name)); 
  145. break; 
  146. default: 
  147. $this->throwError("unknown block type: $block->type\n"); 
  148.  
  149. protected function compileCSSBlock($block) { 
  150. $env = $this->pushEnv(); 
  151.  
  152. $selectors = $this->compileSelectors($block->tags); 
  153. $env->selectors = $this->multiplySelectors($selectors); 
  154. $out = $this->makeOutputBlock(null, $env->selectors); 
  155.  
  156. $this->scope->children[] = $out; 
  157. $this->compileProps($block, $out); 
  158.  
  159. $block->scope = $env; // mixins carry scope with them! 
  160. $this->popEnv(); 
  161.  
  162. protected function compileMedia($media) { 
  163. $env = $this->pushEnv($media); 
  164. $parentScope = $this->mediaParent($this->scope); 
  165.  
  166. $query = $this->compileMediaQuery($this->multiplyMedia($env)); 
  167.  
  168. $this->scope = $this->makeOutputBlock($media->type, array($query)); 
  169. $parentScope->children[] = $this->scope; 
  170.  
  171. $this->compileProps($media, $this->scope); 
  172.  
  173. if (count($this->scope->lines) > 0) { 
  174. $orphanSelelectors = $this->findClosestSelectors(); 
  175. if (!is_null($orphanSelelectors)) { 
  176. $orphan = $this->makeOutputBlock(null, $orphanSelelectors); 
  177. $orphan->lines = $this->scope->lines; 
  178. array_unshift($this->scope->children, $orphan); 
  179. $this->scope->lines = array(); 
  180.  
  181. $this->scope = $this->scope->parent; 
  182. $this->popEnv(); 
  183.  
  184. protected function mediaParent($scope) { 
  185. while (!empty($scope->parent)) { 
  186. if (!empty($scope->type) && $scope->type != "media") { 
  187. break; 
  188. $scope = $scope->parent; 
  189.  
  190. return $scope; 
  191.  
  192. protected function compileNestedBlock($block, $selectors) { 
  193. $this->pushEnv($block); 
  194. $this->scope = $this->makeOutputBlock($block->type, $selectors); 
  195. $this->scope->parent->children[] = $this->scope; 
  196.  
  197. $this->compileProps($block, $this->scope); 
  198.  
  199. $this->scope = $this->scope->parent; 
  200. $this->popEnv(); 
  201.  
  202. protected function compileRoot($root) { 
  203. $this->pushEnv(); 
  204. $this->scope = $this->makeOutputBlock($root->type); 
  205. $this->compileProps($root, $this->scope); 
  206. $this->popEnv(); 
  207.  
  208. protected function compileProps($block, $out) { 
  209. foreach ($this->sortProps($block->props) as $prop) { 
  210. $this->compileProp($prop, $block, $out); 
  211.  
  212. $out->lines = array_values(array_unique($out->lines)); 
  213.  
  214. protected function sortProps($props, $split = false) { 
  215. $vars = array(); 
  216. $imports = array(); 
  217. $other = array(); 
  218.  
  219. foreach ($props as $prop) { 
  220. switch ($prop[0]) { 
  221. case "assign": 
  222. if (isset($prop[1][0]) && $prop[1][0] == $this->vPrefix) { 
  223. $vars[] = $prop; 
  224. } else { 
  225. $other[] = $prop; 
  226. break; 
  227. case "import": 
  228. $id = self::$nextImportId++; 
  229. $prop[] = $id; 
  230. $imports[] = $prop; 
  231. $other[] = array("import_mixin", $id); 
  232. break; 
  233. default: 
  234. $other[] = $prop; 
  235.  
  236. if ($split) { 
  237. return array(array_merge($vars, $imports), $other); 
  238. } else { 
  239. return array_merge($vars, $imports, $other); 
  240.  
  241. protected function compileMediaQuery($queries) { 
  242. $compiledQueries = array(); 
  243. foreach ($queries as $query) { 
  244. $parts = array(); 
  245. foreach ($query as $q) { 
  246. switch ($q[0]) { 
  247. case "mediaType": 
  248. $parts[] = implode(" ", array_slice($q, 1)); 
  249. break; 
  250. case "mediaExp": 
  251. if (isset($q[2])) { 
  252. $parts[] = "($q[1]: " . 
  253. $this->compileValue($this->reduce($q[2])) . ")"; 
  254. } else { 
  255. $parts[] = "($q[1])"; 
  256. break; 
  257. case "variable": 
  258. $parts[] = $this->compileValue($this->reduce($q)); 
  259. break; 
  260.  
  261. if (count($parts) > 0) { 
  262. $compiledQueries[] = implode(" and ", $parts); 
  263.  
  264. $out = "@media"; 
  265. if (!empty($parts)) { 
  266. $out .= " " . 
  267. implode($this->formatter->selectorSeparator, $compiledQueries); 
  268. return $out; 
  269.  
  270. protected function multiplyMedia($env, $childQueries = null) { 
  271. if (is_null($env) || 
  272. !empty($env->block->type) && $env->block->type != "media") 
  273. return $childQueries; 
  274.  
  275. // plain old block, skip 
  276. if (empty($env->block->type)) { 
  277. return $this->multiplyMedia($env->parent, $childQueries); 
  278.  
  279. $out = array(); 
  280. $queries = $env->block->queries; 
  281. if (is_null($childQueries)) { 
  282. $out = $queries; 
  283. } else { 
  284. foreach ($queries as $parent) { 
  285. foreach ($childQueries as $child) { 
  286. $out[] = array_merge($parent, $child); 
  287.  
  288. return $this->multiplyMedia($env->parent, $out); 
  289.  
  290. protected function expandParentSelectors(&$tag, $replace) { 
  291. $parts = explode("$&$", $tag); 
  292. $count = 0; 
  293. foreach ($parts as &$part) { 
  294. $part = str_replace($this->parentSelector, $replace, $part, $c); 
  295. $count += $c; 
  296. $tag = implode($this->parentSelector, $parts); 
  297. return $count; 
  298.  
  299. protected function findClosestSelectors() { 
  300. $env = $this->env; 
  301. $selectors = null; 
  302. while ($env !== null) { 
  303. if (isset($env->selectors)) { 
  304. $selectors = $env->selectors; 
  305. break; 
  306. $env = $env->parent; 
  307.  
  308. return $selectors; 
  309.  
  310.  
  311. // multiply $selectors against the nearest selectors in env 
  312. protected function multiplySelectors($selectors) { 
  313. // find parent selectors 
  314.  
  315. $parentSelectors = $this->findClosestSelectors(); 
  316. if (is_null($parentSelectors)) { 
  317. // kill parent reference in top level selector 
  318. foreach ($selectors as &$s) { 
  319. $this->expandParentSelectors($s, ""); 
  320.  
  321. return $selectors; 
  322.  
  323. $out = array(); 
  324. foreach ($parentSelectors as $parent) { 
  325. foreach ($selectors as $child) { 
  326. $count = $this->expandParentSelectors($child, $parent); 
  327.  
  328. // don't prepend the parent tag if & was used 
  329. if ($count > 0) { 
  330. $out[] = trim($child); 
  331. } else { 
  332. $out[] = trim($parent . ' ' . $child); 
  333.  
  334. return $out; 
  335.  
  336. // reduces selector expressions 
  337. protected function compileSelectors($selectors) { 
  338. $out = array(); 
  339.  
  340. foreach ($selectors as $s) { 
  341. if (is_array($s)) { 
  342. list(, $value) = $s; 
  343. $out[] = trim($this->compileValue($this->reduce($value))); 
  344. } else { 
  345. $out[] = $s; 
  346.  
  347. return $out; 
  348.  
  349. protected function eq($left, $right) { 
  350. return $left == $right; 
  351.  
  352. protected function patternMatch($block, $orderedArgs, $keywordArgs) { 
  353. // match the guards if it has them 
  354. // any one of the groups must have all its guards pass for a match 
  355. if (!empty($block->guards)) { 
  356. $groupPassed = false; 
  357. foreach ($block->guards as $guardGroup) { 
  358. foreach ($guardGroup as $guard) { 
  359. $this->pushEnv(); 
  360. $this->zipSetArgs($block->args, $orderedArgs, $keywordArgs); 
  361.  
  362. $negate = false; 
  363. if ($guard[0] == "negate") { 
  364. $guard = $guard[1]; 
  365. $negate = true; 
  366.  
  367. $passed = $this->reduce($guard) == self::$TRUE; 
  368. if ($negate) $passed = !$passed; 
  369.  
  370. $this->popEnv(); 
  371.  
  372. if ($passed) { 
  373. $groupPassed = true; 
  374. } else { 
  375. $groupPassed = false; 
  376. break; 
  377.  
  378. if ($groupPassed) break; 
  379.  
  380. if (!$groupPassed) { 
  381. return false; 
  382.  
  383. if (empty($block->args)) { 
  384. return $block->isVararg || empty($orderedArgs) && empty($keywordArgs); 
  385.  
  386. $remainingArgs = $block->args; 
  387. if ($keywordArgs) { 
  388. $remainingArgs = array(); 
  389. foreach ($block->args as $arg) { 
  390. if ($arg[0] == "arg" && isset($keywordArgs[$arg[1]])) { 
  391. continue; 
  392.  
  393. $remainingArgs[] = $arg; 
  394.  
  395. $i = -1; // no args 
  396. // try to match by arity or by argument literal 
  397. foreach ($remainingArgs as $i => $arg) { 
  398. switch ($arg[0]) { 
  399. case "lit": 
  400. if (empty($orderedArgs[$i]) || !$this->eq($arg[1], $orderedArgs[$i])) { 
  401. return false; 
  402. break; 
  403. case "arg": 
  404. // no arg and no default value 
  405. if (!isset($orderedArgs[$i]) && !isset($arg[2])) { 
  406. return false; 
  407. break; 
  408. case "rest": 
  409. $i--; // rest can be empty 
  410. break 2; 
  411.  
  412. if ($block->isVararg) { 
  413. return true; // not having enough is handled above 
  414. } else { 
  415. $numMatched = $i + 1; 
  416. // greater than becuase default values always match 
  417. return $numMatched >= count($orderedArgs); 
  418.  
  419. protected function patternMatchAll($blocks, $orderedArgs, $keywordArgs, $skip=array()) { 
  420. $matches = null; 
  421. foreach ($blocks as $block) { 
  422. // skip seen blocks that don't have arguments 
  423. if (isset($skip[$block->id]) && !isset($block->args)) { 
  424. continue; 
  425.  
  426. if ($this->patternMatch($block, $orderedArgs, $keywordArgs)) { 
  427. $matches[] = $block; 
  428.  
  429. return $matches; 
  430.  
  431. // attempt to find blocks matched by path and args 
  432. protected function findBlocks($searchIn, $path, $orderedArgs, $keywordArgs, $seen=array()) { 
  433. if ($searchIn == null) return null; 
  434. if (isset($seen[$searchIn->id])) return null; 
  435. $seen[$searchIn->id] = true; 
  436.  
  437. $name = $path[0]; 
  438.  
  439. if (isset($searchIn->children[$name])) { 
  440. $blocks = $searchIn->children[$name]; 
  441. if (count($path) == 1) { 
  442. $matches = $this->patternMatchAll($blocks, $orderedArgs, $keywordArgs, $seen); 
  443. if (!empty($matches)) { 
  444. // This will return all blocks that match in the closest 
  445. // scope that has any matching block, like lessjs 
  446. return $matches; 
  447. } else { 
  448. $matches = array(); 
  449. foreach ($blocks as $subBlock) { 
  450. $subMatches = $this->findBlocks($subBlock,  
  451. array_slice($path, 1), $orderedArgs, $keywordArgs, $seen); 
  452.  
  453. if (!is_null($subMatches)) { 
  454. foreach ($subMatches as $sm) { 
  455. $matches[] = $sm; 
  456.  
  457. return count($matches) > 0 ? $matches : null; 
  458. if ($searchIn->parent === $searchIn) return null; 
  459. return $this->findBlocks($searchIn->parent, $path, $orderedArgs, $keywordArgs, $seen); 
  460.  
  461. // sets all argument names in $args to either the default value 
  462. // or the one passed in through $values 
  463. protected function zipSetArgs($args, $orderedValues, $keywordValues) { 
  464. $assignedValues = array(); 
  465.  
  466. $i = 0; 
  467. foreach ($args as $a) { 
  468. if ($a[0] == "arg") { 
  469. if (isset($keywordValues[$a[1]])) { 
  470. // has keyword arg 
  471. $value = $keywordValues[$a[1]]; 
  472. } elseif (isset($orderedValues[$i])) { 
  473. // has ordered arg 
  474. $value = $orderedValues[$i]; 
  475. $i++; 
  476. } elseif (isset($a[2])) { 
  477. // has default value 
  478. $value = $a[2]; 
  479. } else { 
  480. $this->throwError("Failed to assign arg " . $a[1]); 
  481. $value = null; // :( 
  482.  
  483. $value = $this->reduce($value); 
  484. $this->set($a[1], $value); 
  485. $assignedValues[] = $value; 
  486. } else { 
  487. // a lit 
  488. $i++; 
  489.  
  490. // check for a rest 
  491. $last = end($args); 
  492. if ($last[0] == "rest") { 
  493. $rest = array_slice($orderedValues, count($args) - 1); 
  494. $this->set($last[1], $this->reduce(array("list", " ", $rest))); 
  495.  
  496. // wow is this the only true use of PHP's + operator for arrays? 
  497. $this->env->arguments = $assignedValues + $orderedValues; 
  498.  
  499. // compile a prop and update $lines or $blocks appropriately 
  500. protected function compileProp($prop, $block, $out) { 
  501. // set error position context 
  502. $this->sourceLoc = isset($prop[-1]) ? $prop[-1] : -1; 
  503.  
  504. switch ($prop[0]) { 
  505. case 'assign': 
  506. list(, $name, $value) = $prop; 
  507. if ($name[0] == $this->vPrefix) { 
  508. $this->set($name, $value); 
  509. } else { 
  510. $out->lines[] = $this->formatter->property($name,  
  511. $this->compileValue($this->reduce($value))); 
  512. break; 
  513. case 'block': 
  514. list(, $child) = $prop; 
  515. $this->compileBlock($child); 
  516. break; 
  517. case 'mixin': 
  518. list(, $path, $args, $suffix) = $prop; 
  519.  
  520. $orderedArgs = array(); 
  521. $keywordArgs = array(); 
  522. foreach ((array)$args as $arg) { 
  523. $argval = null; 
  524. switch ($arg[0]) { 
  525. case "arg": 
  526. if (!isset($arg[2])) { 
  527. $orderedArgs[] = $this->reduce(array("variable", $arg[1])); 
  528. } else { 
  529. $keywordArgs[$arg[1]] = $this->reduce($arg[2]); 
  530. break; 
  531.  
  532. case "lit": 
  533. $orderedArgs[] = $this->reduce($arg[1]); 
  534. break; 
  535. default: 
  536. $this->throwError("Unknown arg type: " . $arg[0]); 
  537.  
  538. $mixins = $this->findBlocks($block, $path, $orderedArgs, $keywordArgs); 
  539.  
  540. if ($mixins === null) { 
  541. // fwrite(STDERR, "failed to find block: ".implode(" > ", $path)."\n"); 
  542. break; // throw error here?? 
  543.  
  544. foreach ($mixins as $mixin) { 
  545. if ($mixin === $block && !$orderedArgs) { 
  546. continue; 
  547.  
  548. $haveScope = false; 
  549. if (isset($mixin->parent->scope)) { 
  550. $haveScope = true; 
  551. $mixinParentEnv = $this->pushEnv(); 
  552. $mixinParentEnv->storeParent = $mixin->parent->scope; 
  553.  
  554. $haveArgs = false; 
  555. if (isset($mixin->args)) { 
  556. $haveArgs = true; 
  557. $this->pushEnv(); 
  558. $this->zipSetArgs($mixin->args, $orderedArgs, $keywordArgs); 
  559.  
  560. $oldParent = $mixin->parent; 
  561. if ($mixin != $block) $mixin->parent = $block; 
  562.  
  563. foreach ($this->sortProps($mixin->props) as $subProp) { 
  564. if ($suffix !== null && 
  565. $subProp[0] == "assign" && 
  566. is_string($subProp[1]) && 
  567. $subProp[1]{0} != $this->vPrefix) 
  568. $subProp[2] = array( 
  569. 'list', ' ',  
  570. array($subProp[2], array('keyword', $suffix)) 
  571. ); 
  572.  
  573. $this->compileProp($subProp, $mixin, $out); 
  574.  
  575. $mixin->parent = $oldParent; 
  576.  
  577. if ($haveArgs) $this->popEnv(); 
  578. if ($haveScope) $this->popEnv(); 
  579.  
  580. break; 
  581. case 'raw': 
  582. $out->lines[] = $prop[1]; 
  583. break; 
  584. case "directive": 
  585. list(, $name, $value) = $prop; 
  586. $out->lines[] = "@$name " . $this->compileValue($this->reduce($value)).';'; 
  587. break; 
  588. case "comment": 
  589. $out->lines[] = $prop[1]; 
  590. break; 
  591. case "import"; 
  592. list(, $importPath, $importId) = $prop; 
  593. $importPath = $this->reduce($importPath); 
  594.  
  595. if (!isset($this->env->imports)) { 
  596. $this->env->imports = array(); 
  597.  
  598. $result = $this->tryImport($importPath, $block, $out); 
  599.  
  600. $this->env->imports[$importId] = $result === false ? 
  601. array(false, "@import " . $this->compileValue($importPath).";") : 
  602. $result; 
  603.  
  604. break; 
  605. case "import_mixin": 
  606. list(, $importId) = $prop; 
  607. $import = $this->env->imports[$importId]; 
  608. if ($import[0] === false) { 
  609. if (isset($import[1])) { 
  610. $out->lines[] = $import[1]; 
  611. } else { 
  612. list(, $bottom, $parser, $importDir) = $import; 
  613. $this->compileImportedProps($bottom, $block, $out, $parser, $importDir); 
  614.  
  615. break; 
  616. default: 
  617. $this->throwError("unknown op: {$prop[0]}\n"); 
  618.  
  619.  
  620. /** 
  621. * Compiles a primitive value into a CSS property value. 
  622. * Values in lessphp are typed by being wrapped in arrays, their format is 
  623. * typically: 
  624. * array(type, contents [, additional_contents]*) 
  625. * The input is expected to be reduced. This function will not work on 
  626. * things like expressions and variables. 
  627. */ 
  628. protected function compileValue($value) { 
  629. switch ($value[0]) { 
  630. case 'list': 
  631. // [1] - delimiter 
  632. // [2] - array of values 
  633. return implode($value[1], array_map(array($this, 'compileValue'), $value[2])); 
  634. case 'raw_color': 
  635. if (!empty($this->formatter->compressColors)) { 
  636. return $this->compileValue($this->coerceColor($value)); 
  637. return $value[1]; 
  638. case 'keyword': 
  639. // [1] - the keyword 
  640. return $value[1]; 
  641. case 'number': 
  642. list(, $num, $unit) = $value; 
  643. // [1] - the number 
  644. // [2] - the unit 
  645. if ($this->numberPrecision !== null) { 
  646. $num = round($num, $this->numberPrecision); 
  647. return $num . $unit; 
  648. case 'string': 
  649. // [1] - contents of string (includes quotes) 
  650. list(, $delim, $content) = $value; 
  651. foreach ($content as &$part) { 
  652. if (is_array($part)) { 
  653. $part = $this->compileValue($part); 
  654. return $delim . implode($content) . $delim; 
  655. case 'color': 
  656. // [1] - red component (either number or a %) 
  657. // [2] - green component 
  658. // [3] - blue component 
  659. // [4] - optional alpha component 
  660. list(, $r, $g, $b) = $value; 
  661. $r = round($r); 
  662. $g = round($g); 
  663. $b = round($b); 
  664.  
  665. if (count($value) == 5 && $value[4] != 1) { // rgba 
  666. return 'rgba('.$r.', '.$g.', '.$b.', '.$value[4].')'; 
  667.  
  668. $h = sprintf("#%02x%02x%02x", $r, $g, $b); 
  669.  
  670. if (!empty($this->formatter->compressColors)) { 
  671. // Converting hex color to short notation (e.g. #003399 to #039) 
  672. if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) { 
  673. $h = '#' . $h[1] . $h[3] . $h[5]; 
  674.  
  675. return $h; 
  676.  
  677. case 'function': 
  678. list(, $name, $args) = $value; 
  679. return $name.'('.$this->compileValue($args).')'; 
  680. default: // assumed to be unit 
  681. $this->throwError("unknown value type: $value[0]"); 
  682.  
  683. protected function lib_pow($args) { 
  684. list($base, $exp) = $this->assertArgs($args, 2, "pow"); 
  685. return pow($this->assertNumber($base), $this->assertNumber($exp)); 
  686.  
  687. protected function lib_pi() { 
  688. return pi(); 
  689.  
  690. protected function lib_mod($args) { 
  691. list($a, $b) = $this->assertArgs($args, 2, "mod"); 
  692. return $this->assertNumber($a) % $this->assertNumber($b); 
  693.  
  694. protected function lib_tan($num) { 
  695. return tan($this->assertNumber($num)); 
  696.  
  697. protected function lib_sin($num) { 
  698. return sin($this->assertNumber($num)); 
  699.  
  700. protected function lib_cos($num) { 
  701. return cos($this->assertNumber($num)); 
  702.  
  703. protected function lib_atan($num) { 
  704. $num = atan($this->assertNumber($num)); 
  705. return array("number", $num, "rad"); 
  706.  
  707. protected function lib_asin($num) { 
  708. $num = asin($this->assertNumber($num)); 
  709. return array("number", $num, "rad"); 
  710.  
  711. protected function lib_acos($num) { 
  712. $num = acos($this->assertNumber($num)); 
  713. return array("number", $num, "rad"); 
  714.  
  715. protected function lib_sqrt($num) { 
  716. return sqrt($this->assertNumber($num)); 
  717.  
  718. protected function lib_extract($value) { 
  719. list($list, $idx) = $this->assertArgs($value, 2, "extract"); 
  720. $idx = $this->assertNumber($idx); 
  721. // 1 indexed 
  722. if ($list[0] == "list" && isset($list[2][$idx - 1])) { 
  723. return $list[2][$idx - 1]; 
  724.  
  725. protected function lib_isnumber($value) { 
  726. return $this->toBool($value[0] == "number"); 
  727.  
  728. protected function lib_isstring($value) { 
  729. return $this->toBool($value[0] == "string"); 
  730.  
  731. protected function lib_iscolor($value) { 
  732. return $this->toBool($this->coerceColor($value)); 
  733.  
  734. protected function lib_iskeyword($value) { 
  735. return $this->toBool($value[0] == "keyword"); 
  736.  
  737. protected function lib_ispixel($value) { 
  738. return $this->toBool($value[0] == "number" && $value[2] == "px"); 
  739.  
  740. protected function lib_ispercentage($value) { 
  741. return $this->toBool($value[0] == "number" && $value[2] == "%"); 
  742.  
  743. protected function lib_isem($value) { 
  744. return $this->toBool($value[0] == "number" && $value[2] == "em"); 
  745.  
  746. protected function lib_isrem($value) { 
  747. return $this->toBool($value[0] == "number" && $value[2] == "rem"); 
  748.  
  749. protected function lib_rgbahex($color) { 
  750. $color = $this->coerceColor($color); 
  751. if (is_null($color)) 
  752. $this->throwError("color expected for rgbahex"); 
  753.  
  754. return sprintf("#%02x%02x%02x%02x",  
  755. isset($color[4]) ? $color[4]*255 : 255,  
  756. $color[1], $color[2], $color[3]); 
  757.  
  758. protected function lib_argb($color) { 
  759. return $this->lib_rgbahex($color); 
  760.  
  761. // utility func to unquote a string 
  762. protected function lib_e($arg) { 
  763. switch ($arg[0]) { 
  764. case "list": 
  765. $items = $arg[2]; 
  766. if (isset($items[0])) { 
  767. return $this->lib_e($items[0]); 
  768. return self::$defaultValue; 
  769. case "string": 
  770. $arg[1] = ""; 
  771. return $arg; 
  772. case "keyword": 
  773. return $arg; 
  774. default: 
  775. return array("keyword", $this->compileValue($arg)); 
  776.  
  777. protected function lib__sprintf($args) { 
  778. if ($args[0] != "list") return $args; 
  779. $values = $args[2]; 
  780. $string = array_shift($values); 
  781. $template = $this->compileValue($this->lib_e($string)); 
  782.  
  783. $i = 0; 
  784. if (preg_match_all('/%[dsa]/', $template, $m)) { 
  785. foreach ($m[0] as $match) { 
  786. $val = isset($values[$i]) ? 
  787. $this->reduce($values[$i]) : array('keyword', ''); 
  788.  
  789. // lessjs compat, renders fully expanded color, not raw color 
  790. if ($color = $this->coerceColor($val)) { 
  791. $val = $color; 
  792.  
  793. $i++; 
  794. $rep = $this->compileValue($this->lib_e($val)); 
  795. $template = preg_replace('/'.self::preg_quote($match).'/',  
  796. $rep, $template, 1); 
  797.  
  798. $d = $string[0] == "string" ? $string[1] : '"'; 
  799. return array("string", $d, array($template)); 
  800.  
  801. protected function lib_floor($arg) { 
  802. $value = $this->assertNumber($arg); 
  803. return array("number", floor($value), $arg[2]); 
  804.  
  805. protected function lib_ceil($arg) { 
  806. $value = $this->assertNumber($arg); 
  807. return array("number", ceil($value), $arg[2]); 
  808.  
  809. protected function lib_round($arg) { 
  810. $value = $this->assertNumber($arg); 
  811. return array("number", round($value), $arg[2]); 
  812.  
  813. protected function lib_unit($arg) { 
  814. if ($arg[0] == "list") { 
  815. list($number, $newUnit) = $arg[2]; 
  816. return array("number", $this->assertNumber($number),  
  817. $this->compileValue($this->lib_e($newUnit))); 
  818. } else { 
  819. return array("number", $this->assertNumber($arg), ""); 
  820.  
  821. /** 
  822. * Helper function to get arguments for color manipulation functions. 
  823. * takes a list that contains a color like thing and a percentage 
  824. */ 
  825. protected function colorArgs($args) { 
  826. if ($args[0] != 'list' || count($args[2]) < 2) { 
  827. return array(array('color', 0, 0, 0), 0); 
  828. list($color, $delta) = $args[2]; 
  829. $color = $this->assertColor($color); 
  830. $delta = floatval($delta[1]); 
  831.  
  832. return array($color, $delta); 
  833.  
  834. protected function lib_darken($args) { 
  835. list($color, $delta) = $this->colorArgs($args); 
  836.  
  837. $hsl = $this->toHSL($color); 
  838. $hsl[3] = $this->clamp($hsl[3] - $delta, 100); 
  839. return $this->toRGB($hsl); 
  840.  
  841. protected function lib_lighten($args) { 
  842. list($color, $delta) = $this->colorArgs($args); 
  843.  
  844. $hsl = $this->toHSL($color); 
  845. $hsl[3] = $this->clamp($hsl[3] + $delta, 100); 
  846. return $this->toRGB($hsl); 
  847.  
  848. protected function lib_saturate($args) { 
  849. list($color, $delta) = $this->colorArgs($args); 
  850.  
  851. $hsl = $this->toHSL($color); 
  852. $hsl[2] = $this->clamp($hsl[2] + $delta, 100); 
  853. return $this->toRGB($hsl); 
  854.  
  855. protected function lib_desaturate($args) { 
  856. list($color, $delta) = $this->colorArgs($args); 
  857.  
  858. $hsl = $this->toHSL($color); 
  859. $hsl[2] = $this->clamp($hsl[2] - $delta, 100); 
  860. return $this->toRGB($hsl); 
  861.  
  862. protected function lib_spin($args) { 
  863. list($color, $delta) = $this->colorArgs($args); 
  864.  
  865. $hsl = $this->toHSL($color); 
  866.  
  867. $hsl[1] = $hsl[1] + $delta % 360; 
  868. if ($hsl[1] < 0) $hsl[1] += 360; 
  869.  
  870. return $this->toRGB($hsl); 
  871.  
  872. protected function lib_fadeout($args) { 
  873. list($color, $delta) = $this->colorArgs($args); 
  874. $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) - $delta/100); 
  875. return $color; 
  876.  
  877. protected function lib_fadein($args) { 
  878. list($color, $delta) = $this->colorArgs($args); 
  879. $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) + $delta/100); 
  880. return $color; 
  881.  
  882. protected function lib_hue($color) { 
  883. $hsl = $this->toHSL($this->assertColor($color)); 
  884. return round($hsl[1]); 
  885.  
  886. protected function lib_saturation($color) { 
  887. $hsl = $this->toHSL($this->assertColor($color)); 
  888. return round($hsl[2]); 
  889.  
  890. protected function lib_lightness($color) { 
  891. $hsl = $this->toHSL($this->assertColor($color)); 
  892. return round($hsl[3]); 
  893.  
  894. // get the alpha of a color 
  895. // defaults to 1 for non-colors or colors without an alpha 
  896. protected function lib_alpha($value) { 
  897. if (!is_null($color = $this->coerceColor($value))) { 
  898. return isset($color[4]) ? $color[4] : 1; 
  899.  
  900. // set the alpha of the color 
  901. protected function lib_fade($args) { 
  902. list($color, $alpha) = $this->colorArgs($args); 
  903. $color[4] = $this->clamp($alpha / 100.0); 
  904. return $color; 
  905.  
  906. protected function lib_percentage($arg) { 
  907. $num = $this->assertNumber($arg); 
  908. return array("number", $num*100, "%"); 
  909.  
  910. // mixes two colors by weight 
  911. // mix(@color1, @color2, [@weight: 50%]); 
  912. // http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#mix-instance_method 
  913. protected function lib_mix($args) { 
  914. if ($args[0] != "list" || count($args[2]) < 2) 
  915. $this->throwError("mix expects (color1, color2, weight)"); 
  916.  
  917. list($first, $second) = $args[2]; 
  918. $first = $this->assertColor($first); 
  919. $second = $this->assertColor($second); 
  920.  
  921. $first_a = $this->lib_alpha($first); 
  922. $second_a = $this->lib_alpha($second); 
  923.  
  924. if (isset($args[2][2])) { 
  925. $weight = $args[2][2][1] / 100.0; 
  926. } else { 
  927. $weight = 0.5; 
  928.  
  929. $w = $weight * 2 - 1; 
  930. $a = $first_a - $second_a; 
  931.  
  932. $w1 = (($w * $a == -1 ? $w : ($w + $a)/(1 + $w * $a)) + 1) / 2.0; 
  933. $w2 = 1.0 - $w1; 
  934.  
  935. $new = array('color',  
  936. $w1 * $first[1] + $w2 * $second[1],  
  937. $w1 * $first[2] + $w2 * $second[2],  
  938. $w1 * $first[3] + $w2 * $second[3],  
  939. ); 
  940.  
  941. if ($first_a != 1.0 || $second_a != 1.0) { 
  942. $new[] = $first_a * $weight + $second_a * ($weight - 1); 
  943.  
  944. return $this->fixColor($new); 
  945.  
  946. protected function lib_contrast($args) { 
  947. if ($args[0] != 'list' || count($args[2]) < 3) { 
  948. return array(array('color', 0, 0, 0), 0); 
  949.  
  950. list($inputColor, $darkColor, $lightColor) = $args[2]; 
  951.  
  952. $inputColor = $this->assertColor($inputColor); 
  953. $darkColor = $this->assertColor($darkColor); 
  954. $lightColor = $this->assertColor($lightColor); 
  955. $hsl = $this->toHSL($inputColor); 
  956.  
  957. if ($hsl[3] > 50) { 
  958. return $darkColor; 
  959.  
  960. return $lightColor; 
  961.  
  962. protected function assertColor($value, $error = "expected color value") { 
  963. $color = $this->coerceColor($value); 
  964. if (is_null($color)) $this->throwError($error); 
  965. return $color; 
  966.  
  967. protected function assertNumber($value, $error = "expecting number") { 
  968. if ($value[0] == "number") return $value[1]; 
  969. $this->throwError($error); 
  970.  
  971. protected function assertArgs($value, $expectedArgs, $name="") { 
  972. if ($expectedArgs == 1) { 
  973. return $value; 
  974. } else { 
  975. if ($value[0] !== "list" || $value[1] != ", ") $this->throwError("expecting list"); 
  976. $values = $value[2]; 
  977. $numValues = count($values); 
  978. if ($expectedArgs != $numValues) { 
  979. if ($name) { 
  980. $name = $name . ": "; 
  981.  
  982. $this->throwError("${name}expecting $expectedArgs arguments, got $numValues"); 
  983.  
  984. return $values; 
  985.  
  986. protected function toHSL($color) { 
  987. if ($color[0] == 'hsl') return $color; 
  988.  
  989. $r = $color[1] / 255; 
  990. $g = $color[2] / 255; 
  991. $b = $color[3] / 255; 
  992.  
  993. $min = min($r, $g, $b); 
  994. $max = max($r, $g, $b); 
  995.  
  996. $L = ($min + $max) / 2; 
  997. if ($min == $max) { 
  998. $S = $H = 0; 
  999. } else { 
  1000. if ($L < 0.5) 
  1001. $S = ($max - $min)/($max + $min); 
  1002. else 
  1003. $S = ($max - $min)/(2.0 - $max - $min); 
  1004.  
  1005. if ($r == $max) $H = ($g - $b)/($max - $min); 
  1006. elseif ($g == $max) $H = 2.0 + ($b - $r)/($max - $min); 
  1007. elseif ($b == $max) $H = 4.0 + ($r - $g)/($max - $min); 
  1008.  
  1009.  
  1010. $out = array('hsl',  
  1011. ($H < 0 ? $H + 6 : $H)*60,  
  1012. $S*100,  
  1013. $L*100,  
  1014. ); 
  1015.  
  1016. if (count($color) > 4) $out[] = $color[4]; // copy alpha 
  1017. return $out; 
  1018.  
  1019. protected function toRGB_helper($comp, $temp1, $temp2) { 
  1020. if ($comp < 0) $comp += 1.0; 
  1021. elseif ($comp > 1) $comp -= 1.0; 
  1022.  
  1023. if (6 * $comp < 1) return $temp1 + ($temp2 - $temp1) * 6 * $comp; 
  1024. if (2 * $comp < 1) return $temp2; 
  1025. if (3 * $comp < 2) return $temp1 + ($temp2 - $temp1)*((2/3) - $comp) * 6; 
  1026.  
  1027. return $temp1; 
  1028.  
  1029. /** 
  1030. * Converts a hsl array into a color value in rgb. 
  1031. * Expects H to be in range of 0 to 360, S and L in 0 to 100 
  1032. */ 
  1033. protected function toRGB($color) { 
  1034. if ($color[0] == 'color') return $color; 
  1035.  
  1036. $H = $color[1] / 360; 
  1037. $S = $color[2] / 100; 
  1038. $L = $color[3] / 100; 
  1039.  
  1040. if ($S == 0) { 
  1041. $r = $g = $b = $L; 
  1042. } else { 
  1043. $temp2 = $L < 0.5 ? 
  1044. $L*(1.0 + $S) : 
  1045. $L + $S - $L * $S; 
  1046.  
  1047. $temp1 = 2.0 * $L - $temp2; 
  1048.  
  1049. $r = $this->toRGB_helper($H + 1/3, $temp1, $temp2); 
  1050. $g = $this->toRGB_helper($H, $temp1, $temp2); 
  1051. $b = $this->toRGB_helper($H - 1/3, $temp1, $temp2); 
  1052.  
  1053. // $out = array('color', round($r*255), round($g*255), round($b*255)); 
  1054. $out = array('color', $r*255, $g*255, $b*255); 
  1055. if (count($color) > 4) $out[] = $color[4]; // copy alpha 
  1056. return $out; 
  1057.  
  1058. protected function clamp($v, $max = 1, $min = 0) { 
  1059. return min($max, max($min, $v)); 
  1060.  
  1061. /** 
  1062. * Convert the rgb, rgba, hsl color literals of function type 
  1063. * as returned by the parser into values of color type. 
  1064. */ 
  1065. protected function funcToColor($func) { 
  1066. $fname = $func[1]; 
  1067. if ($func[2][0] != 'list') return false; // need a list of arguments 
  1068. $rawComponents = $func[2][2]; 
  1069.  
  1070. if ($fname == 'hsl' || $fname == 'hsla') { 
  1071. $hsl = array('hsl'); 
  1072. $i = 0; 
  1073. foreach ($rawComponents as $c) { 
  1074. $val = $this->reduce($c); 
  1075. $val = isset($val[1]) ? floatval($val[1]) : 0; 
  1076.  
  1077. if ($i == 0) $clamp = 360; 
  1078. elseif ($i < 3) $clamp = 100; 
  1079. else $clamp = 1; 
  1080.  
  1081. $hsl[] = $this->clamp($val, $clamp); 
  1082. $i++; 
  1083.  
  1084. while (count($hsl) < 4) $hsl[] = 0; 
  1085. return $this->toRGB($hsl); 
  1086.  
  1087. } elseif ($fname == 'rgb' || $fname == 'rgba') { 
  1088. $components = array(); 
  1089. $i = 1; 
  1090. foreach ($rawComponents as $c) { 
  1091. $c = $this->reduce($c); 
  1092. if ($i < 4) { 
  1093. if ($c[0] == "number" && $c[2] == "%") { 
  1094. $components[] = 255 * ($c[1] / 100); 
  1095. } else { 
  1096. $components[] = floatval($c[1]); 
  1097. } elseif ($i == 4) { 
  1098. if ($c[0] == "number" && $c[2] == "%") { 
  1099. $components[] = 1.0 * ($c[1] / 100); 
  1100. } else { 
  1101. $components[] = floatval($c[1]); 
  1102. } else break; 
  1103.  
  1104. $i++; 
  1105. while (count($components) < 3) $components[] = 0; 
  1106. array_unshift($components, 'color'); 
  1107. return $this->fixColor($components); 
  1108.  
  1109. return false; 
  1110.  
  1111. protected function reduce($value, $forExpression = false) { 
  1112. switch ($value[0]) { 
  1113. case "interpolate": 
  1114. $reduced = $this->reduce($value[1]); 
  1115. $var = $this->compileValue($reduced); 
  1116. $res = $this->reduce(array("variable", $this->vPrefix . $var)); 
  1117.  
  1118. if ($res[0] == "raw_color") { 
  1119. $res = $this->coerceColor($res); 
  1120.  
  1121. if (empty($value[2])) $res = $this->lib_e($res); 
  1122.  
  1123. return $res; 
  1124. case "variable": 
  1125. $key = $value[1]; 
  1126. if (is_array($key)) { 
  1127. $key = $this->reduce($key); 
  1128. $key = $this->vPrefix . $this->compileValue($this->lib_e($key)); 
  1129.  
  1130. $seen =& $this->env->seenNames; 
  1131.  
  1132. if (!empty($seen[$key])) { 
  1133. $this->throwError("infinite loop detected: $key"); 
  1134.  
  1135. $seen[$key] = true; 
  1136. $out = $this->reduce($this->get($key, self::$defaultValue)); 
  1137. $seen[$key] = false; 
  1138. return $out; 
  1139. case "list": 
  1140. foreach ($value[2] as &$item) { 
  1141. $item = $this->reduce($item, $forExpression); 
  1142. return $value; 
  1143. case "expression": 
  1144. return $this->evaluate($value); 
  1145. case "string": 
  1146. foreach ($value[2] as &$part) { 
  1147. if (is_array($part)) { 
  1148. $strip = $part[0] == "variable"; 
  1149. $part = $this->reduce($part); 
  1150. if ($strip) $part = $this->lib_e($part); 
  1151. return $value; 
  1152. case "escape": 
  1153. list(, $inner) = $value; 
  1154. return $this->lib_e($this->reduce($inner)); 
  1155. case "function": 
  1156. $color = $this->funcToColor($value); 
  1157. if ($color) return $color; 
  1158.  
  1159. list(, $name, $args) = $value; 
  1160. if ($name == "%") $name = "_sprintf"; 
  1161. $f = isset($this->libFunctions[$name]) ? 
  1162. $this->libFunctions[$name] : array($this, 'lib_'.$name); 
  1163.  
  1164. if (is_callable($f)) { 
  1165. if ($args[0] == 'list') 
  1166. $args = self::compressList($args[2], $args[1]); 
  1167.  
  1168. $ret = call_user_func($f, $this->reduce($args, true), $this); 
  1169.  
  1170. if (is_null($ret)) { 
  1171. return array("string", "", array( 
  1172. $name, "(", $args, ")" 
  1173. )); 
  1174.  
  1175. // convert to a typed value if the result is a php primitive 
  1176. if (is_numeric($ret)) $ret = array('number', $ret, ""); 
  1177. elseif (!is_array($ret)) $ret = array('keyword', $ret); 
  1178.  
  1179. return $ret; 
  1180.  
  1181. // plain function, reduce args 
  1182. $value[2] = $this->reduce($value[2]); 
  1183. return $value; 
  1184. case "unary": 
  1185. list(, $op, $exp) = $value; 
  1186. $exp = $this->reduce($exp); 
  1187.  
  1188. if ($exp[0] == "number") { 
  1189. switch ($op) { 
  1190. case "+": 
  1191. return $exp; 
  1192. case "-": 
  1193. $exp[1] *= -1; 
  1194. return $exp; 
  1195. return array("string", "", array($op, $exp)); 
  1196.  
  1197. if ($forExpression) { 
  1198. switch ($value[0]) { 
  1199. case "keyword": 
  1200. if ($color = $this->coerceColor($value)) { 
  1201. return $color; 
  1202. break; 
  1203. case "raw_color": 
  1204. return $this->coerceColor($value); 
  1205.  
  1206. return $value; 
  1207.  
  1208.  
  1209. // coerce a value for use in color operation 
  1210. protected function coerceColor($value) { 
  1211. switch($value[0]) { 
  1212. case 'color': return $value; 
  1213. case 'raw_color': 
  1214. $c = array("color", 0, 0, 0); 
  1215. $colorStr = substr($value[1], 1); 
  1216. $num = hexdec($colorStr); 
  1217. $width = strlen($colorStr) == 3 ? 16 : 256; 
  1218.  
  1219. for ($i = 3; $i > 0; $i--) { // 3 2 1 
  1220. $t = $num % $width; 
  1221. $num /= $width; 
  1222.  
  1223. $c[$i] = $t * (256/$width) + $t * floor(16/$width); 
  1224.  
  1225. return $c; 
  1226. case 'keyword': 
  1227. $name = $value[1]; 
  1228. if (isset(self::$cssColors[$name])) { 
  1229. $rgba = explode(', ', self::$cssColors[$name]); 
  1230.  
  1231. if(isset($rgba[3])) 
  1232. return array('color', $rgba[0], $rgba[1], $rgba[2], $rgba[3]); 
  1233.  
  1234. return array('color', $rgba[0], $rgba[1], $rgba[2]); 
  1235. return null; 
  1236.  
  1237. // make something string like into a string 
  1238. protected function coerceString($value) { 
  1239. switch ($value[0]) { 
  1240. case "string": 
  1241. return $value; 
  1242. case "keyword": 
  1243. return array("string", "", array($value[1])); 
  1244. return null; 
  1245.  
  1246. // turn list of length 1 into value type 
  1247. protected function flattenList($value) { 
  1248. if ($value[0] == "list" && count($value[2]) == 1) { 
  1249. return $this->flattenList($value[2][0]); 
  1250. return $value; 
  1251.  
  1252. protected function toBool($a) { 
  1253. if ($a) return self::$TRUE; 
  1254. else return self::$FALSE; 
  1255.  
  1256. // evaluate an expression 
  1257. protected function evaluate($exp) { 
  1258. list(, $op, $left, $right, $whiteBefore, $whiteAfter) = $exp; 
  1259.  
  1260. $left = $this->reduce($left, true); 
  1261. $right = $this->reduce($right, true); 
  1262.  
  1263. if ($leftColor = $this->coerceColor($left)) { 
  1264. $left = $leftColor; 
  1265.  
  1266. if ($rightColor = $this->coerceColor($right)) { 
  1267. $right = $rightColor; 
  1268.  
  1269. $ltype = $left[0]; 
  1270. $rtype = $right[0]; 
  1271.  
  1272. // operators that work on all types 
  1273. if ($op == "and") { 
  1274. return $this->toBool($left == self::$TRUE && $right == self::$TRUE); 
  1275.  
  1276. if ($op == "=") { 
  1277. return $this->toBool($this->eq($left, $right) ); 
  1278.  
  1279. if ($op == "+" && !is_null($str = $this->stringConcatenate($left, $right))) { 
  1280. return $str; 
  1281.  
  1282. // type based operators 
  1283. $fname = "op_${ltype}_${rtype}"; 
  1284. if (is_callable(array($this, $fname))) { 
  1285. $out = $this->$fname($op, $left, $right); 
  1286. if (!is_null($out)) return $out; 
  1287.  
  1288. // make the expression look it did before being parsed 
  1289. $paddedOp = $op; 
  1290. if ($whiteBefore) $paddedOp = " " . $paddedOp; 
  1291. if ($whiteAfter) $paddedOp .= " "; 
  1292.  
  1293. return array("string", "", array($left, $paddedOp, $right)); 
  1294.  
  1295. protected function stringConcatenate($left, $right) { 
  1296. if ($strLeft = $this->coerceString($left)) { 
  1297. if ($right[0] == "string") { 
  1298. $right[1] = ""; 
  1299. $strLeft[2][] = $right; 
  1300. return $strLeft; 
  1301.  
  1302. if ($strRight = $this->coerceString($right)) { 
  1303. array_unshift($strRight[2], $left); 
  1304. return $strRight; 
  1305.  
  1306.  
  1307. // make sure a color's components don't go out of bounds 
  1308. protected function fixColor($c) { 
  1309. foreach (range(1, 3) as $i) { 
  1310. if ($c[$i] < 0) $c[$i] = 0; 
  1311. if ($c[$i] > 255) $c[$i] = 255; 
  1312.  
  1313. return $c; 
  1314.  
  1315. protected function op_number_color($op, $lft, $rgt) { 
  1316. if ($op == '+' || $op == '*') { 
  1317. return $this->op_color_number($op, $rgt, $lft); 
  1318.  
  1319. protected function op_color_number($op, $lft, $rgt) { 
  1320. if ($rgt[0] == '%') $rgt[1] /= 100; 
  1321.  
  1322. return $this->op_color_color($op, $lft,  
  1323. array_fill(1, count($lft) - 1, $rgt[1])); 
  1324.  
  1325. protected function op_color_color($op, $left, $right) { 
  1326. $out = array('color'); 
  1327. $max = count($left) > count($right) ? count($left) : count($right); 
  1328. foreach (range(1, $max - 1) as $i) { 
  1329. $lval = isset($left[$i]) ? $left[$i] : 0; 
  1330. $rval = isset($right[$i]) ? $right[$i] : 0; 
  1331. switch ($op) { 
  1332. case '+': 
  1333. $out[] = $lval + $rval; 
  1334. break; 
  1335. case '-': 
  1336. $out[] = $lval - $rval; 
  1337. break; 
  1338. case '*': 
  1339. $out[] = $lval * $rval; 
  1340. break; 
  1341. case '%': 
  1342. $out[] = $lval % $rval; 
  1343. break; 
  1344. case '/': 
  1345. if ($rval == 0) $this->throwError("evaluate error: can't divide by zero"); 
  1346. $out[] = $lval / $rval; 
  1347. break; 
  1348. default: 
  1349. $this->throwError('evaluate error: color op number failed on op '.$op); 
  1350. return $this->fixColor($out); 
  1351.  
  1352. function lib_red($color) { 
  1353. $color = $this->coerceColor($color); 
  1354. if (is_null($color)) { 
  1355. $this->throwError('color expected for red()'); 
  1356.  
  1357. return $color[1]; 
  1358.  
  1359. function lib_green($color) { 
  1360. $color = $this->coerceColor($color); 
  1361. if (is_null($color)) { 
  1362. $this->throwError('color expected for green()'); 
  1363.  
  1364. return $color[2]; 
  1365.  
  1366. function lib_blue($color) { 
  1367. $color = $this->coerceColor($color); 
  1368. if (is_null($color)) { 
  1369. $this->throwError('color expected for blue()'); 
  1370.  
  1371. return $color[3]; 
  1372.  
  1373.  
  1374. // operator on two numbers 
  1375. protected function op_number_number($op, $left, $right) { 
  1376. $unit = empty($left[2]) ? $right[2] : $left[2]; 
  1377.  
  1378. $value = 0; 
  1379. switch ($op) { 
  1380. case '+': 
  1381. $value = $left[1] + $right[1]; 
  1382. break; 
  1383. case '*': 
  1384. $value = $left[1] * $right[1]; 
  1385. break; 
  1386. case '-': 
  1387. $value = $left[1] - $right[1]; 
  1388. break; 
  1389. case '%': 
  1390. $value = $left[1] % $right[1]; 
  1391. break; 
  1392. case '/': 
  1393. if ($right[1] == 0) $this->throwError('parse error: divide by zero'); 
  1394. $value = $left[1] / $right[1]; 
  1395. break; 
  1396. case '<': 
  1397. return $this->toBool($left[1] < $right[1]); 
  1398. case '>': 
  1399. return $this->toBool($left[1] > $right[1]); 
  1400. case '>=': 
  1401. return $this->toBool($left[1] >= $right[1]); 
  1402. case '=<': 
  1403. return $this->toBool($left[1] <= $right[1]); 
  1404. default: 
  1405. $this->throwError('parse error: unknown number operator: '.$op); 
  1406.  
  1407. return array("number", $value, $unit); 
  1408.  
  1409.  
  1410. /** environment functions */ 
  1411.  
  1412. protected function makeOutputBlock($type, $selectors = null) { 
  1413. $b = new stdclass; 
  1414. $b->lines = array(); 
  1415. $b->children = array(); 
  1416. $b->selectors = $selectors; 
  1417. $b->type = $type; 
  1418. $b->parent = $this->scope; 
  1419. return $b; 
  1420.  
  1421. // the state of execution 
  1422. protected function pushEnv($block = null) { 
  1423. $e = new stdclass; 
  1424. $e->parent = $this->env; 
  1425. $e->store = array(); 
  1426. $e->block = $block; 
  1427.  
  1428. $this->env = $e; 
  1429. return $e; 
  1430.  
  1431. // pop something off the stack 
  1432. protected function popEnv() { 
  1433. $old = $this->env; 
  1434. $this->env = $this->env->parent; 
  1435. return $old; 
  1436.  
  1437. // set something in the current env 
  1438. protected function set($name, $value) { 
  1439. $this->env->store[$name] = $value; 
  1440.  
  1441.  
  1442. // get the highest occurrence entry for a name 
  1443. protected function get($name, $default=null) { 
  1444. $current = $this->env; 
  1445.  
  1446. $isArguments = $name == $this->vPrefix . 'arguments'; 
  1447. while ($current) { 
  1448. if ($isArguments && isset($current->arguments)) { 
  1449. return array('list', ' ', $current->arguments); 
  1450.  
  1451. if (isset($current->store[$name])) 
  1452. return $current->store[$name]; 
  1453. else { 
  1454. $current = isset($current->storeParent) ? 
  1455. $current->storeParent : $current->parent; 
  1456.  
  1457. return $default; 
  1458.  
  1459. // inject array of unparsed strings into environment as variables 
  1460. protected function injectVariables($args) { 
  1461. $this->pushEnv(); 
  1462. $parser = new a3_lessc_parser($this, __METHOD__); 
  1463. foreach ($args as $name => $strValue) { 
  1464. if ($name{0} != '@') $name = '@'.$name; 
  1465. $parser->count = 0; 
  1466. $parser->buffer = (string)$strValue; 
  1467. if (!$parser->propertyValue($value)) { 
  1468. throw new Exception("failed to parse passed in variable $name: $strValue"); 
  1469.  
  1470. $this->set($name, $value); 
  1471.  
  1472. /** 
  1473. * Initialize any static state, can initialize parser for a file 
  1474. * $opts isn't used yet 
  1475. */ 
  1476. public function __construct($fname = null) { 
  1477. if ($fname !== null) { 
  1478. // used for deprecated parse method 
  1479. $this->_parseFile = $fname; 
  1480.  
  1481. public function compile($string, $name = null) { 
  1482. $locale = setlocale(LC_NUMERIC, 0); 
  1483. setlocale(LC_NUMERIC, "C"); 
  1484.  
  1485. $this->parser = $this->makeParser($name); 
  1486. $root = $this->parser->parse($string); 
  1487.  
  1488. $this->env = null; 
  1489. $this->scope = null; 
  1490.  
  1491. $this->formatter = $this->newFormatter(); 
  1492.  
  1493. if (!empty($this->registeredVars)) { 
  1494. $this->injectVariables($this->registeredVars); 
  1495.  
  1496. $this->sourceParser = $this->parser; // used for error messages 
  1497. $this->compileBlock($root); 
  1498.  
  1499. ob_start(); 
  1500. $this->formatter->block($this->scope); 
  1501. $out = ob_get_clean(); 
  1502. setlocale(LC_NUMERIC, $locale); 
  1503. return $out; 
  1504.  
  1505. public function compileFile($fname, $outFname = null) { 
  1506. global $wp_filesystem; 
  1507. if (!is_readable($fname)) { 
  1508. throw new Exception('load error: failed to find '.$fname); 
  1509.  
  1510. $pi = pathinfo($fname); 
  1511.  
  1512. $oldImport = $this->importDir; 
  1513.  
  1514. $this->importDir = (array)$this->importDir; 
  1515. $this->importDir[] = $pi['dirname'].'/'; 
  1516.  
  1517. $this->addParsedFile($fname); 
  1518.  
  1519. $out = $this->compile(file_get_contents($fname), $fname); 
  1520.  
  1521. $this->importDir = $oldImport; 
  1522.  
  1523. if ($outFname !== null) { 
  1524. return $wp_filesystem->put_contents($outFname, $out); 
  1525.  
  1526. return $out; 
  1527.  
  1528. // compile only if changed input has changed or output doesn't exist 
  1529. public function checkedCompile($in, $out) { 
  1530. if (!is_file($out) || filemtime($in) > filemtime($out)) { 
  1531. $this->compileFile($in, $out); 
  1532. return true; 
  1533. return false; 
  1534.  
  1535. /** 
  1536. * Execute lessphp on a .less file or a lessphp cache structure 
  1537. * The lessphp cache structure contains information about a specific 
  1538. * less file having been parsed. It can be used as a hint for future 
  1539. * calls to determine whether or not a rebuild is required. 
  1540. * The cache structure contains two important keys that may be used 
  1541. * externally: 
  1542. * compiled: The final compiled CSS 
  1543. * updated: The time (in seconds) the CSS was last compiled 
  1544. * The cache structure is a plain-ol' PHP associative array and can 
  1545. * be serialized and unserialized without a hitch. 
  1546. * @param mixed $in Input 
  1547. * @param bool $force Force rebuild? 
  1548. * @return array lessphp cache structure 
  1549. */ 
  1550. public function cachedCompile($in, $force = false) { 
  1551. // assume no root 
  1552. $root = null; 
  1553.  
  1554. if (is_string($in)) { 
  1555. $root = $in; 
  1556. } elseif (is_array($in) and isset($in['root'])) { 
  1557. if ($force or ! isset($in['files'])) { 
  1558. // If we are forcing a recompile or if for some reason the 
  1559. // structure does not contain any file information we should 
  1560. // specify the root to trigger a rebuild. 
  1561. $root = $in['root']; 
  1562. } elseif (isset($in['files']) and is_array($in['files'])) { 
  1563. foreach ($in['files'] as $fname => $ftime ) { 
  1564. if (!file_exists($fname) or filemtime($fname) > $ftime) { 
  1565. // One of the files we knew about previously has changed 
  1566. // so we should look at our incoming root again. 
  1567. $root = $in['root']; 
  1568. break; 
  1569. } else { 
  1570. // TODO: Throw an exception? We got neither a string nor something 
  1571. // that looks like a compatible lessphp cache structure. 
  1572. return null; 
  1573.  
  1574. if ($root !== null) { 
  1575. // If we have a root value which means we should rebuild. 
  1576. $out = array(); 
  1577. $out['root'] = $root; 
  1578. $out['compiled'] = $this->compileFile($root); 
  1579. $out['files'] = $this->allParsedFiles(); 
  1580. $out['updated'] = time(); 
  1581. return $out; 
  1582. } else { 
  1583. // No changes, pass back the structure 
  1584. // we were given initially. 
  1585. return $in; 
  1586.  
  1587.  
  1588. // parse and compile buffer 
  1589. // This is deprecated 
  1590. public function parse($str = null, $initialVariables = null) { 
  1591. if (is_array($str)) { 
  1592. $initialVariables = $str; 
  1593. $str = null; 
  1594.  
  1595. $oldVars = $this->registeredVars; 
  1596. if ($initialVariables !== null) { 
  1597. $this->setVariables($initialVariables); 
  1598.  
  1599. if ($str == null) { 
  1600. if (empty($this->_parseFile)) { 
  1601. throw new exception("nothing to parse"); 
  1602.  
  1603. $out = $this->compileFile($this->_parseFile); 
  1604. } else { 
  1605. $out = $this->compile($str); 
  1606.  
  1607. $this->registeredVars = $oldVars; 
  1608. return $out; 
  1609.  
  1610. protected function makeParser($name) { 
  1611. $parser = new a3_lessc_parser($this, $name); 
  1612. $parser->writeComments = $this->preserveComments; 
  1613.  
  1614. return $parser; 
  1615.  
  1616. public function setFormatter($name) { 
  1617. $this->formatterName = $name; 
  1618.  
  1619. protected function newFormatter() { 
  1620. $className = "a3_lessc_formatter_lessjs"; 
  1621. if (!empty($this->formatterName)) { 
  1622. if (!is_string($this->formatterName)) 
  1623. return $this->formatterName; 
  1624. $className = "a3_lessc_formatter_$this->formatterName"; 
  1625.  
  1626. return new $className; 
  1627.  
  1628. public function setPreserveComments($preserve) { 
  1629. $this->preserveComments = $preserve; 
  1630.  
  1631. public function registerFunction($name, $func) { 
  1632. $this->libFunctions[$name] = $func; 
  1633.  
  1634. public function unregisterFunction($name) { 
  1635. unset($this->libFunctions[$name]); 
  1636.  
  1637. public function setVariables($variables) { 
  1638. $this->registeredVars = array_merge($this->registeredVars, $variables); 
  1639.  
  1640. public function unsetVariable($name) { 
  1641. unset($this->registeredVars[$name]); 
  1642.  
  1643. public function setImportDir($dirs) { 
  1644. $this->importDir = (array)$dirs; 
  1645.  
  1646. public function addImportDir($dir) { 
  1647. $this->importDir = (array)$this->importDir; 
  1648. $this->importDir[] = $dir; 
  1649.  
  1650. public function allParsedFiles() { 
  1651. return $this->allParsedFiles; 
  1652.  
  1653. protected function addParsedFile($file) { 
  1654. $this->allParsedFiles[realpath($file)] = filemtime($file); 
  1655.  
  1656. /** 
  1657. * Uses the current value of $this->count to show line and line number 
  1658. */ 
  1659. protected function throwError($msg = null) { 
  1660. if ($this->sourceLoc >= 0) { 
  1661. $this->sourceParser->throwError($msg, $this->sourceLoc); 
  1662. throw new exception($msg); 
  1663.  
  1664. // compile file $in to file $out if $in is newer than $out 
  1665. // returns true when it compiles, false otherwise 
  1666. public static function ccompile($in, $out, $less = null) { 
  1667. if ($less === null) { 
  1668. $less = new self; 
  1669. return $less->checkedCompile($in, $out); 
  1670.  
  1671. public static function cexecute($in, $force = false, $less = null) { 
  1672. if ($less === null) { 
  1673. $less = new self; 
  1674. return $less->cachedCompile($in, $force); 
  1675.  
  1676. static protected $cssColors = array( 
  1677. 'aliceblue' => '240, 248, 255',  
  1678. 'antiquewhite' => '250, 235, 215',  
  1679. 'aqua' => '0, 255, 255',  
  1680. 'aquamarine' => '127, 255, 212',  
  1681. 'azure' => '240, 255, 255',  
  1682. 'beige' => '245, 245, 220',  
  1683. 'bisque' => '255, 228, 196',  
  1684. 'black' => '0, 0, 0',  
  1685. 'blanchedalmond' => '255, 235, 205',  
  1686. 'blue' => '0, 0, 255',  
  1687. 'blueviolet' => '138, 43, 226',  
  1688. 'brown' => '165, 42, 42',  
  1689. 'burlywood' => '222, 184, 135',  
  1690. 'cadetblue' => '95, 158, 160',  
  1691. 'chartreuse' => '127, 255, 0',  
  1692. 'chocolate' => '210, 105, 30',  
  1693. 'coral' => '255, 127, 80',  
  1694. 'cornflowerblue' => '100, 149, 237',  
  1695. 'cornsilk' => '255, 248, 220',  
  1696. 'crimson' => '220, 20, 60',  
  1697. 'cyan' => '0, 255, 255',  
  1698. 'darkblue' => '0, 0, 139',  
  1699. 'darkcyan' => '0, 139, 139',  
  1700. 'darkgoldenrod' => '184, 134, 11',  
  1701. 'darkgray' => '169, 169, 169',  
  1702. 'darkgreen' => '0, 100, 0',  
  1703. 'darkgrey' => '169, 169, 169',  
  1704. 'darkkhaki' => '189, 183, 107',  
  1705. 'darkmagenta' => '139, 0, 139',  
  1706. 'darkolivegreen' => '85, 107, 47',  
  1707. 'darkorange' => '255, 140, 0',  
  1708. 'darkorchid' => '153, 50, 204',  
  1709. 'darkred' => '139, 0, 0',  
  1710. 'darksalmon' => '233, 150, 122',  
  1711. 'darkseagreen' => '143, 188, 143',  
  1712. 'darkslateblue' => '72, 61, 139',  
  1713. 'darkslategray' => '47, 79, 79',  
  1714. 'darkslategrey' => '47, 79, 79',  
  1715. 'darkturquoise' => '0, 206, 209',  
  1716. 'darkviolet' => '148, 0, 211',  
  1717. 'deeppink' => '255, 20, 147',  
  1718. 'deepskyblue' => '0, 191, 255',  
  1719. 'dimgray' => '105, 105, 105',  
  1720. 'dimgrey' => '105, 105, 105',  
  1721. 'dodgerblue' => '30, 144, 255',  
  1722. 'firebrick' => '178, 34, 34',  
  1723. 'floralwhite' => '255, 250, 240',  
  1724. 'forestgreen' => '34, 139, 34',  
  1725. 'fuchsia' => '255, 0, 255',  
  1726. 'gainsboro' => '220, 220, 220',  
  1727. 'ghostwhite' => '248, 248, 255',  
  1728. 'gold' => '255, 215, 0',  
  1729. 'goldenrod' => '218, 165, 32',  
  1730. 'gray' => '128, 128, 128',  
  1731. 'green' => '0, 128, 0',  
  1732. 'greenyellow' => '173, 255, 47',  
  1733. 'grey' => '128, 128, 128',  
  1734. 'honeydew' => '240, 255, 240',  
  1735. 'hotpink' => '255, 105, 180',  
  1736. 'indianred' => '205, 92, 92',  
  1737. 'indigo' => '75, 0, 130',  
  1738. 'ivory' => '255, 255, 240',  
  1739. 'khaki' => '240, 230, 140',  
  1740. 'lavender' => '230, 230, 250',  
  1741. 'lavenderblush' => '255, 240, 245',  
  1742. 'lawngreen' => '124, 252, 0',  
  1743. 'lemonchiffon' => '255, 250, 205',  
  1744. 'lightblue' => '173, 216, 230',  
  1745. 'lightcoral' => '240, 128, 128',  
  1746. 'lightcyan' => '224, 255, 255',  
  1747. 'lightgoldenrodyellow' => '250, 250, 210',  
  1748. 'lightgray' => '211, 211, 211',  
  1749. 'lightgreen' => '144, 238, 144',  
  1750. 'lightgrey' => '211, 211, 211',  
  1751. 'lightpink' => '255, 182, 193',  
  1752. 'lightsalmon' => '255, 160, 122',  
  1753. 'lightseagreen' => '32, 178, 170',  
  1754. 'lightskyblue' => '135, 206, 250',  
  1755. 'lightslategray' => '119, 136, 153',  
  1756. 'lightslategrey' => '119, 136, 153',  
  1757. 'lightsteelblue' => '176, 196, 222',  
  1758. 'lightyellow' => '255, 255, 224',  
  1759. 'lime' => '0, 255, 0',  
  1760. 'limegreen' => '50, 205, 50',  
  1761. 'linen' => '250, 240, 230',  
  1762. 'magenta' => '255, 0, 255',  
  1763. 'maroon' => '128, 0, 0',  
  1764. 'mediumaquamarine' => '102, 205, 170',  
  1765. 'mediumblue' => '0, 0, 205',  
  1766. 'mediumorchid' => '186, 85, 211',  
  1767. 'mediumpurple' => '147, 112, 219',  
  1768. 'mediumseagreen' => '60, 179, 113',  
  1769. 'mediumslateblue' => '123, 104, 238',  
  1770. 'mediumspringgreen' => '0, 250, 154',  
  1771. 'mediumturquoise' => '72, 209, 204',  
  1772. 'mediumvioletred' => '199, 21, 133',  
  1773. 'midnightblue' => '25, 25, 112',  
  1774. 'mintcream' => '245, 255, 250',  
  1775. 'mistyrose' => '255, 228, 225',  
  1776. 'moccasin' => '255, 228, 181',  
  1777. 'navajowhite' => '255, 222, 173',  
  1778. 'navy' => '0, 0, 128',  
  1779. 'oldlace' => '253, 245, 230',  
  1780. 'olive' => '128, 128, 0',  
  1781. 'olivedrab' => '107, 142, 35',  
  1782. 'orange' => '255, 165, 0',  
  1783. 'orangered' => '255, 69, 0',  
  1784. 'orchid' => '218, 112, 214',  
  1785. 'palegoldenrod' => '238, 232, 170',  
  1786. 'palegreen' => '152, 251, 152',  
  1787. 'paleturquoise' => '175, 238, 238',  
  1788. 'palevioletred' => '219, 112, 147',  
  1789. 'papayawhip' => '255, 239, 213',  
  1790. 'peachpuff' => '255, 218, 185',  
  1791. 'peru' => '205, 133, 63',  
  1792. 'pink' => '255, 192, 203',  
  1793. 'plum' => '221, 160, 221',  
  1794. 'powderblue' => '176, 224, 230',  
  1795. 'purple' => '128, 0, 128',  
  1796. 'red' => '255, 0, 0',  
  1797. 'rosybrown' => '188, 143, 143',  
  1798. 'royalblue' => '65, 105, 225',  
  1799. 'saddlebrown' => '139, 69, 19',  
  1800. 'salmon' => '250, 128, 114',  
  1801. 'sandybrown' => '244, 164, 96',  
  1802. 'seagreen' => '46, 139, 87',  
  1803. 'seashell' => '255, 245, 238',  
  1804. 'sienna' => '160, 82, 45',  
  1805. 'silver' => '192, 192, 192',  
  1806. 'skyblue' => '135, 206, 235',  
  1807. 'slateblue' => '106, 90, 205',  
  1808. 'slategray' => '112, 128, 144',  
  1809. 'slategrey' => '112, 128, 144',  
  1810. 'snow' => '255, 250, 250',  
  1811. 'springgreen' => '0, 255, 127',  
  1812. 'steelblue' => '70, 130, 180',  
  1813. 'tan' => '210, 180, 140',  
  1814. 'teal' => '0, 128, 128',  
  1815. 'thistle' => '216, 191, 216',  
  1816. 'tomato' => '255, 99, 71',  
  1817. 'transparent' => '0, 0, 0, 0',  
  1818. 'turquoise' => '64, 224, 208',  
  1819. 'violet' => '238, 130, 238',  
  1820. 'wheat' => '245, 222, 179',  
  1821. 'white' => '255, 255, 255',  
  1822. 'whitesmoke' => '245, 245, 245',  
  1823. 'yellow' => '255, 255, 0',  
  1824. 'yellowgreen' => '154, 205, 50' 
  1825. );