Color.php 7.5 KB

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