1+ <?php
2+
3+ /**
4+ * Get the initials from a name
5+ * It gets deleted if already configured
6+ *
7+ * @param string $name name to get initials from
8+ * @return string initials
9+ */
10+ if (!function_exists ('get_initials ' )) {
11+ function get_initials (string $ name ): string {
12+ $ normalized = preg_replace ('/[.@_\-]+/ ' , ' ' , trim ($ name ));
13+ $ initials = implode ('' , array_map (
14+ fn ($ part ) => mb_substr ($ part , 0 , 1 ),
15+ array_filter (explode (' ' , $ normalized ))
16+ ));
17+ return mb_strtoupper (mb_substr ($ initials , 0 , 2 ));
18+ }}
19+
20+ /**
21+ * Get a color for the avatar based on the id
22+ * It gets deleted if already configured
23+ *
24+ * @param string $id identifier to get a color from
25+ * @return string CSS gradient color
26+ */
27+ if (!hm_exists ('get_avatar_color ' )) {
28+ function get_avatar_color (string $ id ): string {
29+ $ colors = [
30+ 'linear-gradient(135deg, #3b82f6 0%, #1e40af 100%) ' ,
31+ 'linear-gradient(135deg, #328E92 0%, #1e5f5f 100%) ' ,
32+ 'linear-gradient(135deg, #10b981 0%, #065f46 100%) ' ,
33+ 'linear-gradient(135deg, #f59e0b 0%, #d97706 100%) ' ,
34+ 'linear-gradient(135deg, #8b5cf6 0%, #5b21b6 100%) ' ,
35+ 'linear-gradient(135deg, #ef4444 0%, #b91c1c 100%) ' ,
36+ 'linear-gradient(135deg, #06b6d4 0%, #0891b2 100%) ' ,
37+ 'linear-gradient(135deg, #84cc16 0%, #65a30d 100%) ' ,
38+ ];
39+ $ hash = is_numeric ($ id ) ? intval ($ id ) : crc32 ($ id );
40+ return $ colors [$ hash % count ($ colors )];
41+ }}
0 commit comments