Skip to content

Commit

Permalink
rework color fallback strategy and code order
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhahnweilheim committed Feb 2, 2025
1 parent 89e5036 commit f9fa8cd
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 112 deletions.
107 changes: 74 additions & 33 deletions models/AbstractColorSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,30 @@ abstract class AbstractColorSettings extends \yii\base\Model
* Prefix (needed for DarkMode)
*/
public const PREFIX = '';

/*
* colors that need a default value
*/
public const DEFAULT_COLORS = [];

/*
* wether to load settings on init
*/
public bool $autoLoadSettings = true;

/*
* array of the names of the configurable colors
*/
public $configurableColors = [];

/*
* wether the model has warnings (after saving)
*/
public $hasWarnings = false;

// Main Colors (configurable)
/*
* Main Colors (configurable)
*/
public const MAIN_COLORS = ['default', 'primary', 'info', 'link', 'success', 'warning', 'danger'];
public $default;
public $primary;
Expand All @@ -28,7 +45,9 @@ abstract class AbstractColorSettings extends \yii\base\Model
public $warning;
public $danger;

// Text Colors (configurable)
/*
* Text Colors (configurable)
*/
public const TEXT_COLORS = ['text_color_main', 'text_color_secondary', 'text_color_highlight', 'text_color_soft', 'text_color_soft2', 'text_color_soft3', 'text_color_contrast'];
public $text_color_main;
public $text_color_secondary;
Expand All @@ -38,23 +57,44 @@ abstract class AbstractColorSettings extends \yii\base\Model
public $text_color_soft3;
public $text_color_contrast;

// Background Colors (configurable)
/*
* Background Colors (configurable)
*/
public const BACKGROUND_COLORS = ['background_color_main', 'background_color_secondary', 'background_color_page', 'background_color_highlight', 'background_color_highlight_soft', 'background3', 'background4'];
public $background_color_main;
public $background_color_secondary;
public $background_color_page;
public $background_color_highlight = '#daf0f3';
public $background_color_highlight_soft = '#f2f9fb';
public $background_color_highlight;
public $background_color_highlight_soft;
public $background3;
public $background4;

// Special colors which were generated by Less functions lighten(), darken() or fade()
/*
* Special colors which were generated by Less functions lighten(), darken() or fade()
*/
public const SPECIAL_COLORS = ['background4__fade__50','background4__lighten__10','background4__lighten__16','background_color_main__darken__10','background_color_page__darken__5','background_color_page__darken__8','background_color_page__lighten__10','background_color_page__lighten__20','background_color_page__lighten__3','background_color_page__lighten__30','background_color_secondary__darken__5','danger__darken__10','danger__darken__5','danger__lighten__20','danger__lighten__5','default__darken__2','default__darken__5','default__lighten__2','info__darken__10','info__darken__27','info__darken__5','info__lighten__25','info__lighten__30','info__lighten__40','info__lighten__45','info__lighten__5','info__lighten__50','info__lighten__8','link__darken__2','link__fade__60','link__lighten__5','primary__darken__10','primary__darken__5','primary__lighten__10','primary__lighten__20','primary__lighten__25','primary__lighten__5','primary__lighten__8','success__darken__10','success__darken__5','success__lighten__20','success__lighten__5','text_color_highlight__fade__15','text_color_highlight__fade__30','text_color_secondary__lighten__25','warning__darken__10','warning__darken__2','warning__darken__5','warning__lighten__10','warning__lighten__20','warning__lighten__40','warning__lighten__5',];

/*
* abstract function to get all colors, configured, default or fallback
* mainly used to store them in the variables.less file, see saveVarsToFile()
*/
abstract public function getColors(): array;

/*
* color fallback
* 1. default value
* 2. get from standard theme
*/
abstract protected function getColorFallback(string $color): string;


/*
* Additional color saving, used by light mode to save colors as theme var
*/
protected function additonalColorSaving(string $color, ?string $value): void
{
}

