Base
This commit is contained in:
169
app/Helper/Color.php
Normal file
169
app/Helper/Color.php
Normal file
@ -0,0 +1,169 @@
|
||||
<?php
|
||||
namespace App\Helper;
|
||||
|
||||
class Color
|
||||
{
|
||||
protected $color;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$args = func_get_args();
|
||||
switch (count($args)) {
|
||||
case 1:
|
||||
if (is_array($args[0])) {
|
||||
$this->color = $args[0];
|
||||
break;
|
||||
}
|
||||
$this->color = $this->hex2dec($this->hex2array($args[0]));
|
||||
break;
|
||||
case 2:
|
||||
if (is_array($args[0])) {
|
||||
$this->color = $args[0];
|
||||
$this->color []= $args[1];
|
||||
break;
|
||||
}
|
||||
$color = $this->hex2array($args[0]);
|
||||
$color []= $args[1];
|
||||
$this->color = $this->hex2dec($color);
|
||||
break;
|
||||
case 3:
|
||||
case 4:
|
||||
if (is_numeric($args[0])) {
|
||||
$this->color = $args;
|
||||
break;
|
||||
}
|
||||
$this->color = $this->hex2dec($args);
|
||||
break;
|
||||
}
|
||||
}
|
||||
protected function hex2dec(array $hex)
|
||||
{
|
||||
return array_map('hexdec', $hex);
|
||||
}
|
||||
protected function dec2hex(array $bin)
|
||||
{
|
||||
return array_map('dechex', $bin);
|
||||
}
|
||||
protected function hex2array(string $hex)
|
||||
{
|
||||
switch (strlen($hex)) {
|
||||
case 3:
|
||||
case 4:
|
||||
case 6:
|
||||
case 7:
|
||||
return str_split($hex, 2);
|
||||
default:
|
||||
throw new OutOfBoundsException('The string ' . $hex . ' is not a correct color code.');
|
||||
}
|
||||
}
|
||||
protected function array2hex(array $arr)
|
||||
{
|
||||
return implode('', $arr);
|
||||
}
|
||||
|
||||
public function convertTo($type)
|
||||
{
|
||||
switch (strtolower($type)) {
|
||||
case 'hex':
|
||||
if (is_numeric($this->color[0])) {
|
||||
$this->color = $this->dec2hex($this->color);
|
||||
}
|
||||
break;
|
||||
case 'dec':
|
||||
if (!is_numeric($this->color[0])) {
|
||||
$this->color = $this->hex2dec($this->color);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new InvalidArgumentException($type . ' is not a valid argument.');
|
||||
}
|
||||
}
|
||||
public function add($base_color, $amount)
|
||||
{
|
||||
$changed = false;
|
||||
if (!is_numeric($this->color)) {
|
||||
$this->convertTo('dec');
|
||||
$changed = true;
|
||||
}
|
||||
switch (strtolower($base_color)) {
|
||||
case 'r':
|
||||
$base_color = 0;
|
||||
break;
|
||||
case 'g':
|
||||
$base_color = 1;
|
||||
break;
|
||||
case 'b':
|
||||
$base_color = 2;
|
||||
break;
|
||||
case 'a':
|
||||
$base_color = 3;
|
||||
break;
|
||||
default:
|
||||
throw new OutOfBoundsException("Base color '" . $base_color . "' does not exist.");
|
||||
}
|
||||
$this->color[$base_color] += $amount;
|
||||
if ($changed) {
|
||||
$this->convertTo('hex');
|
||||
}
|
||||
}
|
||||
public function print()
|
||||
{
|
||||
$this->convertTo('hex');
|
||||
return implode('', $this->color);
|
||||
}
|
||||
public function toRGB()
|
||||
{
|
||||
$changed = false;
|
||||
$this->convertTo('dec');
|
||||
$str = 'rgb(' . implode(', ', array_map(function($a) {
|
||||
return round($a, 2);
|
||||
}, $this->color)) . ')';
|
||||
if ($changed) {
|
||||
$this->convertTo('hex');
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
public function __toString()
|
||||
{
|
||||
return $this->print();
|
||||
}
|
||||
public function toArray()
|
||||
{
|
||||
return $this->color;
|
||||
}
|
||||
public function toVector()
|
||||
{
|
||||
$changed = false;
|
||||
$this->convertTo('dec');
|
||||
$arr = $this->toArray();
|
||||
if ($changed) {
|
||||
$this->convertTo('hex');
|
||||
}
|
||||
return $arr;
|
||||
}
|
||||
public function luminosity()
|
||||
{
|
||||
$changed = false;
|
||||
$this->convertTo('dec');
|
||||
//sqrt( 0.299*R^2 + 0.587*G^2 + 0.114*B^2 )
|
||||
$str = sqrt(0.299 * pow($this->color[0], 2) + 0.587 * pow($this->color[1], 2) + 0.114 * pow($this->color[2], 2)) / 255 * 100;
|
||||
if ($changed) {
|
||||
$this->convertTo('hex');
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
public function isDark()
|
||||
{
|
||||
if ($this->luminosity() < 50) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public function isBright()
|
||||
{
|
||||
if ($this->luminosity() > 75) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
61
app/Helper/Line.php
Normal file
61
app/Helper/Line.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
namespace App\Helper;
|
||||
|
||||
class Line
|
||||
{
|
||||
protected $origin;
|
||||
protected $length;
|
||||
protected $direction;
|
||||
|
||||
public function __construct($point_a, $point_b)
|
||||
{
|
||||
$this->origin = $point_a;
|
||||
$this->length = $this->distance($point_a, $point_b);
|
||||
$this->direction = $this->gradient($point_a, $point_b);
|
||||
}
|
||||
public function origin()
|
||||
{
|
||||
return $this->origin;
|
||||
}
|
||||
public function length()
|
||||
{
|
||||
return $this->length;
|
||||
}
|
||||
public function direction()
|
||||
{
|
||||
return $this->direction;
|
||||
}
|
||||
/**
|
||||
* Calculate the gradient to move from point a to point b
|
||||
* @param array $point_a [Vector]
|
||||
* @param array $point_b [Vector]
|
||||
* @return array [Vector]
|
||||
*/
|
||||
public function gradient($point_a, $point_b)
|
||||
{
|
||||
$distance = $this->distance($point_a, $point_b);
|
||||
return array_map(function($a, $b) use ($distance) {
|
||||
return ($a - $b) / $distance;
|
||||
}, $point_b, $point_a);
|
||||
}
|
||||
// sqrt((a0-b0)²+(a1-b1)²+(a2-b2)²)
|
||||
public function distance($point_a, $point_b)
|
||||
{
|
||||
return sqrt(array_sum(array_map(function($a, $b) {
|
||||
return pow($a - $b, 2);
|
||||
}, $point_a, $point_b)));
|
||||
}
|
||||
/**
|
||||
* Move from point in the direction of the gradient acording to step_size
|
||||
* @param array $point [Vector]
|
||||
* @param int $step_size [step size]
|
||||
* @return array [Vector]
|
||||
*/
|
||||
public function move($point, $step_size)
|
||||
{
|
||||
$gradient = $this->direction;
|
||||
return array_map(function($a, $b) use ($step_size) {
|
||||
return $a + $b * $step_size;
|
||||
}, $point, $gradient);
|
||||
}
|
||||
}
|
102
app/Helper/functions.php
Normal file
102
app/Helper/functions.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
function route() {
|
||||
return App\Contract\Route::route();
|
||||
}
|
||||
function uf($date, $async = false) {
|
||||
if (is_string($date)) {
|
||||
$date = Carbon\Carbon::parse($date, config('app.timezone'));
|
||||
}
|
||||
$next_m_9 = Carbon\Carbon::today(config('app.timezone'))->copy()->endOfMonth()->addDays(9);
|
||||
if ($date->greaterThanOrEqualTo($next_m_9)) {
|
||||
return (object) ['total' => 0];
|
||||
}
|
||||
$url = 'http://' . config('locations.money') . '/api/uf/value/' . $date->format('Y-m-d');
|
||||
$client = new \Goutte\Client();
|
||||
$client->setHeader('Accept', 'application/json');
|
||||
$client->request('GET', $url);
|
||||
|
||||
$response = $client->getResponse();
|
||||
if (!$response) {
|
||||
return (object) ['total' => 0];
|
||||
}
|
||||
$status = $response->getStatusCode();
|
||||
if ($status >= 200 and $status < 300) {
|
||||
$data = json_decode($response->getContent());
|
||||
return $data;
|
||||
}
|
||||
return (object) ['total' => 0];
|
||||
}
|
||||
function format($tipo, $valor, $format = null, $print = false) {
|
||||
if (strtolower($tipo) == 'localdate') {
|
||||
$d = \Carbon\Carbon::parse($valor);
|
||||
$d->locale('es_ES');
|
||||
if ($format == null) {
|
||||
$format = 'DD [de] MMMM [de] YYYY';
|
||||
}
|
||||
return $d->isoFormat($format);
|
||||
}
|
||||
if (method_exists('\App\Helper\Format', $tipo)) {
|
||||
if ($print) {
|
||||
return \App\Helper\Format::$tipo($valor, $print);
|
||||
} else {
|
||||
return \App\Helper\Format::$tipo($valor);
|
||||
}
|
||||
} else {
|
||||
switch ($tipo) {
|
||||
case 'localDate':
|
||||
if (isset($format)) {
|
||||
$intl = new IntlDateFormatter('es_ES', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT, 'America/Santiago');
|
||||
$intl->setPattern($format);
|
||||
return ucwords($intl->format($valor));
|
||||
}
|
||||
case 'percent':
|
||||
return \App\Helper\Format::number($valor, 2);
|
||||
case 'rut':
|
||||
return \App\Helper\Format::number($valor, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
function model(string $class_name) {
|
||||
return \Model::factory($class_name);
|
||||
}
|
||||
function correctNumber($number) {
|
||||
if (strpos($number, ',') !== false) {
|
||||
return str_replace(',', '.', str_replace('.', '', $number));
|
||||
} elseif (substr_count($number, '.') > 1) {
|
||||
return str_replace('.', '', $number);
|
||||
}
|
||||
return $number;
|
||||
}
|
||||
function parseRanges($range, $numeric = true, $negatives = true) {
|
||||
$rns = preg_split('/[,;]+/', $range);
|
||||
$data = [];
|
||||
foreach ($rns as $p) {
|
||||
if (!$negatives) {
|
||||
if (strpos($p, '-') !== false) {
|
||||
list($ini, $end) = explode('-', $p);
|
||||
$data = array_merge($data, range($ini, $end));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if ($numeric) {
|
||||
$data []= (float) $p;
|
||||
continue;
|
||||
}
|
||||
$data []= $p;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
function nUrl($p, $a = null, $data = null) {
|
||||
$query = ['p' => $p];
|
||||
if ($a != null) {
|
||||
$query['a'] = $a;
|
||||
if ($data != null) {
|
||||
$query = array_merge($query, $data);
|
||||
}
|
||||
}
|
||||
return url('', $query);
|
||||
}
|
||||
function doLog($user, $action, $variables) {
|
||||
App\Service\Register::log($user, $action, $variables);
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user