Util.php 7.1 KB

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