public function __construct($autoLoadSettings = true)
{
$this->autoLoadSettings = $autoLoadSettings;
Expand All @@ -75,16 +115,15 @@ public function init()
public function loadSettings()
{
$settings = static::getSettings();
foreach($this->configurableColors as $color) {
foreach ($this->configurableColors as $color) {
$this->$color = $settings->get(static::PREFIX . $color);
}
}

public function rules(): array
{
return [
[[
'default', 'primary', 'info', 'link', 'success', 'warning', 'danger',
[['default', 'primary', 'info', 'link', 'success', 'warning', 'danger',
'text_color_main', 'text_color_secondary', 'text_color_highlight', 'text_color_soft', 'text_color_soft2', 'text_color_soft3', 'text_color_contrast',
'background_color_main', 'background_color_secondary', 'background_color_page', 'background_color_highlight', 'background_color_highlight_soft', 'background3', 'background4'
], 'validateHexColor']
Expand All @@ -98,6 +137,13 @@ public function validateHexColor(string $attribute, $params, $validator)
}
}

/*
* Save colors to databse
* Save special colors (lightened etc.) to database
* save colors as CSS variables to variables file
* update theme.css
* eventually set hasWarnings to true (especially when file permission problems occur)
*/
public function save(): bool
{
if(!$this->validate()) {
Expand All @@ -122,7 +168,12 @@ public function save(): bool

return true;
}


/*
* Save colors to database
* as module setting
* in light mode also as theme variable
*/
protected function saveColors(): void
{
$settings = static::getSettings();
Expand All @@ -138,14 +189,10 @@ protected function saveColors(): void
$this->additonalColorSaving($color, $value);
}
}

/*
* Additional color saving, used by light mode to save colors as theme var
* Calculate and save special colors to database (module settings)
*/
protected function additonalColorSaving(string $color, ?string $value): void
{
}

protected function saveSpecialColors(): void
{
$settings = static::getSettings();
Expand All @@ -158,27 +205,13 @@ protected function saveSpecialColors(): void
// Get value of base color
$original_color = $this->$base_var;
if (empty($original_color)) {
// Get Base Theme Fallback (only used by light mode)
// Get fallback for base color
$original_color = $this->getColorFallback($base_var);
}

// Calculate color value with ColorHelper functions
if ($function == 'darken') {

$value = ColorHelper::darken($original_color, $amount);

} elseif ($function == 'lighten') {

$value = ColorHelper::lighten($original_color, $amount);

} elseif ($function == 'fade') {

$value = ColorHelper::fade($original_color, $amount);

} elseif ($function == 'fadeout') {

$value = ColorHelper::fadeout($original_color, $amount);

if (!empty($function)) {
$value = ColorHelper::$function($original_color, $amount);
} else {
$value = '';
}
Expand All @@ -202,6 +235,14 @@ protected function saveVarsToFile(): bool

return FileHelper::updateVarsFile($content, static::PREFIX);
}

protected function getDefaultValue(string $color): ?string
{
if (array_key_exists($color, static::EXTRA_COLORS)) {
return static::DEFAULT_COLORS[$color];
}
return '';
}

protected function getSettings()
{
Expand Down
77 changes: 42 additions & 35 deletions models/ColorSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@

class ColorSettings extends AbstractColorSettings
{
// Base Theme for fallback and default values
// Colors that are not hard coded in default theme
public const DEFAULT_COLORS = [
'background_color_highlight' => '#daf0f3',
'background_color_highlight_soft' => '#f2f9fb',
];

/*
* Base Theme for fallback values
*/
public const BASE_THEME = 'HumHub';

/*
* get all colors, configured, default or fallback to standard theme color
* mainly used to store them in the variables.less file, see saveVarsToFile()
*/
public function getColors(): array
{
$settings = self::getSettings();
Expand Down Expand Up @@ -41,6 +53,35 @@ public function getColors(): array
return $result;
}

/*
* color fallback
* 1. default value
* 2. get from standard theme
*/
protected function getColorFallBack(string $color): string
{
$value = self::getDefaultValue($color);

if (empty($value)) {
$theme_var = str_replace('_', '-', $color);
$value = ThemeHelper::getThemeByName(self::BASE_THEME)->variable(static::PREFIX . $theme_var);
}
return $value;
}

/*
* Save color values as theme variables (take default theme's color if value is empty)
*/
protected function additonalColorSaving(string $color, ?string $value): void
{
$theme_var = str_replace('_', '-', $color);
if (empty($value)) {
$value = ThemeHelper::getThemeByName(self::BASE_THEME)->variable($theme_var);
}
$theme_key = 'theme.var.FlexTheme.' . static::PREFIX . $theme_var;
Yii::$app->settings->set($theme_key, $value);
}

public function attributeHints(): array
{
$hints = [];
Expand All @@ -60,38 +101,4 @@ public function attributeHints(): array

return $hints;
}

protected function additonalColorSaving(string $color, ?string $value): void
{
// Save color values as theme variables (take default theme's color if value is empty)
$theme_var = str_replace('_', '-', $color);
if (empty($value)) {
$value = ThemeHelper::getThemeByName(self::BASE_THEME)->variable($theme_var);
}
$theme_key = 'theme.var.FlexTheme.' . static::PREFIX . $theme_var;
Yii::$app->settings->set($theme_key, $value);
}

protected function getColorFallBack(string $color): string
{
$value = self::getDefaultValue($color);

if (empty($value)) {
$theme_var = str_replace('_', '-', $color);
$value = ThemeHelper::getThemeByName(self::BASE_THEME)->variable(static::PREFIX . $theme_var);
}
return $value;
}

private function getDefaultValue(string $color): ?string
{
// compatiblity with PHP 7.4 will be removed in next version
if (version_compare(phpversion(), '8.0.0', '<')) {
$value = (new \ReflectionProperty($this, $color))->getDeclaringClass()->getDefaultProperties()[$color] ?? null;
} else {
// min PHP 8.0
$value = (new \ReflectionClass($this))->getProperty($color)->getDefaultValue();
}
return $value;
}
}
Loading

0 comments on commit f9fa8cd

Please sign in to comment.