YIT_Gradients

The YITH WooCommerce Wishlist YIT Gradients class.

Defined (1)

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

/plugin-fw/lib/yit-plugin-gradients.php  
  1. class YIT_Gradients { 
  2.  
  3. /** 
  4. * An array of colors to use for a gradient. 
  5. * @var array 
  6. * @since 1.0 
  7. */ 
  8. public $colors_gradient = array(); 
  9.  
  10. /** 
  11. * Set properties 
  12. * @param string $key 
  13. * @param $value 
  14. * @internal param array $colors_gradient 
  15. * @return void 
  16. * @since 1.0 
  17. */ 
  18. public function set( $key, $value ) { 
  19. if ( property_exists( $this, $key ) ) { 
  20. $this->{$key} = $value; 
  21.  
  22. /** 
  23. * Get properties 
  24. * @param string $key 
  25. * @return mixed 
  26. * @since 1.0 
  27. */ 
  28. public function get( $key ) { 
  29. if ( property_exists( $this, $key ) ) { 
  30. return $this->{$key}; 
  31.  
  32. /** 
  33. * Add a color to use in a gradient. 
  34. * @param string $color 
  35. * @param int $position 
  36. * @return void 
  37. * @since 1.0 
  38. */ 
  39. public function add_color_gradient( $color, $position ) { 
  40. $the_color['color'] = $color; 
  41. $the_color['position'] = $position; 
  42.  
  43. array_push( $this->colors_gradient, $the_color ); 
  44.  
  45. /** 
  46. * Generate the CSS code for a gradient. 
  47. * @param string $role 
  48. * @param string $direction 
  49. * @return string|bool 
  50. * @since 1.0 
  51. */ 
  52. public function gradient( $role, $direction ) { 
  53. if ( ! empty( $this->colors_gradient ) ) { 
  54.  
  55. $css = array( 
  56. 'old' => $this->_make_old_gradient( $this->colors_gradient[0]['color'] ), //old browser 
  57. 'ff3' => $this->_make_modern_gradient( $this->colors_gradient, $direction, 'moz' ), //firefox 3.6+ 
  58. 'chr_saf4' => $this->_make_chr_saf4_gradient( $this->colors_gradient, $direction ), //chrome and safari4+ 
  59. 'chr10_saf5' => $this->_make_modern_gradient( $this->colors_gradient, $direction, 'webkit' ), //chrome10+ and safari5+ 
  60. 'opera' => $this->_make_modern_gradient( $this->colors_gradient, $direction, 'o' ), //opera11.10+ 
  61. 'ie10' => $this->_make_modern_gradient( $this->colors_gradient, $direction, 'ms' ), //internet explorer 10+ 
  62. 'w3c' => $this->_make_modern_gradient( $this->colors_gradient, $direction, 'w3c' ), //w3c 
  63. 'ie6_9' => $this->_make_ie6_gradient( $this->colors_gradient, $direction ) //ie6-9 
  64. ); 
  65.  
  66. $css = $role . '{' . implode( ';', $css ) . '}'; 
  67.  
  68. $this->colors_gradient = array(); 
  69.  
  70. return $css; 
  71.  
  72. /** 
  73. * Reverse a gradient. This method should be used only before calling ::make_gradient(). Otherwise it will not works. 
  74. * @return void 
  75. * @since 1.0 
  76. */ 
  77. public function reverse_gradient() { 
  78. $colors_gradient = array_reverse( $this->get( 'colors_gradient' ) ); 
  79.  
  80. for ( $i = 0; $i < count( $colors_gradient ); $i ++ ) { 
  81. $colors_gradient[$i]['position'] = 100 - $colors_gradient[$i]['position']; 
  82.  
  83. $this->set( 'colors_gradient', $colors_gradient ); 
  84.  
  85. /** 
  86. * Generate the CSS code for a gradient. 
  87. * @param string $role 
  88. * @param string $direction 
  89. * @return string|bool 
  90. * @since 1.0 
  91. */ 
  92. public function get_gradient( $role, $direction ) { 
  93. return $this->gradient( $role, $direction ); 
  94.  
  95. /** 
  96. * Generate the CSS code for a gradient. 
  97. * @param string $role 
  98. * @param string $direction 
  99. * @return void 
  100. * @since 1.0 
  101. */ 
  102. public function the_gradient( $role, $direction ) { 
  103. echo $this->get_gradient( $role, $direction ); 
  104.  
  105. /** 
  106. * Generate the CSS code for a gradient. 
  107. * @param string $role 
  108. * @param string $from 
  109. * @param string $to 
  110. * @param string $direction 
  111. * @return string|bool 
  112. * @since 1.0 
  113. */ 
  114. public function gradient_from_to( $role, $from, $to, $direction ) { 
  115.  
  116. $colors = array( 
  117. array( 
  118. 'color' => $from,  
  119. 'position' => 0 
  120. ),  
  121. array( 
  122. 'color' => $to,  
  123. 'position' => 100 
  124. ),  
  125. ); 
  126.  
  127. $this->set( 'colors_gradient', $colors ); 
  128. return $this->get_gradient( $role, $direction ); 
  129.  
  130. /** 
  131. * Generate the CSS code for a gradient. 
  132. * @param string $role 
  133. * @param string $color 
  134. * @param string $direction 
  135. * @param int|string $factor 
  136. * @return string|bool 
  137. * @since 1.0 
  138. */ 
  139. public function gradient_darker( $role, $color, $direction, $factor = 30 ) { 
  140.  
  141. $colors = array( 
  142. array( 
  143. 'color' => $color,  
  144. 'position' => 0 
  145. ),  
  146. array( 
  147. 'color' => $this->hex_darker( $color, $factor ),  
  148. 'position' => 100 
  149. ),  
  150. ); 
  151.  
  152. $this->set( 'colors_gradient', $colors ); 
  153. return $this->get_gradient( $role, $direction ); 
  154.  
  155. /** 
  156. * Generate the CSS code for a gradient. 
  157. * @param string $role 
  158. * @param string $color 
  159. * @param string $direction 
  160. * @param int|string $factor 
  161. * @return string|bool 
  162. * @since 1.0 
  163. */ 
  164. public function gradient_lighter( $role, $color, $direction, $factor = 30 ) { 
  165.  
  166. $colors = array( 
  167. array( 
  168. 'color' => $color,  
  169. 'position' => 0 
  170. ),  
  171. array( 
  172. 'color' => $this->hex_lighter( $color, $factor ),  
  173. 'position' => 100 
  174. ),  
  175. ); 
  176.  
  177. $this->set( 'colors_gradient', $colors ); 
  178. return $this->get_gradient( $role, $direction ); 
  179.  
  180. /** 
  181. * Generate the CSS code for a gradient that not supports gradients (add only a background color). 
  182. * @param $color 
  183. * @internal param string $role 
  184. * @return string|bool 
  185. * @access private 
  186. * @since 1.0 
  187. */ 
  188. private function _make_old_gradient( $color ) { 
  189. return 'background:' . $color; 
  190.  
  191. /** 
  192. * Generate the CSS code for a gradient in IE6-9. 
  193. * @param $colors 
  194. * @param $direction 
  195. * @internal param string $role 
  196. * @return string|bool 
  197. * @access private 
  198. * @since 1.0 
  199. */ 
  200. private function _make_ie6_gradient( $colors, $direction ) { 
  201. $css = 'filter:progid:DXImageTransform.Microsoft.gradient('; 
  202. $css .= 'startColorstr=\'' . $colors[0]['color'] . '\', '; 
  203. $css .= 'endColorstr=\'' . $colors[count( $colors ) - 1]['color'] . '\', '; 
  204.  
  205. if ( $direction == 'horizontal' ) { 
  206. $css .= 'GradientType=1'; 
  207. else { 
  208. $css .= 'GradientType=0'; 
  209. } //vertical 
  210.  
  211. $css .= ')'; 
  212.  
  213. return $css; 
  214.  
  215. /** 
  216. * Make the CSS 3 for a gradient in modern browsers( FF3.6+, Chrome, Safari5+, Opera11.10+, IE10+ ) 
  217. * @param array $colors 
  218. * @param string $direction 
  219. * @param $browser 
  220. * @return string 
  221. * @access private 
  222. * @since 1.0 
  223. */ 
  224. private function _make_modern_gradient( $colors, $direction, $browser ) { 
  225. $css = 'background:'; 
  226.  
  227. //Add the browser suffix 
  228. if ( $browser != 'w3c' ) { 
  229. $browser = '-' . $browser . '-'; 
  230. else { 
  231. $browser = ''; 
  232.  
  233. switch ( $direction ) { 
  234. case 'vertical' : 
  235. $css .= $browser . 'linear-gradient(top, '; 
  236. break; 
  237. case 'horizontal' : 
  238. $css .= $browser . 'linear-gradient(left, '; 
  239. break; 
  240. case 'diagonal-bottom': 
  241. $css .= $browser . 'linear-gradient(-45deg, '; 
  242. break; 
  243. case 'diagonal-top' : 
  244. $css .= $browser . 'linear-gradient(45deg, '; 
  245. break; 
  246. case 'radial' : 
  247. $css .= $browser . 'radial-gradient(center, ellipse cover, '; 
  248. break; 
  249.  
  250. foreach ( $colors as $stop ) { 
  251. $css .= $stop['color'] . ' ' . $stop['position'] . '%, '; 
  252.  
  253. $css = rtrim( $css ); 
  254. $css = rtrim( $css, ', ' ); 
  255. $css .= ')'; 
  256.  
  257. return $css; 
  258.  
  259. /** 
  260. * Make the CSS 3 for a gradient in Chrome and Safari 4+ 
  261. * @param array $colors 
  262. * @param string $direction 
  263. * @return string 
  264. * @access private 
  265. * @since 1.0 
  266. */ 
  267. private function _make_chr_saf4_gradient( $colors, $direction ) { 
  268. $css = 'background:'; 
  269.  
  270. switch ( $direction ) { 
  271. case 'vertical' : 
  272. $css .= '-webkit-gradient(linear, left top, left bottom, '; 
  273. break; 
  274. case 'horizontal' : 
  275. $css .= '-webkit-gradient(linear, left top, right top, '; 
  276. break; 
  277. case 'diagonal-bottom': 
  278. $css .= '-webkit-gradient(linear, left top, right bottom, '; 
  279. break; 
  280. case 'diagonal-top' : 
  281. $css .= '-webkit-gradient(linear, left bottom, right top, '; 
  282. break; 
  283. case 'radial' : 
  284. $css .= '-webkit-gradient(radial, center center, 0px, center center, 100%, '; 
  285. break; 
  286.  
  287. foreach ( $colors as $stop ) { 
  288. $css .= 'color-stop(' . $stop['position'] . '%, ' . $stop['color'] . '), '; 
  289.  
  290. $css = rtrim( $css ); 
  291. $css = rtrim( $css, ', ' ); 
  292. $css .= ')'; 
  293.  
  294. return $css; 
  295.  
  296.  
  297. /** 
  298. * Return an instance of the model called 
  299. * @param string $class The name of class that I want the instance 
  300. * @since 2.0.0 
  301. * @author Simone D'Amico <simone.damico@yithemes.com> 
  302. * @return mixed 
  303. */ 
  304. public function getModel( $class ) { 
  305. return YIT_Registry::get_instance()->$class; 
  306.  
  307.  
  308. /** 
  309. * Return a color darker then $color. 
  310. * @param string $color 
  311. * @param int $factor 
  312. * @return string 
  313. * @since 1.0 
  314. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  315. */ 
  316. public function hex_darker( $color, $factor = 30 ) { 
  317. $color = str_replace( '#', '', $color ); 
  318.  
  319. $base['R'] = hexdec( substr( $color, 0, 2 ) ); 
  320. $base['G'] = hexdec( substr( $color, 2, 2 ) ); 
  321. $base['B'] = hexdec( substr( $color, 4, 2 ) ); 
  322.  
  323. $color = '#'; 
  324.  
  325. foreach ( $base as $k => $v ) { 
  326. $amount = $v / 100; 
  327. $amount = round( $amount * $factor ); 
  328. $new_decimal = $v - $amount; 
  329.  
  330. $new_hex_component = dechex( $new_decimal ); 
  331.  
  332. if ( strlen( $new_hex_component ) < 2 ) { 
  333. $new_hex_component = "0" . $new_hex_component; 
  334.  
  335. $color .= $new_hex_component; 
  336.  
  337. return $color; 
  338.  
  339. /** 
  340. * Return a color lighter then $color. 
  341. * @param string $color 
  342. * @param int $factor 
  343. * @return string 
  344. * @since 1.0 
  345. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  346. */ 
  347. public function hex_lighter( $color, $factor = 30 ) { 
  348. $color = str_replace( '#', '', $color ); 
  349.  
  350. $base['R'] = hexdec( $color{0} . $color{1} ); 
  351. $base['G'] = hexdec( $color{2} . $color{3} ); 
  352. $base['B'] = hexdec( $color{4} . $color{5} ); 
  353.  
  354. $color = '#'; 
  355.  
  356. foreach ( $base as $k => $v ) { 
  357. $amount = 255 - $v; 
  358. $amount = $amount / 100; 
  359. $amount = round( $amount * $factor ); 
  360. $new_decimal = $v + $amount; 
  361.  
  362. $new_hex_component = dechex( $new_decimal ); 
  363.  
  364. if ( strlen( $new_hex_component ) < 2 ) { 
  365. $new_hex_component = "0" . $new_hex_component; 
  366.  
  367. $color .= $new_hex_component; 
  368.  
  369. return $color; 
  370.  
  371. /** 
  372. * Detect if we must use a color darker or lighter then the background. 
  373. * @param string $color 
  374. * @param string $dark 
  375. * @param string $light 
  376. * @return string 
  377. * @since 1.0 
  378. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  379. */ 
  380. public function light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) { 
  381. $hex = str_replace( '#', '', $color ); 
  382.  
  383. $c_r = hexdec( substr( $hex, 0, 2 ) ); 
  384. $c_g = hexdec( substr( $hex, 2, 2 ) ); 
  385. $c_b = hexdec( substr( $hex, 4, 2 ) ); 
  386. $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000; 
  387.  
  388. return ( $brightness > 155 ) ? $dark : $light; 
  389.  
  390. /** 
  391. * Detect if we must use a color darker or lighter then the background. 
  392. * @param $hex 
  393. * @internal param string $color 
  394. * @return string 
  395. * @since 1.0 
  396. * @author Andrea Grillo <andrea.grillo@yithemes.com> 
  397. */ 
  398. public function hex2rgb( $hex ) { 
  399. $hex = str_replace( "#", "", $hex ); 
  400.  
  401. if ( strlen( $hex ) == 3 ) { 
  402. $r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) ); 
  403. $g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) ); 
  404. $b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) ); 
  405. else { 
  406. $r = hexdec( substr( $hex, 0, 2 ) ); 
  407. $g = hexdec( substr( $hex, 2, 2 ) ); 
  408. $b = hexdec( substr( $hex, 4, 2 ) ); 
  409. $rgb = array( $r, $g, $b ); 
  410. //return implode(", ", $rgb); // returns the rgb values separated by commas 
  411. return $rgb; // returns an array with the rgb values 
  412.  
  413. /** 
  414. * Magic method for this class 
  415. * @param $name string The name of magic property 
  416. * @since 2.0.0 
  417. * @author Simone D'Amico <simone.damico@yithemes.com> 
  418. * @return mixed 
  419. */ 
  420. public function __get( $name ) { 
  421. if ( $name == 'request' ) { 
  422. if ( ! $this->_request instanceof YIT_Request ) { 
  423. $this->_request = YIT_Registry::get_instance()->request; 
  424.  
  425. return $this->_request;