diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index f2561d6..3fa12ef 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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) diff --git a/helpers/ColorHelper.php b/helpers/ColorHelper.php index 3ad0019..4095612 100644 --- a/helpers/ColorHelper.php +++ b/helpers/ColorHelper.php @@ -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