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

87 lines
2.5 KiB
PHP
Raw Normal View History

2020-04-18 17:45:11 -06:00
<?php
namespace Grav\Plugin\Admin;
use Grav\Common\Grav;
class Whitebox
{
protected $grav;
protected $scss;
public function __construct()
{
$this->grav = Grav::instance();
$this->scss = new ScssCompiler();
}
2020-04-19 18:14:12 -06:00
public function compileScss($config, $options = ['filename' => 'preset'])
2020-04-18 17:45:11 -06:00
{
if (is_array($config)) {
$color_scheme = $config['color_scheme'];
} else {
2020-04-19 18:14:12 -06:00
$color_scheme = $config->get('whitebox.color_scheme');
2020-04-18 17:45:11 -06:00
}
if ($color_scheme) {
$locator = $this->grav['locator'];
$admin_in_base = $locator->findResource('plugin://admin/themes/grav/scss');
2020-04-19 22:08:46 +01:00
$custom_out_base = $locator->findResource('plugin://admin/themes/grav/css-compiled');
2020-04-18 17:45:11 -06:00
$preset_in_path = $admin_in_base .'/preset.scss';
2020-04-19 18:14:12 -06:00
$preset_out_path = $custom_out_base . '/'. $options['filename'] . '.css';
2020-04-19 18:31:11 -06:00
try {
$this->compilePresetScss($color_scheme, $preset_in_path, $preset_out_path);
} 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)
{
$compiler = $this->scss->reset();
$compiler->setVariables($colors['colors'] + $colors['accents']);
$compiler->setImportPaths(dirname($in_path));
$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';
}
}