Color.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace Dcat\Admin;
  3. use Dcat\Admin\Support\Helper;
  4. /**
  5. * Class Color.
  6. *
  7. *
  8. * @method string primary(int $amt = 0)
  9. * @method string primaryDarker(int $amt = 0)
  10. * @method string link(int $amt = 0)
  11. * @method string info(int $amt = 0)
  12. * @method string success(int $amt = 0)
  13. * @method string danger(int $amt = 0)
  14. * @method string warning(int $amt = 0)
  15. * @method string indigo(int $amt = 0)
  16. * @method string blue(int $amt = 0)
  17. * @method string red(int $amt = 0)
  18. * @method string orange(int $amt = 0)
  19. * @method string green(int $amt = 0)
  20. * @method string cyan(int $amt = 0)
  21. * @method string purple(int $amt = 0)
  22. * @method string custom(int $amt = 0)
  23. * @method string pink(int $amt = 0)
  24. * @method string dark(int $amt = 0)
  25. * @method string white(int $amt = 0)
  26. * @method string white50(int $amt = 0)
  27. * @method string blue1(int $amt = 0)
  28. * @method string blue2(int $amt = 0)
  29. * @method string orange1(int $amt = 0)
  30. * @method string orange2(int $amt = 0)
  31. * @method string yellow(int $amt = 0)
  32. * @method string indigoDarker(int $amt = 0)
  33. * @method string redDarker(int $amt = 0)
  34. * @method string blueDarker(int $amt = 0)
  35. * @method string cyanDarker(int $amt = 0)
  36. * @method string gray(int $amt = 0)
  37. * @method string light(int $amt = 0)
  38. * @method string tear(int $amt = 0)
  39. * @method string tear1(int $amt = 0)
  40. * @method string dark20(int $amt = 0)
  41. * @method string dark30(int $amt = 0)
  42. * @method string dark35(int $amt = 0)
  43. * @method string dark40(int $amt = 0)
  44. * @method string dark50(int $amt = 0)
  45. * @method string dark60(int $amt = 0)
  46. * @method string dark70(int $amt = 0)
  47. * @method string dark80(int $amt = 0)
  48. * @method string dark90(int $amt = 0)
  49. * @method string dark90half(int $amt = 0)
  50. * @method string font(int $amt = 0)
  51. * @method string grayBg(int $amt = 0)
  52. * @method string border(int $amt = 0)
  53. * @method string inputBorder(int $amt = 0)
  54. */
  55. class Color
  56. {
  57. /**
  58. * 颜色.
  59. *
  60. * @var array
  61. */
  62. protected static $colors = [
  63. 'indigo' => [
  64. 'css' => [
  65. ],
  66. 'colors' => [
  67. 'primary' => 'indigo',
  68. 'primary-darker' => 'indigo-darker',
  69. 'link' => 'indigo-darker',
  70. ],
  71. ],
  72. ];
  73. /**
  74. * 默认颜色.
  75. *
  76. * @var array
  77. */
  78. protected static $default = [
  79. 'info' => 'blue',
  80. 'success' => 'green',
  81. 'danger' => 'red',
  82. 'warning' => 'orange',
  83. 'indigo' => '#5c6bc6',
  84. 'blue' => '#3085d6',
  85. 'red' => '#ea5455',
  86. 'orange' => '#dda451',
  87. 'green' => '#21b978',
  88. 'cyan' => '#7367f0',
  89. 'purple' => '#5b69bc',
  90. 'custom' => '#59a9f8',
  91. 'pink' => '#ff8acc',
  92. 'dark' => '#22292f',
  93. 'white' => '#fff',
  94. 'white50' => 'hsla(0,0%,100%,.5)',
  95. // 其他蓝色
  96. 'blue1' => '#007ee5',
  97. 'blue2' => '#4199de',
  98. // 橘色
  99. 'orange1' => '#ffcc80',
  100. 'orange2' => '#F99037',
  101. // 黄色
  102. 'yellow' => '#edc30e',
  103. 'indigo-darker' => '#495abf',
  104. 'red-darker' => '#bd4147',
  105. 'blue-darker' => '#236bb0',
  106. 'cyan-darker' => '#6355ee',
  107. // 灰色
  108. 'gray' => '#b9c3cd',
  109. // 轻灰
  110. 'light' => '#f7f7f9',
  111. // 水鸭色
  112. 'tear' => '#01847f',
  113. 'tear1' => '#00b5b5',
  114. // 深色
  115. 'dark20' => '#f6fbff',
  116. 'dark30' => '#f4f7fa',
  117. 'dark35' => '#e7eef7',
  118. 'dark40' => '#ebf0f3',
  119. 'dark50' => '#d3dde5',
  120. 'dark60' => '#bacad6',
  121. 'dark70' => '#b3b9bf',
  122. 'dark80' => '#7c858e',
  123. 'dark85' => '#5c7089',
  124. 'dark90' => '#252d37',
  125. // 文本通用颜色
  126. 'font' => '#414750',
  127. // 灰色背景
  128. 'gray-bg' => '#f1f1f1',
  129. // 边框颜色
  130. 'border' => '#ebeff2',
  131. // 表单边框
  132. 'input-border' => '#d9d9d9',
  133. ];
  134. /**
  135. * 主题名称.
  136. *
  137. * @var string
  138. */
  139. protected $name;
  140. /**
  141. * @var array
  142. */
  143. protected $currentColors = [];
  144. /**
  145. * @var array
  146. */
  147. protected $realColors;
  148. /**
  149. * Color constructor.
  150. *
  151. * @param string $name
  152. */
  153. public function __construct($name = null)
  154. {
  155. $this->name = ($name ?: config('admin.layout.color')) ?: 'indigo';
  156. $this->currentColors = array_merge(
  157. static::$default,
  158. static::$colors[$this->name]['colors'] ?? []
  159. );
  160. }
  161. /**
  162. * @return string
  163. */
  164. public function name()
  165. {
  166. return $this->name;
  167. }
  168. /**
  169. * 获取css文件路径.
  170. *
  171. * @return array
  172. */
  173. public function css()
  174. {
  175. return static::$colors[$this->name]['css'];
  176. }
  177. /**
  178. * 获取颜色.
  179. *
  180. * @param array $colorName
  181. * @param string $default
  182. *
  183. * @return string
  184. */
  185. public function get(string $colorName, string $default = null)
  186. {
  187. if ($this->realColors) {
  188. return $this->realColors[$colorName] ?? $default;
  189. }
  190. $result = $this->currentColors[$colorName] ?? $default;
  191. if ($result && ! empty($this->currentColors[$result])) {
  192. return $this->get($result, $default);
  193. }
  194. return $result;
  195. }
  196. /**
  197. * 获取所有颜色.
  198. *
  199. * @return array
  200. */
  201. public function all()
  202. {
  203. if ($this->realColors === null) {
  204. foreach ($this->currentColors as $key => &$color) {
  205. $color = $this->get($key);
  206. }
  207. $this->realColors = &$this->currentColors;
  208. }
  209. return $this->realColors;
  210. }
  211. /**
  212. * 颜色转亮.
  213. *
  214. * @param string $color
  215. * @param int $amt
  216. *
  217. * @return string
  218. */
  219. public function lighten(string $color, int $amt)
  220. {
  221. return Helper::colorLighten($this->get($color, $color), $amt);
  222. }
  223. /**
  224. * 颜色转暗.
  225. *
  226. * @param string $color
  227. * @param int $amt
  228. *
  229. * @return string
  230. */
  231. public function darken(string $color, int $amt)
  232. {
  233. return Helper::colorDarken($this->get($color, $color), $amt);
  234. }
  235. /**
  236. * 颜色透明度转化.
  237. *
  238. * @param string $color
  239. * @param float|string $alpha
  240. *
  241. * @return string
  242. */
  243. public function alpha(string $color, $alpha)
  244. {
  245. return Helper::colorAlpha($this->get($color, $color), $alpha);
  246. }
  247. /**
  248. * 获取颜色.
  249. *
  250. * @param string $method
  251. * @param array $arguments
  252. *
  253. * @return string
  254. */
  255. public function __call(string $method, array $arguments = [])
  256. {
  257. return $this->darken(
  258. Helper::slug($method),
  259. $arguments[0] ?? 0
  260. );
  261. }
  262. /**
  263. * 扩展颜色.
  264. *
  265. * @param string $name
  266. * @param string|array $skinPath
  267. * @param array $colors
  268. *
  269. * @return void
  270. */
  271. public static function extend(string $name, $skinPath, array $colors)
  272. {
  273. static::$colors[$name] = [
  274. 'css' => array_filter((array) $skinPath),
  275. 'colors' => $colors,
  276. ];
  277. }
  278. }