1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-04-07 00:55:58 +02:00

Merge pull request from lsv/colors

Color provider
This commit is contained in:
Francois Zaninotto 2013-07-01 05:49:36 -07:00
commit 5d660b794d
3 changed files with 152 additions and 0 deletions
readme.md
src/Faker/Provider
test/Faker/Provider

@ -186,6 +186,15 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
fileExtension // 'avi'
mimeType // 'video/x-msvideo'
### `Faker\Provider\Color`
hexcolor // '#fa3cc2'
rgbcolor // '0,255,122'
rgbColorAsArray // array(0,255,122)
rgbCssColor // 'rgb(0,255,122)'
safeColorName // 'fuchsia'
colorName // 'Gainsbor'
## Localization
`Faker\Factory` can take a locale as an argument, to return localized data. If no localized provider is found, the factory fallbacks to the default locale (en_EN).

@ -0,0 +1,111 @@
<?php
/**
* Created by lsv
* Date: 6/17/13
* Time: 12:49 AM
*/
namespace Faker\Provider;
class Color extends Base
{
protected static $safeColorNames = array(
'black', 'maroon', 'green', 'navy', 'olive',
'purple', 'teal', 'lime', 'blue', 'silver',
'gray', 'yellow', 'fuchsia', 'aqua', 'white'
);
protected static $allColorNames = array(
'AliceBlue', 'AntiqueWhite', 'Aqua', 'Aquamarine',
'Azure', 'Beige', 'Bisque', 'Black', 'BlanchedAlmond',
'Blue', 'BlueViolet', 'Brown', 'BurlyWood', 'CadetBlue',
'Chartreuse', 'Chocolate', 'Coral', 'CornflowerBlue',
'Cornsilk', 'Crimson', 'Cyan', 'DarkBlue', 'DarkCyan',
'DarkGoldenRod', 'DarkGray', 'DarkGreen', 'DarkKhaki',
'DarkMagenta', 'DarkOliveGreen', 'Darkorange', 'DarkOrchid',
'DarkRed', 'DarkSalmon', 'DarkSeaGreen', 'DarkSlateBlue',
'DarkSlateGray', 'DarkTurquoise', 'DarkViolet', 'DeepPink',
'DeepSkyBlue', 'DimGray', 'DimGrey', 'DodgerBlue', 'FireBrick',
'FloralWhite', 'ForestGreen', 'Fuchsia', 'Gainsboro', 'GhostWhite',
'Gold', 'GoldenRod', 'Gray', 'Green', 'GreenYellow', 'HoneyDew',
'HotPink', 'IndianRed ', 'Indigo ', 'Ivory', 'Khaki', 'Lavender',
'LavenderBlush', 'LawnGreen', 'LemonChiffon', 'LightBlue', 'LightCoral',
'LightCyan', 'LightGoldenRodYellow', 'LightGray', 'LightGreen', 'LightPink',
'LightSalmon', 'LightSeaGreen', 'LightSkyBlue', 'LightSlateGray', 'LightSteelBlue',
'LightYellow', 'Lime', 'LimeGreen', 'Linen', 'Magenta', 'Maroon', 'MediumAquaMarine',
'MediumBlue', 'MediumOrchid', 'MediumPurple', 'MediumSeaGreen', 'MediumSlateBlue',
'MediumSpringGreen', 'MediumTurquoise', 'MediumVioletRed', 'MidnightBlue',
'MintCream', 'MistyRose', 'Moccasin', 'NavajoWhite', 'Navy', 'OldLace', 'Olive',
'OliveDrab', 'Orange', 'OrangeRed', 'Orchid', 'PaleGoldenRod', 'PaleGreen',
'PaleTurquoise', 'PaleVioletRed', 'PapayaWhip', 'PeachPuff', 'Peru', 'Pink', 'Plum',
'PowderBlue', 'Purple', 'Red', 'RosyBrown', 'RoyalBlue', 'SaddleBrown', 'Salmon',
'SandyBrown', 'SeaGreen', 'SeaShell', 'Sienna', 'Silver', 'SkyBlue', 'SlateBlue',
'SlateGray', 'Snow', 'SpringGreen', 'SteelBlue', 'Tan', 'Teal', 'Thistle', 'Tomato',
'Turquoise', 'Violet', 'Wheat', 'White', 'WhiteSmoke', 'Yellow', 'YellowGreen'
);
/**
* @example '#fa3cc2'
*/
public static function hexColor()
{
return '#' . dechex(mt_rand(1, 16777215));
}
/**
* @example '#ff0044'
*/
public static function safeHexColor()
{
$color = str_pad(dechex(mt_rand(0,255)), 3, '0', STR_PAD_LEFT);
return '#' . $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2];
}
/**
* @example 'array(0,255,122)'
*/
public static function rgbColorAsArray()
{
$color = static::hexColor();
return array(
hexdec(substr($color,1,2)),
hexdec(substr($color,3,2)),
hexdec(substr($color,5,2))
);
}
/**
* @example '0,255,122'
*/
public static function rgbColor()
{
return implode(',', static::rgbColorAsArray());
}
/**
* @example 'rgb(0,255,122)'
*/
public static function rgbCssColor()
{
return 'rgb(' . static::rgbColor() . ')';
}
/**
* @example 'blue'
*/
public static function safeColorName()
{
return static::randomElement(static::$safeColorNames);
}
/**
* @example 'NavajoWhite'
*/
public static function colorName()
{
return static::randomElement(static::$allColorNames);
}
}

@ -0,0 +1,32 @@
<?php
namespace Faker\Test\Provider;
use Faker\Provider\Color;
class ColorTest extends \PHPUnit_Framework_TestCase
{
public function testHexColor()
{
$this->assertRegExp('/^#[a-f0-9]{6}$/i', Color::hexColor());
}
public function testRgbColorAsArray()
{
$this->assertEquals(3, count(Color::rgbColorAsArray()));
}
public function testRgbColor()
{
$regexp = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])';
$this->assertRegExp('/^' . $regexp . ',' . $regexp . ',' . $regexp . '$/i', Color::rgbColor());
}
public function testRgbCssColor()
{
$regexp = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])';
$this->assertRegExp('/^rgb\(' . $regexp . ',' . $regexp . ',' . $regexp . '\)$/i', Color::rgbCssColor());
}
}