/vendor/oyejorge/less.php/lib/Less/Tree/Color.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 230 lines · 157 code · 43 blank · 30 comment · 24 complexity · 096e1c980f3a670a5f5f07e2aba3c96f MD5 · raw file

  1. <?php
  2. /**
  3. * Color
  4. *
  5. * @package Less
  6. * @subpackage tree
  7. */
  8. class Less_Tree_Color extends Less_Tree{
  9. public $rgb;
  10. public $alpha;
  11. public $isTransparentKeyword;
  12. public $type = 'Color';
  13. public function __construct($rgb, $a = 1, $isTransparentKeyword = null ){
  14. if( $isTransparentKeyword ){
  15. $this->rgb = $rgb;
  16. $this->alpha = $a;
  17. $this->isTransparentKeyword = true;
  18. return;
  19. }
  20. $this->rgb = array();
  21. if( is_array($rgb) ){
  22. $this->rgb = $rgb;
  23. }else if( strlen($rgb) == 6 ){
  24. foreach(str_split($rgb, 2) as $c){
  25. $this->rgb[] = hexdec($c);
  26. }
  27. }else{
  28. foreach(str_split($rgb, 1) as $c){
  29. $this->rgb[] = hexdec($c.$c);
  30. }
  31. }
  32. $this->alpha = is_numeric($a) ? $a : 1;
  33. }
  34. public function compile(){
  35. return $this;
  36. }
  37. public function luma(){
  38. $r = $this->rgb[0] / 255;
  39. $g = $this->rgb[1] / 255;
  40. $b = $this->rgb[2] / 255;
  41. $r = ($r <= 0.03928) ? $r / 12.92 : pow((($r + 0.055) / 1.055), 2.4);
  42. $g = ($g <= 0.03928) ? $g / 12.92 : pow((($g + 0.055) / 1.055), 2.4);
  43. $b = ($b <= 0.03928) ? $b / 12.92 : pow((($b + 0.055) / 1.055), 2.4);
  44. return 0.2126 * $r + 0.7152 * $g + 0.0722 * $b;
  45. }
  46. /**
  47. * @see Less_Tree::genCSS
  48. */
  49. public function genCSS( $output ){
  50. $output->add( $this->toCSS() );
  51. }
  52. public function toCSS( $doNotCompress = false ){
  53. $compress = Less_Parser::$options['compress'] && !$doNotCompress;
  54. $alpha = Less_Functions::fround( $this->alpha );
  55. //
  56. // If we have some transparency, the only way to represent it
  57. // is via `rgba`. Otherwise, we use the hex representation,
  58. // which has better compatibility with older browsers.
  59. // Values are capped between `0` and `255`, rounded and zero-padded.
  60. //
  61. if( $alpha < 1 ){
  62. if( ( $alpha === 0 || $alpha === 0.0 ) && isset($this->isTransparentKeyword) && $this->isTransparentKeyword ){
  63. return 'transparent';
  64. }
  65. $values = array();
  66. foreach($this->rgb as $c){
  67. $values[] = Less_Functions::clamp( round($c), 255);
  68. }
  69. $values[] = $alpha;
  70. $glue = ($compress ? ',' : ', ');
  71. return "rgba(" . implode($glue, $values) . ")";
  72. }else{
  73. $color = $this->toRGB();
  74. if( $compress ){
  75. // Convert color to short format
  76. if( $color[1] === $color[2] && $color[3] === $color[4] && $color[5] === $color[6]) {
  77. $color = '#'.$color[1] . $color[3] . $color[5];
  78. }
  79. }
  80. return $color;
  81. }
  82. }
  83. //
  84. // Operations have to be done per-channel, if not,
  85. // channels will spill onto each other. Once we have
  86. // our result, in the form of an integer triplet,
  87. // we create a new Color node to hold the result.
  88. //
  89. /**
  90. * @param string $op
  91. */
  92. public function operate( $op, $other) {
  93. $rgb = array();
  94. $alpha = $this->alpha * (1 - $other->alpha) + $other->alpha;
  95. for ($c = 0; $c < 3; $c++) {
  96. $rgb[$c] = Less_Functions::operate( $op, $this->rgb[$c], $other->rgb[$c]);
  97. }
  98. return new Less_Tree_Color($rgb, $alpha);
  99. }
  100. public function toRGB(){
  101. return $this->toHex($this->rgb);
  102. }
  103. public function toHSL(){
  104. $r = $this->rgb[0] / 255;
  105. $g = $this->rgb[1] / 255;
  106. $b = $this->rgb[2] / 255;
  107. $a = $this->alpha;
  108. $max = max($r, $g, $b);
  109. $min = min($r, $g, $b);
  110. $l = ($max + $min) / 2;
  111. $d = $max - $min;
  112. $h = $s = 0;
  113. if( $max !== $min ){
  114. $s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min);
  115. switch ($max) {
  116. case $r: $h = ($g - $b) / $d + ($g < $b ? 6 : 0); break;
  117. case $g: $h = ($b - $r) / $d + 2; break;
  118. case $b: $h = ($r - $g) / $d + 4; break;
  119. }
  120. $h /= 6;
  121. }
  122. return array('h' => $h * 360, 's' => $s, 'l' => $l, 'a' => $a );
  123. }
  124. //Adapted from http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
  125. public function toHSV() {
  126. $r = $this->rgb[0] / 255;
  127. $g = $this->rgb[1] / 255;
  128. $b = $this->rgb[2] / 255;
  129. $a = $this->alpha;
  130. $max = max($r, $g, $b);
  131. $min = min($r, $g, $b);
  132. $v = $max;
  133. $d = $max - $min;
  134. if ($max === 0) {
  135. $s = 0;
  136. } else {
  137. $s = $d / $max;
  138. }
  139. $h = 0;
  140. if( $max !== $min ){
  141. switch($max){
  142. case $r: $h = ($g - $b) / $d + ($g < $b ? 6 : 0); break;
  143. case $g: $h = ($b - $r) / $d + 2; break;
  144. case $b: $h = ($r - $g) / $d + 4; break;
  145. }
  146. $h /= 6;
  147. }
  148. return array('h'=> $h * 360, 's'=> $s, 'v'=> $v, 'a' => $a );
  149. }
  150. public function toARGB(){
  151. $argb = array_merge( (array) Less_Parser::round($this->alpha * 255), $this->rgb);
  152. return $this->toHex( $argb );
  153. }
  154. public function compare($x){
  155. if( !property_exists( $x, 'rgb' ) ){
  156. return -1;
  157. }
  158. return ($x->rgb[0] === $this->rgb[0] &&
  159. $x->rgb[1] === $this->rgb[1] &&
  160. $x->rgb[2] === $this->rgb[2] &&
  161. $x->alpha === $this->alpha) ? 0 : -1;
  162. }
  163. public function toHex( $v ){
  164. $ret = '#';
  165. foreach($v as $c){
  166. $c = Less_Functions::clamp( Less_Parser::round($c), 255);
  167. if( $c < 16 ){
  168. $ret .= '0';
  169. }
  170. $ret .= dechex($c);
  171. }
  172. return $ret;
  173. }
  174. /**
  175. * @param string $keyword
  176. */
  177. public static function fromKeyword( $keyword ){
  178. $keyword = strtolower($keyword);
  179. if( Less_Colors::hasOwnProperty($keyword) ){
  180. // detect named color
  181. return new Less_Tree_Color(substr(Less_Colors::color($keyword), 1));
  182. }
  183. if( $keyword === 'transparent' ){
  184. return new Less_Tree_Color( array(0, 0, 0), 0, true);
  185. }
  186. }
  187. }