Color.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace Dcat\Admin;
  3. use Dcat\Admin\Support\Helper;
  4. use Illuminate\Support\Traits\Macroable;
  5. /**
  6. * Class Color.
  7. *
  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. * @method string background(int $amt = 0)
  56. * @method string darkModeBg(int $amt = 0)
  57. * @method string darkModeFont(int $amt = 0)
  58. * @method string darkModeColor(int $amt = 0)
  59. * @method string darkModeColor2(int $amt = 0)
  60. */
  61. class Color
  62. {
  63. use Macroable;
  64. const DEFAULT_COLOR = 'default';
  65. /**
  66. * 颜色.
  67. *
  68. * @var array
  69. */
  70. protected static $extensions = [
  71. 'default' => [
  72. 'colors' => [
  73. 'primary' => '#586cb1',
  74. 'primary-darker' => '#4c60a3',
  75. 'link' => '#4c60a3',
  76. ],
  77. ],
  78. 'blue-light' => [
  79. 'colors' => [
  80. 'primary' => '#62a8ea',
  81. 'primary-darker' => '#62a8ea',
  82. 'link' => '#62a8ea',
  83. ],
  84. ],
  85. 'blue' => [
  86. 'colors' => [
  87. 'primary' => '#6d8be6',
  88. 'primary-darker' => '#6d8be6',
  89. 'link' => '#6d8be6',
  90. ],
  91. ],
  92. 'green' => [
  93. 'colors' => [
  94. 'primary' => '#4e9876',
  95. 'primary-darker' => '#458769',
  96. 'link' => '#458769',
  97. ],
  98. ],
  99. ];
  100. /**
  101. * 默认颜色.
  102. *
  103. * @var array
  104. */
  105. protected static $allColors = [
  106. 'info' => 'blue',
  107. 'success' => 'green',
  108. 'danger' => 'red',
  109. 'warning' => 'orange',
  110. 'indigo' => '#5c6bc6',
  111. 'blue' => '#3085d6',
  112. 'red' => '#ea5455',
  113. 'orange' => '#dda451',
  114. 'green' => '#21b978',
  115. 'cyan' => '#7367f0',
  116. 'purple' => '#5b69bc',
  117. 'custom' => '#59a9f8',
  118. 'pink' => '#ff8acc',
  119. 'dark' => '#22292f',
  120. 'white' => '#fff',
  121. 'white50' => 'hsla(0,0%,100%,.5)',
  122. // 其他蓝色
  123. 'blue1' => '#007ee5',
  124. 'blue2' => '#3d97dd',
  125. // 橘色
  126. 'orange1' => '#ffcc80',
  127. 'orange2' => '#F99037',
  128. // 黄色
  129. 'yellow' => '#edc30e',
  130. 'indigo-darker' => '#495abf',
  131. 'red-darker' => '#bd4147',
  132. 'blue-darker' => '#236bb0',
  133. 'cyan-darker' => '#6355ee',
  134. // 灰色
  135. 'gray' => '#b9c3cd',
  136. // 轻灰
  137. 'light' => '#f7f7f9',
  138. // 水鸭色
  139. 'tear' => '#01847f',
  140. 'tear1' => '#00b5b5',
  141. // 深色
  142. 'dark20' => '#f6fbff',
  143. 'dark30' => '#f4f7fa',
  144. 'dark35' => '#e7eef7',
  145. 'dark40' => '#ebf0f3',
  146. 'dark50' => '#d3dde5',
  147. 'dark60' => '#bacad6',
  148. 'dark70' => '#b3b9bf',
  149. 'dark80' => '#7c858e',
  150. 'dark85' => '#5c7089',
  151. 'dark90' => '#252d37',
  152. // 文本通用颜色
  153. 'font' => '#414750',
  154. // 灰色背景
  155. 'gray-bg' => '#f1f1f1',
  156. // 边框颜色
  157. 'border' => '#ebeff2',
  158. // 表单边框
  159. 'input-border' => '#d9d9d9',
  160. // 背景色
  161. 'background' => '#eff3f8',
  162. // 深色模式
  163. // 背景色
  164. 'dark-mode-bg' => '#2c2c43',
  165. // 深色
  166. 'dark-mode-color' => '#222233',
  167. 'dark-mode-color2' => '#1e1e2d',
  168. 'dark-mode-font' => '##a8a9bb',
  169. ];
  170. /**
  171. * 主题名称.
  172. *
  173. * @var string
  174. */
  175. protected $name;
  176. /**
  177. * @var array
  178. */
  179. protected $colors = [];
  180. /**
  181. * @var array
  182. */
  183. protected $realColors;
  184. /**
  185. * 获取主题色名称.
  186. *
  187. * @return string
  188. */
  189. public function getName()
  190. {
  191. if (! $this->name) {
  192. $this->name = config('admin.layout.color') ?: static::DEFAULT_COLOR;
  193. }
  194. return $this->name;
  195. }
  196. /**
  197. * 设置主题色名称.
  198. *
  199. * @param string $name
  200. *
  201. * @return void
  202. */
  203. public function setName(string $name)
  204. {
  205. $this->name = $name;
  206. }
  207. /**
  208. * 获取颜色.
  209. *
  210. * @param string $colorName
  211. * @param string $default
  212. *
  213. * @return string
  214. */
  215. public function get(?string $colorName, ?string $default = null)
  216. {
  217. if ($this->realColors) {
  218. return $this->realColors[$colorName] ?? $default;
  219. }
  220. $colors = $this->getColors();
  221. $result = $colors[$colorName] ?? $default;
  222. if ($result && ! empty($colors[$result])) {
  223. return $this->get($result, $default);
  224. }
  225. return $result;
  226. }
  227. /**
  228. * 获取所有颜色.
  229. *
  230. * @return array
  231. */
  232. public function all()
  233. {
  234. if ($this->realColors === null) {
  235. $colors = $this->getColors();
  236. foreach ($colors as $key => &$color) {
  237. $color = $this->get($key);
  238. }
  239. $this->realColors = &$colors;
  240. }
  241. return $this->realColors;
  242. }
  243. /**
  244. * 颜色转亮.
  245. *
  246. * @param string $color
  247. * @param int $amt
  248. *
  249. * @return string
  250. */
  251. public function lighten(?string $color, int $amt)
  252. {
  253. return Helper::colorLighten($this->get($color, $color), $amt);
  254. }
  255. /**
  256. * 颜色转暗.
  257. *
  258. * @param string $color
  259. * @param int $amt
  260. *
  261. * @return string
  262. */
  263. public function darken(string $color, int $amt)
  264. {
  265. return Helper::colorDarken($this->get($color, $color), $amt);
  266. }
  267. /**
  268. * 颜色透明度转化.
  269. *
  270. * @param string $color
  271. * @param float|string $alpha
  272. *
  273. * @return string
  274. */
  275. public function alpha(?string $color, $alpha)
  276. {
  277. return Helper::colorAlpha($this->get($color, $color), $alpha);
  278. }
  279. /**
  280. * @return array
  281. */
  282. protected function getColors()
  283. {
  284. if (! $this->colors) {
  285. $this->colors = array_merge(
  286. static::$allColors,
  287. static::$extensions[$this->getName()]['colors'] ?? []
  288. );
  289. }
  290. return $this->colors;
  291. }
  292. /**
  293. * 获取颜色.
  294. *
  295. * @param string $method
  296. * @param array $arguments
  297. *
  298. * @return string
  299. */
  300. public function __call(string $method, array $arguments = [])
  301. {
  302. return $this->darken(
  303. Helper::slug($method),
  304. $arguments[0] ?? 0
  305. );
  306. }
  307. /**
  308. * 扩展颜色.
  309. *
  310. * @param string $name
  311. * @param array $colors
  312. *
  313. * @return void
  314. */
  315. public static function extend(string $name, array $colors)
  316. {
  317. static::$extensions[$name] = [
  318. 'colors' => $colors,
  319. ];
  320. }
  321. }