diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9ea0f13 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.* +!.gitignore diff --git a/Module.php b/Module.php index 0ca3488..10e9e56 100644 --- a/Module.php +++ b/Module.php @@ -24,7 +24,7 @@ public function getDescription() // Link to configuration page public function getConfigUrl() { - return Url::to(['/flex-theme/config']); + return Url::to(['/flex-theme/admin']); } // Module Activation diff --git a/controllers/ConfigController.php b/controllers/AdminController.php similarity index 98% rename from controllers/ConfigController.php rename to controllers/AdminController.php index dbbf3ba..c2f5488 100644 --- a/controllers/ConfigController.php +++ b/controllers/AdminController.php @@ -8,7 +8,7 @@ use humhub\modules\flexTheme\models\AdvancedSettings; use Yii; -class ConfigController extends \humhub\modules\admin\components\Controller +class AdminController extends \humhub\modules\admin\components\Controller { public $subLayout = '@flex-theme/views/layouts/admin'; diff --git a/docs/INSTALLATION.md b/docs/INSTALLATION.md deleted file mode 100644 index 745f0bd..0000000 --- a/docs/INSTALLATION.md +++ /dev/null @@ -1,6 +0,0 @@ -# Installation - -1. Upload the module to /protected/modules (or another module loader path) OR use `git clone https://github.com/felixhahnweilheim/humhub-flex-theme.git` -2. Enable the module in Administration > Modules - -That's it! The theme is automatically activated. diff --git a/helpers/ColorHelper.php b/helpers/ColorHelper.php index 3ad0019..50fd12b 100644 --- a/helpers/ColorHelper.php +++ b/helpers/ColorHelper.php @@ -2,31 +2,29 @@ namespace humhub\modules\flexTheme\helpers; +/** + * helper class for color manipulations, imitating the LESS functions lighten, darken, fade + */ class ColorHelper { /* - * This function imitates the LESS function lighten() - * But it does not convert the color into HSL and back because this is not necessary to achieve the same result. + * lighten() imitates the LESS function lighten() + * It does not convert the color into HSL and back because this is not necessary to achieve the same result. + * @param string $color RGB hexadecimal color code including '#' + * @param int $amount between 0 and 100 + * @param bool $relative wether to lighten relatively to ligthness, default: false + * @return string RGB hexadecimal color code including '#' */ 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 - */ + // split color into its components $color_parts = ColorHelper::getColorComponents($color); - // $amount is expected to be a number between 0 an 100 $percentage = $amount / 100; // By default the LESS lighten() function adds the $amount absolutely to L, not relatively if (!$relative) { - - /* - * Converting a RGB color to HSL, the Lightness would be calculated by L = [max(R,G,B) + min(R,G,B)] / (2 * 255) - * So we need $max and $min - */ + //Converting a RGB color to HSL, the Lightness would be calculated by L = [max(R,G,B) + min(R,G,B)] / (2 * 255) $max = hexdec(max($color_parts)); $min = hexdec(min($color_parts)); if ($max != 0) { @@ -34,63 +32,74 @@ public static function lighten(string $color, int $amount, bool $relative = fals } } - $return = '#'; + $result = '#'; foreach ($color_parts as $color) { $color = hexdec($color); // Convert to decimal $color = round($color + (255 - $color) * $percentage); // Adjust color $color = max(min($color, 255), 0); // keep between 0 and 255 - $return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code + $result .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code } - return $return; - + return $result; } /* - * Documentation, see lighten() + * see lighten() + * @param string $color RGB hexadecimal color code including '#' + * @param int $amount between 0 and 100 + * @param bool $relative wether to darken relatively to ligthness, default: false + * @return string RGB hexadecimal color code including '#' */ public static function darken(string $color, int $amount, bool $relative = false): string { - - $percentage = $amount / 100; + // split color into its components $color_parts = ColorHelper::getColorComponents($color); - $max = hexdec(max($color_parts)); - $min = hexdec(min($color_parts)); - - if ($max == 0) { - return '#000000'; - } - + + $percentage = $amount / 100; + + // By default the LESS darken() function substracts the $amount absolutely to L, not relatively if (!$relative) { - $percentage = 2 * 255 * $percentage / ($max + $min); + //Converting a RGB color to HSL, the Lightness would be calculated by L = [max(R,G,B) + min(R,G,B)] / (2 * 255) + $max = hexdec(max($color_parts)); + $min = hexdec(min($color_parts)); + if ($max !== 0) { + $percentage = 2 * 255 * $percentage / ($max + $min); + } } - $return = '#'; + $result = '#'; foreach ($color_parts as $color) { $color = hexdec($color); // Convert to decimal $color = round($color * (1 - $percentage)); // Adjust color $color = max(min($color, 255), 0); // keep between 0 and 255 - $return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code + $result .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code } - return $return; - + return $result; } + /* + * fade() imitates the LESS function fade() which sets the opacity defined by $amount + * @param string $color RGB hexadecimal color code including '#' + * @param int $amount between 0 and 100 + * @return string RGBA hexadecimal color code including '#' + */ 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 $opacity = str_pad(dechex($opacity), 2, '0', STR_PAD_LEFT); // make 2 char hex code - // return RGBA as hex code return $color . $opacity; } - + + /* + * Split color into its components (R, G, B) + * @param string $color RGB hexadecimal color code + * @return array color components as 2 character hexadecimal code + */ protected static function getColorComponents(string $color): array { // Remove leading '#' diff --git a/helpers/FileHelper.php b/helpers/FileHelper.php index 172d960..9fa3d3a 100644 --- a/helpers/FileHelper.php +++ b/helpers/FileHelper.php @@ -5,30 +5,18 @@ use Yii; use yii\base\ErrorException; +/* + * helper class for safely accessing and modifying files + */ class FileHelper { public const THEME_PATH = '@flex-theme/themes/FlexTheme'; - private static function getVarsFile(string $prefix) - { - return Yii::getAlias(self::THEME_PATH . '/css/' . $prefix . 'variables.css'); - } - - private static function getThemeFile(string $prefix) - { - if ($prefix === 'dark_') { - $fileName = 'dark'; - } else { - $fileName = 'theme'; - } - return Yii::getAlias(self::THEME_PATH . '/css/' . $fileName . '.css'); - } - - private static function getThemeBaseFile(string $prefix) - { - return Yii::getAlias(self::THEME_PATH . '/css/' . $prefix . 'theme_base.css'); - } - + /* + * Update the color variables CSS file with given content + * @param string $content + * @param string $prefix, empty for default file or 'dark_' + */ public static function updateVarsFile(string $content, string $prefix): bool { try { @@ -40,16 +28,16 @@ public static function updateVarsFile(string $content, string $prefix): bool return true; } + /* + * Reload the CSS and update the main theme CSS file + * @param string $prefix, empty for default file or 'dark_' + */ public static function updateThemeFile(string $prefix): bool { // Base Theme - $theme_base = file_get_contents(self::getThemeBaseFile($prefix)); - + $content = file_get_contents(self::getThemeBaseFile($prefix)); // CSS Variables - $vars = file_get_contents(self::getVarsFile($prefix)); - - // Create/Update theme.css - $content = $theme_base . $vars; + $content .= file_get_contents(self::getVarsFile($prefix)); try { file_put_contents(self::getThemeFile($prefix), $content); @@ -67,4 +55,24 @@ public static function updateThemeFile(string $prefix): bool } return true; } + + private static function getVarsFile(string $prefix): string + { + return Yii::getAlias(self::THEME_PATH . '/css/' . $prefix . 'variables.css'); + } + + private static function getThemeFile(string $prefix): string + { + if ($prefix === 'dark_') { + $fileName = 'dark'; + } else { + $fileName = 'theme'; + } + return Yii::getAlias(self::THEME_PATH . '/css/' . $fileName . '.css'); + } + + private static function getThemeBaseFile(string $prefix): string + { + return Yii::getAlias(self::THEME_PATH . '/css/' . $prefix . 'theme_base.css'); + } }