From cbf6b6c5df88bc5248982c70d77c5e5c7f481132 Mon Sep 17 00:00:00 2001 From: Pim Jansen Date: Fri, 28 Jan 2022 23:17:28 +0100 Subject: [PATCH] Added color extension to core --- src/Faker/Core/Color.php | 180 +++++++++++++++++++++++ src/Faker/Extension/ColorExtension.php | 63 ++++++++ src/Faker/Extension/ContainerBuilder.php | 1 + test/Faker/Core/ColorTest.php | 77 ++++++++++ 4 files changed, 321 insertions(+) create mode 100644 src/Faker/Core/Color.php create mode 100644 src/Faker/Extension/ColorExtension.php create mode 100644 test/Faker/Core/ColorTest.php diff --git a/src/Faker/Core/Color.php b/src/Faker/Core/Color.php new file mode 100644 index 0000000000..03395068ea --- /dev/null +++ b/src/Faker/Core/Color.php @@ -0,0 +1,180 @@ +numberBetween(1, 16777215)), 6, '0', STR_PAD_LEFT); + } + + /** + * @example '#ff0044' + */ + public function safeHexColor(): string + { + $number = new Number(); + $color = str_pad(dechex($number->numberBetween(0, 255)), 3, '0', STR_PAD_LEFT); + + return sprintf( + '#%s%s%s%s%s%s', + $color[0], + $color[0], + $color[1], + $color[1], + $color[2], + $color[2] + ); + } + + /** + * @example 'array(0,255,122)' + * + * @return string[] + */ + public function rgbColorAsArray(): array + { + $color = $this->hexColor(); + + return [ + hexdec(substr($color, 1, 2)), + hexdec(substr($color, 3, 2)), + hexdec(substr($color, 5, 2)), + ]; + } + + /** + * @example '0,255,122' + */ + public function rgbColor(): string + { + return implode(',', $this->rgbColorAsArray()); + } + + /** + * @example 'rgb(0,255,122)' + */ + public function rgbCssColor(): string + { + return sprintf( + 'rgb(%s)', + $this->rgbColor() + ); + } + + /** + * @example 'rgba(0,255,122,0.8)' + */ + public function rgbaCssColor(): string + { + $number = new Number(); + + return sprintf( + 'rgba(%s,%s)', + $this->rgbColor(), + $number->randomFloat(1, 0, 1) + ); + } + + /** + * @example 'blue' + */ + public function safeColorName(): string + { + return Helper::randomElement($this->safeColorNames); + } + + /** + * @example 'NavajoWhite' + */ + public function colorName(): string + { + return Helper::randomElement($this->allColorNames); + } + + /** + * @example '340,50,20' + */ + public function hslColor(): string + { + $number = new Number(); + + return sprintf( + '%s,%s,%s', + $number->numberBetween(0, 360), + $number->numberBetween(0, 100), + $number->numberBetween(0, 100) + ); + } + + /** + * @example array(340, 50, 20) + * + * @return string[] + */ + public function hslColorAsArray(): array + { + $number = new Number(); + + return [ + $number->numberBetween(0, 360), + $number->numberBetween(0, 100), + $number->numberBetween(0, 100), + ]; + } +} diff --git a/src/Faker/Extension/ColorExtension.php b/src/Faker/Extension/ColorExtension.php new file mode 100644 index 0000000000..6f738d012b --- /dev/null +++ b/src/Faker/Extension/ColorExtension.php @@ -0,0 +1,63 @@ + Core\Barcode::class, BloodExtension::class => Core\Blood::class, + ColorExtension::class => Core\Color::class, FileExtension::class => Core\File::class, NumberExtension::class => Core\Number::class, VersionExtension::class => Core\Version::class, diff --git a/test/Faker/Core/ColorTest.php b/test/Faker/Core/ColorTest.php new file mode 100644 index 0000000000..a75349fca8 --- /dev/null +++ b/test/Faker/Core/ColorTest.php @@ -0,0 +1,77 @@ +hexColor()); + } + + public function testSafeHexColor() + { + $color = new Color(); + self::assertMatchesRegularExpression('/^#[a-f0-9]{6}$/i', $color->safeHexColor()); + } + + public function testRgbColorAsArray() + { + $color = new Color(); + self::assertCount(3, $color->rgbColorAsArray()); + } + + public function testRgbColor() + { + $color = new Color(); + $regexp = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])'; + self::assertMatchesRegularExpression('/^' . $regexp . ',' . $regexp . ',' . $regexp . '$/i', $color->rgbColor()); + } + + public function testRgbCssColor() + { + $color = new Color(); + $regexp = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])'; + self::assertMatchesRegularExpression('/^rgb\(' . $regexp . ',' . $regexp . ',' . $regexp . '\)$/i', $color->rgbCssColor()); + } + + public function testRgbaCssColor() + { + $color = new Color(); + $regexp = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])'; + $regexpAlpha = '([01]?(\.\d+)?)'; + self::assertMatchesRegularExpression('/^rgba\(' . $regexp . ',' . $regexp . ',' . $regexp . ',' . $regexpAlpha . '\)$/i', $color->rgbaCssColor()); + } + + public function testSafeColorName() + { + $color = new Color(); + self::assertMatchesRegularExpression('/^[\w]+$/', $color->safeColorName()); + } + + public function testColorName() + { + $color = new Color(); + self::assertMatchesRegularExpression('/^[\w]+$/', $color->colorName()); + } + + public function testHslColor() + { + $color = new Color(); + $regexp360 = '(?:36[0]|3[0-5][0-9]|[12][0-9][0-9]|[1-9]?[0-9])'; + $regexp100 = '(?:100|[1-9]?[0-9])'; + self::assertMatchesRegularExpression('/^' . $regexp360 . ',' . $regexp100 . ',' . $regexp100 . '$/', $color->hslColor()); + } + + public function testHslColorArray() + { + $color = new Color(); + self::assertCount(3, $color->hslColorAsArray()); + } +}