@@ -38,218 +38,218 @@ |
||
38 | 38 | |
39 | 39 | class Util { |
40 | 40 | |
41 | - /** @var IConfig */ |
|
42 | - private $config; |
|
43 | - |
|
44 | - /** @var IAppManager */ |
|
45 | - private $appManager; |
|
46 | - |
|
47 | - /** @var IAppData */ |
|
48 | - private $appData; |
|
49 | - |
|
50 | - /** |
|
51 | - * Util constructor. |
|
52 | - * |
|
53 | - * @param IConfig $config |
|
54 | - * @param IAppManager $appManager |
|
55 | - * @param IAppData $appData |
|
56 | - */ |
|
57 | - public function __construct(IConfig $config, IAppManager $appManager, IAppData $appData) { |
|
58 | - $this->config = $config; |
|
59 | - $this->appManager = $appManager; |
|
60 | - $this->appData = $appData; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @param string $color rgb color value |
|
65 | - * @return bool |
|
66 | - */ |
|
67 | - public function invertTextColor($color) { |
|
68 | - $l = $this->calculateLuma($color); |
|
69 | - if ($l > 0.6) { |
|
70 | - return true; |
|
71 | - } else { |
|
72 | - return false; |
|
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * get color for on-page elements: |
|
78 | - * theme color by default, grey if theme color is to bright |
|
79 | - * @param string $color |
|
80 | - * @param bool $brightBackground |
|
81 | - * @return string |
|
82 | - */ |
|
83 | - public function elementColor($color, bool $brightBackground = true) { |
|
84 | - $luminance = $this->calculateLuminance($color); |
|
85 | - |
|
86 | - if ($brightBackground && $luminance > 0.8) { |
|
87 | - // If the color is too bright in bright mode, we fall back to a darker gray |
|
88 | - return '#aaaaaa'; |
|
89 | - } |
|
90 | - |
|
91 | - if (!$brightBackground && $luminance < 0.2) { |
|
92 | - // If the color is too dark in dark mode, we fall back to a brighter gray |
|
93 | - return '#555555'; |
|
94 | - } |
|
95 | - |
|
96 | - return $color; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @param string $color rgb color value |
|
101 | - * @return float |
|
102 | - */ |
|
103 | - public function calculateLuminance($color) { |
|
104 | - list($red, $green, $blue) = $this->hexToRGB($color); |
|
105 | - $compiler = new Compiler(); |
|
106 | - $hsl = $compiler->toHSL($red, $green, $blue); |
|
107 | - return $hsl[3] / 100; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @param string $color rgb color value |
|
112 | - * @return float |
|
113 | - */ |
|
114 | - public function calculateLuma($color) { |
|
115 | - list($red, $green, $blue) = $this->hexToRGB($color); |
|
116 | - return (0.2126 * $red + 0.7152 * $green + 0.0722 * $blue) / 255; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @param string $color rgb color value |
|
121 | - * @return int[] |
|
122 | - */ |
|
123 | - public function hexToRGB($color) { |
|
124 | - $hex = preg_replace("/[^0-9A-Fa-f]/", '', $color); |
|
125 | - if (strlen($hex) === 3) { |
|
126 | - $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
|
127 | - } |
|
128 | - if (strlen($hex) !== 6) { |
|
129 | - return 0; |
|
130 | - } |
|
131 | - return [ |
|
132 | - hexdec(substr($hex, 0, 2)), |
|
133 | - hexdec(substr($hex, 2, 2)), |
|
134 | - hexdec(substr($hex, 4, 2)) |
|
135 | - ]; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @param $color |
|
140 | - * @return string base64 encoded radio button svg |
|
141 | - */ |
|
142 | - public function generateRadioButton($color) { |
|
143 | - $radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' . |
|
144 | - '<path d="M8 1a7 7 0 0 0-7 7 7 7 0 0 0 7 7 7 7 0 0 0 7-7 7 7 0 0 0-7-7zm0 1a6 6 0 0 1 6 6 6 6 0 0 1-6 6 6 6 0 0 1-6-6 6 6 0 0 1 6-6zm0 2a4 4 0 1 0 0 8 4 4 0 0 0 0-8z" fill="'.$color.'"/></svg>'; |
|
145 | - return base64_encode($radioButtonIcon); |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * @param $app string app name |
|
151 | - * @return string|ISimpleFile path to app icon / file of logo |
|
152 | - */ |
|
153 | - public function getAppIcon($app) { |
|
154 | - $app = str_replace(['\0', '/', '\\', '..'], '', $app); |
|
155 | - try { |
|
156 | - $appPath = $this->appManager->getAppPath($app); |
|
157 | - $icon = $appPath . '/img/' . $app . '.svg'; |
|
158 | - if (file_exists($icon)) { |
|
159 | - return $icon; |
|
160 | - } |
|
161 | - $icon = $appPath . '/img/app.svg'; |
|
162 | - if (file_exists($icon)) { |
|
163 | - return $icon; |
|
164 | - } |
|
165 | - } catch (AppPathNotFoundException $e) { |
|
166 | - } |
|
167 | - |
|
168 | - if ($this->config->getAppValue('theming', 'logoMime', '') !== '') { |
|
169 | - $logoFile = null; |
|
170 | - try { |
|
171 | - $folder = $this->appData->getFolder('images'); |
|
172 | - if ($folder !== null) { |
|
173 | - return $folder->getFile('logo'); |
|
174 | - } |
|
175 | - } catch (NotFoundException $e) { |
|
176 | - } |
|
177 | - } |
|
178 | - return \OC::$SERVERROOT . '/core/img/logo/logo.svg'; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * @param $app string app name |
|
183 | - * @param $image string relative path to image in app folder |
|
184 | - * @return string|false absolute path to image |
|
185 | - */ |
|
186 | - public function getAppImage($app, $image) { |
|
187 | - $app = str_replace(['\0', '/', '\\', '..'], '', $app); |
|
188 | - $image = str_replace(['\0', '\\', '..'], '', $image); |
|
189 | - if ($app === "core") { |
|
190 | - $icon = \OC::$SERVERROOT . '/core/img/' . $image; |
|
191 | - if (file_exists($icon)) { |
|
192 | - return $icon; |
|
193 | - } |
|
194 | - } |
|
195 | - |
|
196 | - try { |
|
197 | - $appPath = $this->appManager->getAppPath($app); |
|
198 | - } catch (AppPathNotFoundException $e) { |
|
199 | - return false; |
|
200 | - } |
|
201 | - |
|
202 | - $icon = $appPath . '/img/' . $image; |
|
203 | - if (file_exists($icon)) { |
|
204 | - return $icon; |
|
205 | - } |
|
206 | - $icon = $appPath . '/img/' . $image . '.svg'; |
|
207 | - if (file_exists($icon)) { |
|
208 | - return $icon; |
|
209 | - } |
|
210 | - $icon = $appPath . '/img/' . $image . '.png'; |
|
211 | - if (file_exists($icon)) { |
|
212 | - return $icon; |
|
213 | - } |
|
214 | - $icon = $appPath . '/img/' . $image . '.gif'; |
|
215 | - if (file_exists($icon)) { |
|
216 | - return $icon; |
|
217 | - } |
|
218 | - $icon = $appPath . '/img/' . $image . '.jpg'; |
|
219 | - if (file_exists($icon)) { |
|
220 | - return $icon; |
|
221 | - } |
|
222 | - |
|
223 | - return false; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * replace default color with a custom one |
|
228 | - * |
|
229 | - * @param $svg string content of a svg file |
|
230 | - * @param $color string color to match |
|
231 | - * @return string |
|
232 | - */ |
|
233 | - public function colorizeSvg($svg, $color) { |
|
234 | - $svg = preg_replace('/#0082c9/i', $color, $svg); |
|
235 | - return $svg; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Check if a custom theme is set in the server configuration |
|
240 | - * |
|
241 | - * @return bool |
|
242 | - */ |
|
243 | - public function isAlreadyThemed() { |
|
244 | - $theme = $this->config->getSystemValue('theme', ''); |
|
245 | - if ($theme !== '') { |
|
246 | - return true; |
|
247 | - } |
|
248 | - return false; |
|
249 | - } |
|
250 | - |
|
251 | - public function isBackgroundThemed() { |
|
252 | - $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', ''); |
|
253 | - return $backgroundLogo !== '' && $backgroundLogo !== 'backgroundColor'; |
|
254 | - } |
|
41 | + /** @var IConfig */ |
|
42 | + private $config; |
|
43 | + |
|
44 | + /** @var IAppManager */ |
|
45 | + private $appManager; |
|
46 | + |
|
47 | + /** @var IAppData */ |
|
48 | + private $appData; |
|
49 | + |
|
50 | + /** |
|
51 | + * Util constructor. |
|
52 | + * |
|
53 | + * @param IConfig $config |
|
54 | + * @param IAppManager $appManager |
|
55 | + * @param IAppData $appData |
|
56 | + */ |
|
57 | + public function __construct(IConfig $config, IAppManager $appManager, IAppData $appData) { |
|
58 | + $this->config = $config; |
|
59 | + $this->appManager = $appManager; |
|
60 | + $this->appData = $appData; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @param string $color rgb color value |
|
65 | + * @return bool |
|
66 | + */ |
|
67 | + public function invertTextColor($color) { |
|
68 | + $l = $this->calculateLuma($color); |
|
69 | + if ($l > 0.6) { |
|
70 | + return true; |
|
71 | + } else { |
|
72 | + return false; |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * get color for on-page elements: |
|
78 | + * theme color by default, grey if theme color is to bright |
|
79 | + * @param string $color |
|
80 | + * @param bool $brightBackground |
|
81 | + * @return string |
|
82 | + */ |
|
83 | + public function elementColor($color, bool $brightBackground = true) { |
|
84 | + $luminance = $this->calculateLuminance($color); |
|
85 | + |
|
86 | + if ($brightBackground && $luminance > 0.8) { |
|
87 | + // If the color is too bright in bright mode, we fall back to a darker gray |
|
88 | + return '#aaaaaa'; |
|
89 | + } |
|
90 | + |
|
91 | + if (!$brightBackground && $luminance < 0.2) { |
|
92 | + // If the color is too dark in dark mode, we fall back to a brighter gray |
|
93 | + return '#555555'; |
|
94 | + } |
|
95 | + |
|
96 | + return $color; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @param string $color rgb color value |
|
101 | + * @return float |
|
102 | + */ |
|
103 | + public function calculateLuminance($color) { |
|
104 | + list($red, $green, $blue) = $this->hexToRGB($color); |
|
105 | + $compiler = new Compiler(); |
|
106 | + $hsl = $compiler->toHSL($red, $green, $blue); |
|
107 | + return $hsl[3] / 100; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @param string $color rgb color value |
|
112 | + * @return float |
|
113 | + */ |
|
114 | + public function calculateLuma($color) { |
|
115 | + list($red, $green, $blue) = $this->hexToRGB($color); |
|
116 | + return (0.2126 * $red + 0.7152 * $green + 0.0722 * $blue) / 255; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @param string $color rgb color value |
|
121 | + * @return int[] |
|
122 | + */ |
|
123 | + public function hexToRGB($color) { |
|
124 | + $hex = preg_replace("/[^0-9A-Fa-f]/", '', $color); |
|
125 | + if (strlen($hex) === 3) { |
|
126 | + $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
|
127 | + } |
|
128 | + if (strlen($hex) !== 6) { |
|
129 | + return 0; |
|
130 | + } |
|
131 | + return [ |
|
132 | + hexdec(substr($hex, 0, 2)), |
|
133 | + hexdec(substr($hex, 2, 2)), |
|
134 | + hexdec(substr($hex, 4, 2)) |
|
135 | + ]; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @param $color |
|
140 | + * @return string base64 encoded radio button svg |
|
141 | + */ |
|
142 | + public function generateRadioButton($color) { |
|
143 | + $radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' . |
|
144 | + '<path d="M8 1a7 7 0 0 0-7 7 7 7 0 0 0 7 7 7 7 0 0 0 7-7 7 7 0 0 0-7-7zm0 1a6 6 0 0 1 6 6 6 6 0 0 1-6 6 6 6 0 0 1-6-6 6 6 0 0 1 6-6zm0 2a4 4 0 1 0 0 8 4 4 0 0 0 0-8z" fill="'.$color.'"/></svg>'; |
|
145 | + return base64_encode($radioButtonIcon); |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * @param $app string app name |
|
151 | + * @return string|ISimpleFile path to app icon / file of logo |
|
152 | + */ |
|
153 | + public function getAppIcon($app) { |
|
154 | + $app = str_replace(['\0', '/', '\\', '..'], '', $app); |
|
155 | + try { |
|
156 | + $appPath = $this->appManager->getAppPath($app); |
|
157 | + $icon = $appPath . '/img/' . $app . '.svg'; |
|
158 | + if (file_exists($icon)) { |
|
159 | + return $icon; |
|
160 | + } |
|
161 | + $icon = $appPath . '/img/app.svg'; |
|
162 | + if (file_exists($icon)) { |
|
163 | + return $icon; |
|
164 | + } |
|
165 | + } catch (AppPathNotFoundException $e) { |
|
166 | + } |
|
167 | + |
|
168 | + if ($this->config->getAppValue('theming', 'logoMime', '') !== '') { |
|
169 | + $logoFile = null; |
|
170 | + try { |
|
171 | + $folder = $this->appData->getFolder('images'); |
|
172 | + if ($folder !== null) { |
|
173 | + return $folder->getFile('logo'); |
|
174 | + } |
|
175 | + } catch (NotFoundException $e) { |
|
176 | + } |
|
177 | + } |
|
178 | + return \OC::$SERVERROOT . '/core/img/logo/logo.svg'; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * @param $app string app name |
|
183 | + * @param $image string relative path to image in app folder |
|
184 | + * @return string|false absolute path to image |
|
185 | + */ |
|
186 | + public function getAppImage($app, $image) { |
|
187 | + $app = str_replace(['\0', '/', '\\', '..'], '', $app); |
|
188 | + $image = str_replace(['\0', '\\', '..'], '', $image); |
|
189 | + if ($app === "core") { |
|
190 | + $icon = \OC::$SERVERROOT . '/core/img/' . $image; |
|
191 | + if (file_exists($icon)) { |
|
192 | + return $icon; |
|
193 | + } |
|
194 | + } |
|
195 | + |
|
196 | + try { |
|
197 | + $appPath = $this->appManager->getAppPath($app); |
|
198 | + } catch (AppPathNotFoundException $e) { |
|
199 | + return false; |
|
200 | + } |
|
201 | + |
|
202 | + $icon = $appPath . '/img/' . $image; |
|
203 | + if (file_exists($icon)) { |
|
204 | + return $icon; |
|
205 | + } |
|
206 | + $icon = $appPath . '/img/' . $image . '.svg'; |
|
207 | + if (file_exists($icon)) { |
|
208 | + return $icon; |
|
209 | + } |
|
210 | + $icon = $appPath . '/img/' . $image . '.png'; |
|
211 | + if (file_exists($icon)) { |
|
212 | + return $icon; |
|
213 | + } |
|
214 | + $icon = $appPath . '/img/' . $image . '.gif'; |
|
215 | + if (file_exists($icon)) { |
|
216 | + return $icon; |
|
217 | + } |
|
218 | + $icon = $appPath . '/img/' . $image . '.jpg'; |
|
219 | + if (file_exists($icon)) { |
|
220 | + return $icon; |
|
221 | + } |
|
222 | + |
|
223 | + return false; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * replace default color with a custom one |
|
228 | + * |
|
229 | + * @param $svg string content of a svg file |
|
230 | + * @param $color string color to match |
|
231 | + * @return string |
|
232 | + */ |
|
233 | + public function colorizeSvg($svg, $color) { |
|
234 | + $svg = preg_replace('/#0082c9/i', $color, $svg); |
|
235 | + return $svg; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Check if a custom theme is set in the server configuration |
|
240 | + * |
|
241 | + * @return bool |
|
242 | + */ |
|
243 | + public function isAlreadyThemed() { |
|
244 | + $theme = $this->config->getSystemValue('theme', ''); |
|
245 | + if ($theme !== '') { |
|
246 | + return true; |
|
247 | + } |
|
248 | + return false; |
|
249 | + } |
|
250 | + |
|
251 | + public function isBackgroundThemed() { |
|
252 | + $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', ''); |
|
253 | + return $backgroundLogo !== '' && $backgroundLogo !== 'backgroundColor'; |
|
254 | + } |
|
255 | 255 | } |