ldc-showroom-bo/app/Lib/Helpers/ToolboxHelper.php
2025-05-27 09:11:43 +02:00

51 lines
1.6 KiB
PHP

<?php
namespace App\Lib\Helpers;
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
class ToolboxHelper
{
/**
* Get the contrast for any color
*
* @param string $hexcolor An HTML Hex color.
* @return string "#000000" or "#FFFFFF".
*/
public static function getContrastYiq(string $hexcolor): string
{
$r = intval(\substr($hexcolor, 1, 2), 16);
$g = intval(\substr($hexcolor, 3, 2), 16);
$b = intval(\substr($hexcolor, 5, 2), 16);
$yiq = (($r * 299) + ($g * 587) + ($b * 114)) / 1000;
return ($yiq >= 128) ? "#000000" : "#FFFFFF";
}
/**
* Get a specific json schema
*
* @param string $schema
* @return array|false Returns false if the schema cannot be found.
* @throws \RuntimeException If the schema does not exists.
*/
public static function getJsonSchema(string $schema): array|false
{
$schemaPath = Str::of(config('json-schema.local_base_prefix'))->finish("/{$schema}");
if (!File::exists($schemaPath)) {
return false;
}
$schemaContent = File::get($schemaPath);
if (!Str::isJson($schemaContent)) {
throw new \RuntimeException("{$schemaPath} is not a json schema .");
}
$schemaContent = json_decode($schemaContent, true);
$schemaRoute = route('fo.schema', ['schema' => $schema]);
if (app()->environment('local')) {
$schemaRoute = Str::replace('https', 'http', $schemaRoute);
}
Arr::set($schemaContent, '$id', $schemaRoute);
return $schemaContent;
}
}