lessc

The LESS compiler and parser.

Defined (1)

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

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