Files
Grav-Admin-Plugin/classes/plugin/WhiteLabel.php

94 lines
2.7 KiB
PHP
Raw Normal View History

2020-04-18 17:45:11 -06:00
<?php
namespace Grav\Plugin\Admin;
use Grav\Common\Grav;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
2020-04-18 17:45:11 -06:00
2020-04-20 09:57:44 -06:00
class WhiteLabel
2020-04-18 17:45:11 -06:00
{
protected $grav;
protected $scss;
public function __construct()
{
$this->grav = Grav::instance();
$this->scss = new ScssCompiler();
}
public function compileScss($config, $options = [
'input' => 'plugin://admin/themes/grav/scss/preset.scss',
'output' => 'asset://admin-preset.css'
])
2020-04-18 17:45:11 -06:00
{
if (is_array($config)) {
$color_scheme = $config['color_scheme'];
} else {
2020-04-20 09:57:44 -06:00
$color_scheme = $config->get('whitelabel.color_scheme');
2020-04-18 17:45:11 -06:00
}
if ($color_scheme) {
/** @var UniformResourceLocator $locator */
2020-04-18 17:45:11 -06:00
$locator = $this->grav['locator'];
$input_scss = $locator->findResource($options['input']);
$output_css = $locator->findResource(($options['output']), true, true);
2020-04-18 17:45:11 -06:00
$input_path = dirname($input_scss);
$imports = [$locator->findResource('plugin://admin/themes/grav/scss')];
if (!in_array($input_path, $imports)) {
$imports[] = $input_path;
}
2020-04-19 18:14:12 -06:00
2020-04-19 18:31:11 -06:00
try {
$this->compilePresetScss($color_scheme, $input_scss, $output_css, $imports);
2020-04-19 18:31:11 -06:00
} catch (\Exception $e) {
return [false, $e->getMessage()];
}
2020-04-18 17:45:11 -06:00
2020-04-19 18:14:12 -06:00
return [true, 'Recompiled successfully'];
2020-04-18 17:45:11 -06:00
}
2020-04-19 18:14:12 -06:00
return [false, ' Could not be recompiled, missing color scheme...'];
2020-04-18 17:45:11 -06:00
}
public function compilePresetScss($colors, $in_path, $out_path, $imports)
2020-04-18 17:45:11 -06:00
{
$compiler = $this->scss->reset();
$compiler->setVariables($colors['colors'] + $colors['accents']);
$compiler->setImportPaths($imports);
2020-04-18 17:45:11 -06:00
$compiler->compile($in_path, $out_path);
}
public function colorContrast($color)
{
$opacity = 1;
$RGB = [];
if (substr($color, 0, 1) === '#') {
$color = ltrim($color, '#');
$RGB = [
hexdec(substr($color, 0, 2)),
hexdec(substr($color, 2, 2)),
hexdec(substr($color, 4, 2))
];
}
if (substr($color, 0, 3) === 'rgb') {
preg_match("/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/", $color, $matches);
array_shift($matches);
if (!$matches) { return $color; }
$RGB = [$matches[0], $matches[1], $matches[3]];
if (count($matches) === 4) {
$opacity = $matches[3];
}
}
$YIQ = (($RGB[0] * 299) + ($RGB[1] * 587) + ($RGB[2] * 114)) / 1000;
return ($YIQ >= 128) || $opacity <= 0.50 ? 'dark' : 'light';
}
}