Util.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 Julius Haertl <jus@bitgrid.net>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Michael Weimann <mail@michael-weimann.eu>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\Theming;
  29. use OCP\App\AppPathNotFoundException;
  30. use OCP\App\IAppManager;
  31. use OCP\Files\IAppData;
  32. use OCP\Files\NotFoundException;
  33. use OCP\Files\SimpleFS\ISimpleFile;
  34. use OCP\IConfig;
  35. use ScssPhp\ScssPhp\Compiler;
  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. * @param string $color rgb color value
  88. * @return float
  89. */
  90. public function calculateLuminance($color) {
  91. list($red, $green, $blue) = $this->hexToRGB($color);
  92. $compiler = new Compiler();
  93. $hsl = $compiler->toHSL($red, $green, $blue);
  94. return $hsl[3] / 100;
  95. }
  96. /**
  97. * @param string $color rgb color value
  98. * @return float
  99. */
  100. public function calculateLuma($color) {
  101. list($red, $green, $blue) = $this->hexToRGB($color);
  102. return (0.2126 * $red + 0.7152 * $green + 0.0722 * $blue) / 255;
  103. }
  104. /**
  105. * @param string $color rgb color value
  106. * @return int[]
  107. */
  108. public function hexToRGB($color) {
  109. $hex = preg_replace("/[^0-9A-Fa-f]/", '', $color);
  110. if (strlen($hex) === 3) {
  111. $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
  112. }
  113. if (strlen($hex) !== 6) {
  114. return 0;
  115. }
  116. return [
  117. hexdec(substr($hex, 0, 2)),
  118. hexdec(substr($hex, 2, 2)),
  119. hexdec(substr($hex, 4, 2))
  120. ];
  121. }
  122. /**
  123. * @param $color
  124. * @return string base64 encoded radio button svg
  125. */
  126. public function generateRadioButton($color) {
  127. $radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' .
  128. '<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>';
  129. return base64_encode($radioButtonIcon);
  130. }
  131. /**
  132. * @param $app string app name
  133. * @return string|ISimpleFile path to app icon / file of logo
  134. */
  135. public function getAppIcon($app) {
  136. $app = str_replace(['\0', '/', '\\', '..'], '', $app);
  137. try {
  138. $appPath = $this->appManager->getAppPath($app);
  139. $icon = $appPath . '/img/' . $app . '.svg';
  140. if (file_exists($icon)) {
  141. return $icon;
  142. }
  143. $icon = $appPath . '/img/app.svg';
  144. if (file_exists($icon)) {
  145. return $icon;
  146. }
  147. } catch (AppPathNotFoundException $e) {
  148. }
  149. if ($this->config->getAppValue('theming', 'logoMime', '') !== '') {
  150. $logoFile = null;
  151. try {
  152. $folder = $this->appData->getFolder('images');
  153. if ($folder !== null) {
  154. return $folder->getFile('logo');
  155. }
  156. } catch (NotFoundException $e) {
  157. }
  158. }
  159. return \OC::$SERVERROOT . '/core/img/logo/logo.svg';
  160. }
  161. /**
  162. * @param $app string app name
  163. * @param $image string relative path to image in app folder
  164. * @return string|false absolute path to image
  165. */
  166. public function getAppImage($app, $image) {
  167. $app = str_replace(['\0', '/', '\\', '..'], '', $app);
  168. $image = str_replace(['\0', '\\', '..'], '', $image);
  169. if ($app === "core") {
  170. $icon = \OC::$SERVERROOT . '/core/img/' . $image;
  171. if (file_exists($icon)) {
  172. return $icon;
  173. }
  174. }
  175. try {
  176. $appPath = $this->appManager->getAppPath($app);
  177. } catch (AppPathNotFoundException $e) {
  178. return false;
  179. }
  180. $icon = $appPath . '/img/' . $image;
  181. if (file_exists($icon)) {
  182. return $icon;
  183. }
  184. $icon = $appPath . '/img/' . $image . '.svg';
  185. if (file_exists($icon)) {
  186. return $icon;
  187. }
  188. $icon = $appPath . '/img/' . $image . '.png';
  189. if (file_exists($icon)) {
  190. return $icon;
  191. }
  192. $icon = $appPath . '/img/' . $image . '.gif';
  193. if (file_exists($icon)) {
  194. return $icon;
  195. }
  196. $icon = $appPath . '/img/' . $image . '.jpg';
  197. if (file_exists($icon)) {
  198. return $icon;
  199. }
  200. return false;
  201. }
  202. /**
  203. * replace default color with a custom one
  204. *
  205. * @param $svg string content of a svg file
  206. * @param $color string color to match
  207. * @return string
  208. */
  209. public function colorizeSvg($svg, $color) {
  210. $svg = preg_replace('/#0082c9/i', $color, $svg);
  211. return $svg;
  212. }
  213. /**
  214. * Check if a custom theme is set in the server configuration
  215. *
  216. * @return bool
  217. */
  218. public function isAlreadyThemed() {
  219. $theme = $this->config->getSystemValue('theme', '');
  220. if ($theme !== '') {
  221. return true;
  222. }
  223. return false;
  224. }
  225. public function isBackgroundThemed() {
  226. $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', '');
  227. return $backgroundLogo !== '' && $backgroundLogo !== 'backgroundColor';
  228. }
  229. }