Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Util.php 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julien Veyssier <eneiluj@posteo.net>
  9. * @author Julius Haertl <jus@bitgrid.net>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Michael Weimann <mail@michael-weimann.eu>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\Theming;
  30. use Mexitek\PHPColors\Color;
  31. use OCP\App\AppPathNotFoundException;
  32. use OCP\App\IAppManager;
  33. use OCP\Files\IAppData;
  34. use OCP\Files\NotFoundException;
  35. use OCP\Files\SimpleFS\ISimpleFile;
  36. use OCP\IConfig;
  37. use OCP\IUserSession;
  38. class Util {
  39. private IConfig $config;
  40. private IAppManager $appManager;
  41. private IAppData $appData;
  42. private ImageManager $imageManager;
  43. public function __construct(IConfig $config, IAppManager $appManager, IAppData $appData, ImageManager $imageManager) {
  44. $this->config = $config;
  45. $this->appManager = $appManager;
  46. $this->appData = $appData;
  47. $this->imageManager = $imageManager;
  48. }
  49. /**
  50. * Should we invert the text on this background color?
  51. * @param string $color rgb color value
  52. * @return bool
  53. */
  54. public function invertTextColor(string $color): bool {
  55. return $this->colorContrast($color, '#ffffff') < 4.5;
  56. }
  57. /**
  58. * Is this color too bright ?
  59. * @param string $color rgb color value
  60. * @return bool
  61. */
  62. public function isBrightColor(string $color): bool {
  63. $l = $this->calculateLuma($color);
  64. if ($l > 0.6) {
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. }
  70. /**
  71. * get color for on-page elements:
  72. * theme color by default, grey if theme color is to bright
  73. * @param string $color
  74. * @param ?bool $brightBackground
  75. * @return string
  76. */
  77. public function elementColor($color, ?bool $brightBackground = null, ?string $backgroundColor = null, bool $highContrast = false) {
  78. if ($backgroundColor !== null) {
  79. $brightBackground = $brightBackground ?? $this->isBrightColor($backgroundColor);
  80. // Minimal amount that is possible to change the luminance
  81. $epsilon = 1.0 / 255.0;
  82. // Current iteration to prevent infinite loops
  83. $iteration = 0;
  84. // We need to keep blurred backgrounds in mind which might be mixed with the background
  85. $blurredBackground = $this->mix($backgroundColor, $brightBackground ? $color : '#ffffff', 66);
  86. $contrast = $this->colorContrast($color, $blurredBackground);
  87. // Min. element contrast is 3:1 but we need to keep hover states in mind -> min 3.2:1
  88. $minContrast = $highContrast ? 5.5 : 3.2;
  89. while ($contrast < $minContrast && $iteration++ < 100) {
  90. $hsl = Color::hexToHsl($color);
  91. $hsl['L'] = max(0, min(1, $hsl['L'] + ($brightBackground ? -$epsilon : $epsilon)));
  92. $color = '#' . Color::hslToHex($hsl);
  93. $contrast = $this->colorContrast($color, $blurredBackground);
  94. }
  95. return $color;
  96. }
  97. // Fallback for legacy calling
  98. $luminance = $this->calculateLuminance($color);
  99. if ($brightBackground !== false && $luminance > 0.8) {
  100. // If the color is too bright in bright mode, we fall back to a darkened color
  101. return $this->darken($color, 30);
  102. }
  103. if ($brightBackground !== true && $luminance < 0.2) {
  104. // If the color is too dark in dark mode, we fall back to a brightened color
  105. return $this->lighten($color, 30);
  106. }
  107. return $color;
  108. }
  109. public function mix(string $color1, string $color2, int $factor): string {
  110. $color = new Color($color1);
  111. return '#' . $color->mix($color2, $factor);
  112. }
  113. public function lighten(string $color, int $factor): string {
  114. $color = new Color($color);
  115. return '#' . $color->lighten($factor);
  116. }
  117. public function darken(string $color, int $factor): string {
  118. $color = new Color($color);
  119. return '#' . $color->darken($factor);
  120. }
  121. /**
  122. * Convert RGB to HSL
  123. *
  124. * Copied from cssphp, copyright Leaf Corcoran, licensed under MIT
  125. *
  126. * @param int $red
  127. * @param int $green
  128. * @param int $blue
  129. *
  130. * @return float[]
  131. */
  132. public function toHSL(int $red, int $green, int $blue): array {
  133. $color = new Color(Color::rgbToHex(['R' => $red, 'G' => $green, 'B' => $blue]));
  134. return array_values($color->getHsl());
  135. }
  136. /**
  137. * @param string $color rgb color value
  138. * @return float
  139. */
  140. public function calculateLuminance(string $color): float {
  141. [$red, $green, $blue] = $this->hexToRGB($color);
  142. $hsl = $this->toHSL($red, $green, $blue);
  143. return $hsl[2];
  144. }
  145. /**
  146. * Calculate the Luma according to WCAG 2
  147. * http://www.w3.org/TR/WCAG20/#relativeluminancedef
  148. * @param string $color rgb color value
  149. * @return float
  150. */
  151. public function calculateLuma(string $color): float {
  152. $rgb = $this->hexToRGB($color);
  153. // Normalize the values by converting to float and applying the rules from WCAG2.0
  154. $rgb = array_map(function (int $color) {
  155. $color = $color / 255.0;
  156. if ($color <= 0.03928) {
  157. return $color / 12.92;
  158. } else {
  159. return pow((($color + 0.055) / 1.055), 2.4);
  160. }
  161. }, $rgb);
  162. [$red, $green, $blue] = $rgb;
  163. return (0.2126 * $red + 0.7152 * $green + 0.0722 * $blue);
  164. }
  165. /**
  166. * Calculat the color contrast according to WCAG 2
  167. * http://www.w3.org/TR/WCAG20/#contrast-ratiodef
  168. * @param string $color1 The first color
  169. * @param string $color2 The second color
  170. */
  171. public function colorContrast(string $color1, string $color2): float {
  172. $luminance1 = $this->calculateLuma($color1) + 0.05;
  173. $luminance2 = $this->calculateLuma($color2) + 0.05;
  174. return max($luminance1, $luminance2) / min($luminance1, $luminance2);
  175. }
  176. /**
  177. * @param string $color rgb color value
  178. * @return int[]
  179. * @psalm-return array{0: int, 1: int, 2: int}
  180. */
  181. public function hexToRGB(string $color): array {
  182. $color = new Color($color);
  183. return array_values($color->getRgb());
  184. }
  185. /**
  186. * @param $color
  187. * @return string base64 encoded radio button svg
  188. */
  189. public function generateRadioButton($color) {
  190. $radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' .
  191. '<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>';
  192. return base64_encode($radioButtonIcon);
  193. }
  194. /**
  195. * @param string $app app name
  196. * @return string|ISimpleFile path to app icon / file of logo
  197. */
  198. public function getAppIcon($app) {
  199. $app = str_replace(['\0', '/', '\\', '..'], '', $app);
  200. try {
  201. $appPath = $this->appManager->getAppPath($app);
  202. $icon = $appPath . '/img/' . $app . '.svg';
  203. if (file_exists($icon)) {
  204. return $icon;
  205. }
  206. $icon = $appPath . '/img/app.svg';
  207. if (file_exists($icon)) {
  208. return $icon;
  209. }
  210. } catch (AppPathNotFoundException $e) {
  211. }
  212. if ($this->config->getAppValue('theming', 'logoMime', '') !== '') {
  213. $logoFile = null;
  214. try {
  215. $folder = $this->appData->getFolder('global/images');
  216. return $folder->getFile('logo');
  217. } catch (NotFoundException $e) {
  218. }
  219. }
  220. return \OC::$SERVERROOT . '/core/img/logo/logo.svg';
  221. }
  222. /**
  223. * @param string $app app name
  224. * @param string $image relative path to image in app folder
  225. * @return string|false absolute path to image
  226. */
  227. public function getAppImage($app, $image) {
  228. $app = str_replace(['\0', '/', '\\', '..'], '', $app);
  229. $image = str_replace(['\0', '\\', '..'], '', $image);
  230. if ($app === "core") {
  231. $icon = \OC::$SERVERROOT . '/core/img/' . $image;
  232. if (file_exists($icon)) {
  233. return $icon;
  234. }
  235. }
  236. try {
  237. $appPath = $this->appManager->getAppPath($app);
  238. } catch (AppPathNotFoundException $e) {
  239. return false;
  240. }
  241. $icon = $appPath . '/img/' . $image;
  242. if (file_exists($icon)) {
  243. return $icon;
  244. }
  245. $icon = $appPath . '/img/' . $image . '.svg';
  246. if (file_exists($icon)) {
  247. return $icon;
  248. }
  249. $icon = $appPath . '/img/' . $image . '.png';
  250. if (file_exists($icon)) {
  251. return $icon;
  252. }
  253. $icon = $appPath . '/img/' . $image . '.gif';
  254. if (file_exists($icon)) {
  255. return $icon;
  256. }
  257. $icon = $appPath . '/img/' . $image . '.jpg';
  258. if (file_exists($icon)) {
  259. return $icon;
  260. }
  261. return false;
  262. }
  263. /**
  264. * replace default color with a custom one
  265. *
  266. * @param string $svg content of a svg file
  267. * @param string $color color to match
  268. * @return string
  269. */
  270. public function colorizeSvg($svg, $color) {
  271. $svg = preg_replace('/#0082c9/i', $color, $svg);
  272. return $svg;
  273. }
  274. /**
  275. * Check if a custom theme is set in the server configuration
  276. *
  277. * @return bool
  278. */
  279. public function isAlreadyThemed() {
  280. $theme = $this->config->getSystemValue('theme', '');
  281. if ($theme !== '') {
  282. return true;
  283. }
  284. return false;
  285. }
  286. public function isBackgroundThemed() {
  287. $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', '');
  288. return $backgroundLogo !== '' && $backgroundLogo !== 'backgroundColor';
  289. }
  290. public function isLogoThemed() {
  291. return $this->imageManager->hasImage('logo')
  292. || $this->imageManager->hasImage('logoheader');
  293. }
  294. public function getCacheBuster(): string {
  295. $userSession = \OC::$server->get(IUserSession::class);
  296. $userId = '';
  297. $user = $userSession->getUser();
  298. if (!is_null($user)) {
  299. $userId = $user->getUID();
  300. }
  301. $userCacheBuster = '';
  302. if ($userId) {
  303. $userCacheBusterValue = (int)$this->config->getUserValue($userId, 'theming', 'userCacheBuster', '0');
  304. $userCacheBuster = $userId . '_' . $userCacheBusterValue;
  305. }
  306. $systemCacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
  307. return substr(sha1($userCacheBuster . $systemCacheBuster), 0, 8);
  308. }
  309. }