Skip to content

Commit

Permalink
Merge pull request #42 from felixhahnweilheim/fix-41
Browse files Browse the repository at this point in the history
Fix division by zero
felixhahnweilheim authored Jan 26, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 938d090 + a525fdf commit 7c7cd45
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## TBA
- Fix #42: Division by zero (when trying to lighten white color)

## 0.3.0 (14 february 2024)
**NOTE** Files in themes/FlexTheme/css now need to be writable by the PHP process (see https://docs.humhub.org/docs/admin/installation/#file-permissions)

12 changes: 8 additions & 4 deletions helpers/ColorHelper.php
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ class ColorHelper
*/
public static function lighten(string $color, int $amount, bool $relative = false): string
{

/*
* $color is expected to be a hexadecimal color code (including '#')
* and has to be splitted into its components
@@ -29,9 +28,15 @@ public static function lighten(string $color, int $amount, bool $relative = fals
*/
$max = hexdec(max($color_parts));
$min = hexdec(min($color_parts));
if ($max != 0) {
$percentage = $percentage / (1 - ($max + $min) / (2 * 255));

/*
* if $min is 255, we would divide by zero below
* and white #ffffff does not need lightening anyways
*/
if ($min == 255) {
return $color;
}
$percentage = $percentage / (1 - ($max + $min) / (2 * 255));
}

$return = '#';
@@ -81,7 +86,6 @@ public static function darken(string $color, int $amount, bool $relative = false

public static function fade(string $color, int $amount): string
{

// $amount is expected to be between 0 and 100
$opacity = ($amount / 100) * 255;
$opacity = max(min($opacity, 255), 0); // keep between 0 and 255

0 comments on commit 7c7cd45

Please sign in to comment.