1
0

Util.php 7.2 KB

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