Skip to content

Commit

Permalink
Merge pull request #9 from xefi/feature/colors-extension
Browse files Browse the repository at this point in the history
Feature/colors extension
  • Loading branch information
GautierDele authored Oct 22, 2024
2 parents 637a66d + ce8abcd commit e731ae4
Show file tree
Hide file tree
Showing 5 changed files with 527 additions and 4 deletions.
140 changes: 140 additions & 0 deletions src/Extensions/ColorsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace Xefi\Faker\Extensions;

use Xefi\Faker\Extensions\Traits\HasLocale;

class ColorsExtension extends Extension
{
use HasLocale;

protected array $safeColorNames = [
'Niger', 'Castaneus', 'Viridis', 'Navalis', 'Oliva',
'Purpureus', 'CaeruleusMarinus', 'Lima', 'Caeruleus', 'Argenteus',
'Canus', 'Flavus', 'Fuchsia', 'Aqua', 'Albus',
];

protected array $colorNames = [
'Niger', 'Castaneus', 'Viridis', 'Navalis', 'Oliva',
'Purpureus', 'CaeruleusMarinus', 'Lima', 'Caeruleus', 'Argenteus',
'Canus', 'Flavus', 'Fuchsia', 'Aqua', 'Albus',
'Beige', 'Chocolatum', 'Corallium', 'Cyanus', 'Crimsonus',
'CaeruleusFuscus', 'AurantiacusFuscus', 'Latericius', 'ViridisMarinus', 'AlbusFloralis',
'ViridisSilvestris', 'Aurum', 'Roseus', 'Indicum', 'Khaki',
'Lavandula', 'CaeruleusLevis', 'CoralliumLeve', 'ViridisLevis', 'SalmoneusLevis',
'Linum', 'Magenta', 'CaeruleusMedius', 'OrchisMedius', 'Mocassin',
'Aurantiacus', 'Ruber', 'Prunum', 'Peruviana', 'CaeruleusRegius',
'Salmoneus', 'FuscusArenosus', 'CaeruleusCaelum', 'Nix', 'Turcicus',
'Violaceus', 'AlbusFumosus',
];

public function safeColorName(): string
{
return $this->pickArrayRandomElement($this->safeColorNames);
}

public function colorName(): string
{
return $this->pickArrayRandomElement($this->colorNames);
}

public function safeHexColor(): string
{
$rand = '';
for ($i = 0; $i < 3; $i++) {
$rand .= str_repeat(dechex($this->randomizer->getInt(0, 15)), 2);
}

return '#'.$rand;
}

public function hexColor(): string
{
return '#'.bin2hex($this->randomizer->getBytes(3));
}

public function rgbColorAsArray(): array
{
$colors = [];
for ($i = 0; $i < 3; $i++) {
$colors[] = $this->randomizer->getInt(0, 255);
}

return $colors;
}

public function rgbColor(): string
{
return implode(',', $this->rgbColorAsArray());
}

public function rgbCssColor(): string
{
return 'rgb('.$this->rgbColor().')';
}

public function rgbaColorAsArray(): array
{
$colors = [];
for ($i = 0; $i < 3; $i++) {
$colors[] = $this->randomizer->getInt(0, 255);
}

$colors[] = round($this->randomizer->getFloat(0, 1), 2);

return $colors;
}

public function rgbaColor(): string
{
return implode(',', $this->rgbaColorAsArray());
}

public function rgbaCssColor(): string
{
return 'rgba('.$this->rgbaColor().')';
}

public function hslColorAsArray(): array
{
$colors = [];

$colors[] = $this->randomizer->getInt(0, 360); // Hue
$colors[] = $this->randomizer->getInt(0, 100); // Saturation
$colors[] = $this->randomizer->getInt(0, 100); // Lightness

return $colors;
}

public function hslColor(): string
{
return implode(',', $this->hslColorAsArray());
}

public function hslCssColor(): string
{
return 'hsl('.$this->hslColor().')';
}

public function hslaColorAsArray(): array
{
$colors = [];

$colors[] = $this->randomizer->getInt(0, 360); // Hue
$colors[] = $this->randomizer->getInt(0, 100); // Saturation
$colors[] = $this->randomizer->getInt(0, 100); // Lightness
$colors[] = round($this->randomizer->getFloat(0, 1), 2); // Alpha

return $colors;
}

public function hslaColor(): string
{
return implode(',', $this->hslaColorAsArray());
}

public function hslaCssColor(): string
{
return 'hsla('.$this->hslaColor().')';
}
}
19 changes: 17 additions & 2 deletions src/Extensions/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,29 @@ public function getName(): string
);
}

protected function pickArrayRandomElements(array $array, int $elements = 1)
/**
* Return a given number of random elements from the given array.
*
* @param array $array
* @param int $elements
*
* @return array
*/
protected function pickArrayRandomElements(array $array, int $elements = 1): array
{
$keys = $this->randomizer->pickArrayKeys($array, $elements);

return array_intersect_key($array, array_flip($keys));
}

protected function pickArrayRandomElement(array $array)
/**
* Return a random element from the given array.
*
* @param array $array
*
* @return mixed
*/
protected function pickArrayRandomElement(array $array): mixed
{
$elements = $this->pickArrayRandomElements($array);

Expand Down
2 changes: 2 additions & 0 deletions src/FakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Xefi\Faker;

use Xefi\Faker\Extensions\ColorsExtension;
use Xefi\Faker\Extensions\DateTimeExtension;
use Xefi\Faker\Extensions\FinancialExtension;
use Xefi\Faker\Extensions\HashExtension;
Expand All @@ -23,6 +24,7 @@ public function boot(): void
StringsExtension::class,
HashExtension::class,
DateTimeExtension::class,
ColorsExtension::class,
PersonExtension::class,
FinancialExtension::class,
]);
Expand Down
Loading

0 comments on commit e731ae4

Please sign in to comment.