Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhahnweilheim committed Feb 25, 2024
1 parent 0d78e21 commit 7cc9902
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.*
!.gitignore
2 changes: 1 addition & 1 deletion Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
6 changes: 0 additions & 6 deletions docs/INSTALLATION.md

This file was deleted.

81 changes: 45 additions & 36 deletions helpers/ColorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,104 @@

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) {
$percentage = $percentage / (1 - ($max + $min) / (2 * 255));
}
}

$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 '#'
Expand Down
60 changes: 34 additions & 26 deletions helpers/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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');
}
}

0 comments on commit 7cc9902

Please sign in to comment.