Color.php 7.7 KB

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