diff options
Diffstat (limited to 'apps/theming/lib/Util.php')
-rw-r--r-- | apps/theming/lib/Util.php | 75 |
1 files changed, 27 insertions, 48 deletions
diff --git a/apps/theming/lib/Util.php b/apps/theming/lib/Util.php index 05b954d5059..beaca679149 100644 --- a/apps/theming/lib/Util.php +++ b/apps/theming/lib/Util.php @@ -34,17 +34,13 @@ use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\IConfig; +use Mexitek\PHPColors\Color; class Util { - /** @var IConfig */ - private $config; - - /** @var IAppManager */ - private $appManager; - - /** @var IAppData */ - private $appData; + private IConfig $config; + private IAppManager $appManager; + private IAppData $appData; /** * Util constructor. @@ -95,6 +91,21 @@ class Util { return $color; } + public function mix(string $color1, string $color2, int $factor): string { + $color = new Color($color1); + return '#' . $color->mix($color2, $factor); + } + + public function lighten(string $color, int $factor): string { + $color = new Color($color); + return '#' . $color->lighten($factor); + } + + public function darken(string $color, int $factor): string { + $color = new Color($color); + return '#' . $color->darken($factor); + } + /** * Convert RGB to HSL * @@ -106,38 +117,16 @@ class Util { * * @return array */ - public function toHSL($red, $green, $blue) { - $min = min($red, $green, $blue); - $max = max($red, $green, $blue); - $l = $min + $max; - $d = $max - $min; - - if ((int) $d === 0) { - $h = $s = 0; - } else { - if ($l < 255) { - $s = $d / $l; - } else { - $s = $d / (510 - $l); - } - - if ($red == $max) { - $h = 60 * ($green - $blue) / $d; - } elseif ($green == $max) { - $h = 60 * ($blue - $red) / $d + 120; - } else { - $h = 60 * ($red - $green) / $d + 240; - } - } - - return [fmod($h, 360), $s * 100, $l / 5.1]; + public function toHSL(string $red, string $green, string $blue): array { + $color = new Color(Color::rgbToHex(['R' => $red, 'G' => $green, 'B' => $blue])); + return array_values($color->getHsl()); } /** * @param string $color rgb color value * @return float */ - public function calculateLuminance($color) { + public function calculateLuminance(string $color): float { [$red, $green, $blue] = $this->hexToRGB($color); $hsl = $this->toHSL($red, $green, $blue); return $hsl[2] / 100; @@ -147,7 +136,7 @@ class Util { * @param string $color rgb color value * @return float */ - public function calculateLuma($color) { + public function calculateLuma(string $color): float { [$red, $green, $blue] = $this->hexToRGB($color); return (0.2126 * $red + 0.7152 * $green + 0.0722 * $blue) / 255; } @@ -157,19 +146,9 @@ class Util { * @return int[] * @psalm-return array{0: int, 1: int, 2: int} */ - public function hexToRGB($color) { - $hex = preg_replace("/[^0-9A-Fa-f]/", '', $color); - if (strlen($hex) === 3) { - $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; - } - if (strlen($hex) !== 6) { - return [0, 0, 0]; - } - return [ - hexdec(substr($hex, 0, 2)), - hexdec(substr($hex, 2, 2)), - hexdec(substr($hex, 4, 2)) - ]; + public function hexToRGB(string $color): array { + $color = new Color($color); + return array_values($color->getRgb()); } /** |