summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2022-06-23 11:42:14 +0200
committerCarl Schwan <carl@carlschwan.eu>2022-07-05 14:44:44 +0200
commitec5cbdeb7ffb87c0169c39e6f44846e819b41f14 (patch)
tree275f11c139076fbcb60cc39c8ce609dfd3f41f82 /lib
parent812016d62614ac3669a3fdf51fa2e07170e08c08 (diff)
downloadnextcloud-server-ec5cbdeb7ffb87c0169c39e6f44846e819b41f14.tar.gz
nextcloud-server-ec5cbdeb7ffb87c0169c39e6f44846e819b41f14.zip
Make Color class public
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Diffstat (limited to 'lib')
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/private/Avatar/Avatar.php58
-rw-r--r--lib/private/Avatar/GuestAvatar.php3
-rw-r--r--lib/private/Avatar/UserAvatar.php2
-rw-r--r--lib/private/User/Listeners/UserChangedListener.php2
-rw-r--r--lib/private/User/Listeners/UserDeletedListener.php8
-rw-r--r--lib/public/Color.php142
-rw-r--r--lib/public/IAvatar.php3
-rw-r--r--lib/public/IAvatarManager.php7
10 files changed, 169 insertions, 58 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 646bd3acb5e..bdfa9ecff61 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -150,6 +150,7 @@ return array(
'OCP\\Collaboration\\Resources\\IResource' => $baseDir . '/lib/public/Collaboration/Resources/IResource.php',
'OCP\\Collaboration\\Resources\\LoadAdditionalScriptsEvent' => $baseDir . '/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php',
'OCP\\Collaboration\\Resources\\ResourceException' => $baseDir . '/lib/public/Collaboration/Resources/ResourceException.php',
+ 'OCP\\Color' => $baseDir . '/lib/public/Color.php',
'OCP\\Command\\IBus' => $baseDir . '/lib/public/Command/IBus.php',
'OCP\\Command\\ICommand' => $baseDir . '/lib/public/Command/ICommand.php',
'OCP\\Comments\\CommentsEntityEvent' => $baseDir . '/lib/public/Comments/CommentsEntityEvent.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index 75d00eb32c7..740c2fe1fc8 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -183,6 +183,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Collaboration\\Resources\\IResource' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IResource.php',
'OCP\\Collaboration\\Resources\\LoadAdditionalScriptsEvent' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php',
'OCP\\Collaboration\\Resources\\ResourceException' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/ResourceException.php',
+ 'OCP\\Color' => __DIR__ . '/../../..' . '/lib/public/Color.php',
'OCP\\Command\\IBus' => __DIR__ . '/../../..' . '/lib/public/Command/IBus.php',
'OCP\\Command\\ICommand' => __DIR__ . '/../../..' . '/lib/public/Command/ICommand.php',
'OCP\\Comments\\CommentsEntityEvent' => __DIR__ . '/../../..' . '/lib/public/Comments/CommentsEntityEvent.php',
diff --git a/lib/private/Avatar/Avatar.php b/lib/private/Avatar/Avatar.php
index 188e7378aff..25099a4f139 100644
--- a/lib/private/Avatar/Avatar.php
+++ b/lib/private/Avatar/Avatar.php
@@ -37,7 +37,7 @@ declare(strict_types=1);
namespace OC\Avatar;
use Imagick;
-use OC\Color;
+use OCP\Color;
use OCP\Files\NotFoundException;
use OCP\IAvatar;
use Psr\Log\LoggerInterface;
@@ -114,7 +114,7 @@ abstract class Avatar implements IAvatar {
protected function getAvatarVector(int $size): string {
$userDisplayName = $this->getDisplayName();
$bgRGB = $this->avatarBackgroundColor($userDisplayName);
- $bgHEX = sprintf("%02x%02x%02x", $bgRGB->r, $bgRGB->g, $bgRGB->b);
+ $bgHEX = sprintf("%02x%02x%02x", $bgRGB->red(), $bgRGB->green(), $bgRGB->blue());
$text = $this->getAvatarText();
$toReplace = ['{size}', '{fill}', '{letter}'];
return str_replace($toReplace, [$size, $bgHEX, $text], $this->svgTemplate);
@@ -122,13 +122,10 @@ abstract class Avatar implements IAvatar {
/**
* Generate png avatar from svg with Imagick
- *
- * @param int $size
- * @return string|boolean
*/
- protected function generateAvatarFromSvg(int $size) {
+ protected function generateAvatarFromSvg(int $size): ?string {
if (!extension_loaded('imagick')) {
- return false;
+ return null;
}
try {
$font = __DIR__ . '/../../core/fonts/NotoSans-Regular.ttf';
@@ -139,10 +136,9 @@ abstract class Avatar implements IAvatar {
$avatar->setImageFormat('png');
$image = new \OCP\Image();
$image->loadFromData((string)$avatar);
- $data = $image->data();
- return $data === null ? false : $data;
+ return $image->data();
} catch (\Exception $e) {
- return false;
+ return null;
}
}
@@ -156,9 +152,9 @@ abstract class Avatar implements IAvatar {
$im = imagecreatetruecolor($size, $size);
$background = imagecolorallocate(
$im,
- $backgroundColor->r,
- $backgroundColor->g,
- $backgroundColor->b
+ $backgroundColor->red(),
+ $backgroundColor->green(),
+ $backgroundColor->blue()
);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, $size, $size, $background);
@@ -215,36 +211,6 @@ abstract class Avatar implements IAvatar {
return [$x, $y];
}
- /**
- * Calculate steps between two Colors
- * @param int $steps start color
- * @param Color[] $ends end color
- * @return array{0: int, 1: int, 2: int} [r,g,b] steps for each color to go from $steps to $ends
- */
- private function stepCalc(int $steps, array $ends): array {
- $step = [];
- $step[0] = ($ends[1]->r - $ends[0]->r) / $steps;
- $step[1] = ($ends[1]->g - $ends[0]->g) / $steps;
- $step[2] = ($ends[1]->b - $ends[0]->b) / $steps;
- return $step;
- }
-
- /**
- * Mix two colors
- *
- * @return Color[]
- */
- private function mixPalette($steps, Color $color1, Color $color2) {
- $palette = [$color1];
- $step = $this->stepCalc($steps, [$color1, $color2]);
- for ($i = 1; $i < $steps; $i++) {
- $r = intval($color1->r + ($step[0] * $i));
- $g = intval($color1->g + ($step[1] * $i));
- $b = intval($color1->b + ($step[2] * $i));
- $palette[] = new Color($r, $g, $b);
- }
- return $palette;
- }
/**
* Convert a string to an integer evenly
@@ -292,9 +258,9 @@ abstract class Avatar implements IAvatar {
// 3 colors * 6 will result in 18 generated colors
$steps = 6;
- $palette1 = $this->mixPalette($steps, $red, $yellow);
- $palette2 = $this->mixPalette($steps, $yellow, $blue);
- $palette3 = $this->mixPalette($steps, $blue, $red);
+ $palette1 = Color::mixPalette($steps, $red, $yellow);
+ $palette2 = Color::mixPalette($steps, $yellow, $blue);
+ $palette3 = Color::mixPalette($steps, $blue, $red);
$finalPalette = array_merge($palette1, $palette2, $palette3);
diff --git a/lib/private/Avatar/GuestAvatar.php b/lib/private/Avatar/GuestAvatar.php
index f3dde0840e9..79d7e6ee094 100644
--- a/lib/private/Avatar/GuestAvatar.php
+++ b/lib/private/Avatar/GuestAvatar.php
@@ -53,7 +53,8 @@ class GuestAvatar extends Avatar {
* Tests if the user has an avatar.
*/
public function exists(): bool {
- return true; // Guests always have an avatar.
+ // Guests always have an avatar.
+ return true;
}
/**
diff --git a/lib/private/Avatar/UserAvatar.php b/lib/private/Avatar/UserAvatar.php
index 191a6ebfff4..f5a1d7e77b1 100644
--- a/lib/private/Avatar/UserAvatar.php
+++ b/lib/private/Avatar/UserAvatar.php
@@ -192,7 +192,7 @@ class UserAvatar extends Avatar {
$avatars = $this->folder->getDirectoryListing();
$this->config->setUserValue($this->user->getUID(), 'avatar', 'version',
- (int) $this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1);
+ (string)((int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', '0') + 1));
foreach ($avatars as $avatar) {
$avatar->delete();
diff --git a/lib/private/User/Listeners/UserChangedListener.php b/lib/private/User/Listeners/UserChangedListener.php
index f3c7468460a..a561db2423d 100644
--- a/lib/private/User/Listeners/UserChangedListener.php
+++ b/lib/private/User/Listeners/UserChangedListener.php
@@ -30,7 +30,7 @@ use OCP\Files\NotFoundException;
use OCP\IAvatarManager;
/**
- * @template-implements IEventListener<\OCP\User\Events\UserChangedEvent>
+ * @template-implements IEventListener<UserChangedEvent>
*/
class UserChangedListener implements IEventListener {
private IAvatarManager $avatarManager;
diff --git a/lib/private/User/Listeners/UserDeletedListener.php b/lib/private/User/Listeners/UserDeletedListener.php
index a8ad56d97ab..7c9c46ef371 100644
--- a/lib/private/User/Listeners/UserDeletedListener.php
+++ b/lib/private/User/Listeners/UserDeletedListener.php
@@ -31,7 +31,7 @@ use OCP\IAvatarManager;
use Psr\Log\LoggerInterface;
/**
- * @template-implements IEventListener<\OCP\User\Events\UserDeletedEvent>
+ * @template-implements IEventListener<UserDeletedEvent>
*/
class UserDeletedListener implements IEventListener {
private IAvatarManager $avatarManager;
@@ -52,12 +52,14 @@ class UserDeletedListener implements IEventListener {
// Delete avatar on user deletion
try {
$avatar = $this->avatarManager->getAvatar($user->getUID());
- $avatar->remove();
+ $avatar->remove(true);
} catch (NotFoundException $e) {
// no avatar to remove
} catch (\Exception $e) {
// Ignore exceptions
- $this->logger->info('Could not cleanup avatar of ' . $user->getUID());
+ $this->logger->info('Could not cleanup avatar of ' . $user->getUID(), [
+ 'exception' => $e,
+ ]);
}
}
}
diff --git a/lib/public/Color.php b/lib/public/Color.php
new file mode 100644
index 00000000000..e2cabd9c2fc
--- /dev/null
+++ b/lib/public/Color.php
@@ -0,0 +1,142 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016, John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @author Christoph Wurst <christoph@winzerhof-wurst.at>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+namespace OCP;
+
+/**
+ * Simple RGB color container
+ * @since 25.0.0
+ */
+class Color {
+ private int $r;
+ private int $g;
+ private int $b;
+
+ /**
+ * @since 25.0.0
+ */
+ public function __construct($r, $g, $b) {
+ $this->r = $r;
+ $this->g = $g;
+ $this->b = $b;
+ }
+
+ /**
+ * Returns the red color component of this color as an int from 0 to 255
+ *
+ * @since 25.0.0
+ */
+ public function red(): int {
+ return $this->r;
+ }
+
+ /**
+ * Returns the red color component of this color as a float from 0 to 1
+ *
+ * @since 25.0.0
+ */
+ public function redF(): float {
+ return $this->r / 255;
+ }
+
+ /**
+ * Returns the green color component of this color as an int from 0 to 255
+ *
+ * @since 25.0.0
+ */
+ public function green(): int {
+ return $this->g;
+ }
+
+ /**
+ * Returns the green color component of this color as a float from 0 to 1
+ *
+ * @since 25.0.0
+ */
+ public function greenF(): float {
+ return $this->g / 255;
+ }
+
+ /**
+ * Returns the green blue component of this color as an int from 0 to 255
+ *
+ * @since 25.0.0
+ */
+ public function blue(): int {
+ return $this->b;
+ }
+
+ /**
+ * Returns the blue color component of this color as a float from 0 to 1
+ *
+ * @since 25.0.0
+ */
+ public function blueF(): float {
+ return $this->g / 255;
+ }
+
+ /**
+ * Returns the name of the color in the format "#RRGGBB"; i.e. a "#" character followed by three two-digit hexadecimal numbers.
+ *
+ * @since 25.0.0
+ */
+ public function name(): string {
+ return sprintf("#%02x%02x%02x", $this->r, $this->g, $this->b);
+ }
+
+ /**
+ * Mix two colors
+ *
+ * @param int $steps the number of intermediate colors that should be generated for the palette
+ * @param Color $color1 the first color
+ * @param Color $color2 the second color
+ * @return list<Color>
+ * @since 25.0.0
+ */
+ public static function mixPalette(int $steps, Color $color1, Color $color2): array {
+ $palette = [$color1];
+ $step = self::stepCalc($steps, [$color1, $color2]);
+ for ($i = 1; $i < $steps; $i++) {
+ $r = intval($color1->red() + ($step[0] * $i));
+ $g = intval($color1->green() + ($step[1] * $i));
+ $b = intval($color1->blue() + ($step[2] * $i));
+ $palette[] = new Color($r, $g, $b);
+ }
+ return $palette;
+ }
+
+ /**
+ * Calculate steps between two Colors
+ * @param int $steps start color
+ * @param Color[] $ends end color
+ * @return array{0: int, 1: int, 2: int} [r,g,b] steps for each color to go from $steps to $ends
+ * @since 25.0.0
+ */
+ private static function stepCalc(int $steps, array $ends): array {
+ $step = [];
+ $step[0] = ($ends[1]->red() - $ends[0]->red()) / $steps;
+ $step[1] = ($ends[1]->green() - $ends[0]->green()) / $steps;
+ $step[2] = ($ends[1]->blue() - $ends[0]->blue()) / $steps;
+ return $step;
+ }
+}
diff --git a/lib/public/IAvatar.php b/lib/public/IAvatar.php
index e4a9ce5dcc1..d05a12e1dbf 100644
--- a/lib/public/IAvatar.php
+++ b/lib/public/IAvatar.php
@@ -89,10 +89,9 @@ interface IAvatar {
/**
* Get the avatar background color
*
- * @return Color Object containting r g b int in the range [0, 255]
* @since 14.0.0
*/
- public function avatarBackgroundColor(string $text);
+ public function avatarBackgroundColor(string $hash): Color;
/**
* Updates the display name if changed.
diff --git a/lib/public/IAvatarManager.php b/lib/public/IAvatarManager.php
index 573e109f003..15894550d10 100644
--- a/lib/public/IAvatarManager.php
+++ b/lib/public/IAvatarManager.php
@@ -37,15 +37,14 @@ namespace OCP;
interface IAvatarManager {
/**
- * return a user specific instance of \OCP\IAvatar
+ * Return a user specific instance of \OCP\IAvatar
* @see IAvatar
- * @param string $user the ownCloud user id
- * @return IAvatar
+ * @param string $userId the Nextcloud user id
* @throws \Exception In case the username is potentially dangerous
* @throws \OCP\Files\NotFoundException In case there is no user folder yet
* @since 6.0.0
*/
- public function getAvatar(string $user) : IAvatar;
+ public function getAvatar(string $userId): IAvatar;
/**
* Returns a guest user avatar instance.