-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Atum.php
245 lines (217 loc) · 5.53 KB
/
Atum.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* @package Joomla.Administrator
* @subpackage Templates.Atum
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\Registry\Registry;
use OzdemirBurak\Iris\Color\Hex;
use OzdemirBurak\Iris\Color\Hsl;
use OzdemirBurak\Iris\Color\Rgb;
use OzdemirBurak\Iris\Color\Hsla;
/**
* Template Atum HTML Helper
*
* @since 4.0.0
*/
class JHtmlAtum
{
/**
* Calculates the different template colors and set the CSS variables
*
* @param Registry $params Template parameters.
*
* @return void
*
* @since 4.0.0
*/
public static function rootcolors(Registry $params): void
{
$root = [];
if ($params->get('hue'))
{
$root = static::bgdarkcalc($params->get('hue'));
}
$bgLight = $params->get('bg-light');
if (static::isHex($bgLight))
{
try
{
$bgLight = new Hex($bgLight);
$root[] = '--atum-bg-light: ' . $bgLight . ';';
$root[] = '--toolbar-bg: ' . $bgLight->lighten(5) . ';';
}
catch (\Exception $ex)
{
// Just ignore exceptions
}
}
$textDark = $params->get('text-dark');
if (static::isHex($textDark))
{
try
{
$textDark = new Hex($textDark);
$root[] = '--atum-text-dark: ' . $textDark . ';';
}
catch (\Exception $ex)
{
// Just ignore exceptions
}
}
$textLight = $params->get('text-light');
if (static::isHex($textLight))
{
try
{
$textLight = new Hex($textLight);
$root[] = '--atum-text-light: ' . $textLight . ';';
}
catch (\Exception $ex)
{
// Just ignore exceptions
}
}
$linkColor = $params->get('link-color');
if (static::isHex($linkColor))
{
try
{
$linkColor = new Hex($linkColor);
$root[] = '--atum-link-color: ' . $linkColor . ';';
$root[] = '--atum-link-hover-color: ' . $linkColor->darken(20) . ';';
}
catch (\Exception $ex)
{
// Just ignore exceptions
}
}
$specialColor = $params->get('special-color');
if (static::isHex($specialColor))
{
try
{
$specialcolor = new Hex($specialColor);
$root[] = '--atum-special-color: ' . $specialcolor . ';';
}
catch (\Exception $ex)
{
// Just ignore exceptions
}
}
if (count($root))
{
/** @var Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wa->addInlineStyle(':root {' . implode($root) . '}');
}
}
/**
* Calculates the different template colors
*
* @param string $color Template parameter color.
* @param boolean $monochrome Template parameter monochrome.
*
* @return array An array of calculated color values and css variables
*
* @since 4.0.0
*/
protected static function bgdarkcalc($color): array
{
$hue = 207;
if (strpos($color, 'hsl(') !== false)
{
try
{
$hue = (new Hsl($color))->hue();
}
catch (\Exception $ex)
{
// Just ignore exceptions
}
}
elseif (static::isHex($color))
{
try
{
$hue = (new Hex($color))->toHsl()->hue();
}
catch (\Exception $ex)
{
// Just ignore exceptions
}
}
elseif (strpos($color, 'hsla(') !== false)
{
try
{
$hue = (new Hsla($color))->toHsl()->hue();
}
catch (\Exception $ex)
{
// Just ignore exceptions
}
}
elseif (strpos($color, 'rgb(') !== false)
{
try
{
$hue = (new Rgb($color))->toHsl()->hue();
}
catch (\Exception $ex)
{
// Just ignore exceptions
}
}
$hue = min(359, max(0, (int) $hue));
$root = [];
// No need to calculate if we have the default value
if ($hue === 214)
{
return $root;
}
try
{
$bgcolor = new Hsl('hsl(' . $hue . ', ' . 63 . ', 20)');
$root[] = '--atum-bg-dark: ' . (new Hsl('hsl(' . $hue . ', 63, 20)'))->toHex() . ';';
$root[] = '--atum-contrast: ' . (new Hsl('hsl(' . $hue . ', 63, 20)'))->spin(-40)->lighten(18)->toHex() . ';';
$root[] = '--atum-bg-dark-0: ' . (clone $bgcolor)->desaturate(86)->lighten(71.4)->spin(-6)->toHex() . ';';
$root[] = '--atum-bg-dark-5: ' . (clone $bgcolor)->desaturate(85)->lighten(65.1)->spin(-6)->toHex() . ';';
$root[] = '--atum-bg-dark-10: ' . (clone $bgcolor)->desaturate(80)->lighten(59.4)->spin(-6)->toHex() . ';';
$root[] = '--atum-bg-dark-20: ' . (clone $bgcolor)->desaturate(75)->lighten(47.3)->spin(-6)->toHex() . ';';
$root[] = '--atum-bg-dark-30: ' . (clone $bgcolor)->desaturate(55)->lighten(34.3)->spin(-5)->toHex() . ';';
$root[] = '--atum-bg-dark-40: ' . (clone $bgcolor)->desaturate(40)->lighten(21.4)->spin(-3)->toHex() . ';';
$root[] = '--atum-bg-dark-50: ' . (clone $bgcolor)->desaturate(15)->lighten(10)->spin(-1)->toHex() . ';';
$root[] = '--atum-bg-dark-70: ' . (clone $bgcolor)->lighten(-6)->spin(4)->toHex() . ';';
$root[] = '--atum-bg-dark-80: ' . (clone $bgcolor)->lighten(-11.5)->spin(7)->toHex() . ';';
$root[] = '--atum-bg-dark-90: ' . (clone $bgcolor)->desaturate(-1)->lighten(-17)->spin(10)->toHex() . ';';
}
catch (\Exception $ex)
{
// Just ignore exceptions
}
return $root;
}
/**
* Determinates if the given string is a color hex value
*
* @param string $hex The string to test
*
* @return boolean True when color hex value otherwise false
*
* @since 4.0.0
*/
protected static function isHex($hex): bool
{
if (substr($hex, 0, 1) !== '#')
{
return false;
}
$value = ltrim($hex, '#');
return (strlen($value) == 6 || strlen($value) == 3) && ctype_xdigit($value);
}
}