Skip to content

Commit

Permalink
Merge pull request #18 from darrynten/dev
Browse files Browse the repository at this point in the history
Colour Validation for RGB and RGBa
  • Loading branch information
darrynten authored Mar 19, 2017
2 parents e88e89e + 3f15d8c commit e08c1dd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/Validators/ColourValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,37 @@ class ColourValidator
/**
* Valid short hex (#FFF)
*
* @var array $validShortHex
* @var string $validShortHex
*/
private static $validShortHex = '/^\#[0-9A-Fa-f]{3}$/';

/**
* Valid long hex (#FFFFFF)
*
* @var array $validLongHex
* @var string $validLongHex
*/
private static $validLongHex = '/^\#[0-9A-Fa-f]{6}$/';

/**
* Valid rgba
*
* rgba(1,1,1,1)
* rgba(0,10,0,0)
* rgba(0, 0 ,0, 0.0)
* rgba( 255,255,255,1)
* rgba(255, 111 ,0 , 0.5 )
*
* @var string $validRgba
*/
private static $validRgba = '/^rgba\(\s?[0-2]{0,1}?[0-9]{0,2}?\s?,\s?[0-2]{0,1}?[0-9]{0,2}?\s?,\s?[0-2]{0,1}[0-9]{0,2}?\s?(,\s?[0-1]{1}\.*?[0-9]?\s?)?\)$/';

/**
* Valid rgb
*
* @var string $validRgb
*/
private static $validRgb = '/^rgb\(\s?[0-2]{0,1}?[0-9]{0,2}?\s?,\s?[0-2]{0,1}?[0-9]{0,2}?\s?,\s?[0-2]{0,1}[0-9]{0,2}?\s?\)$/';

/**
* Valid word
*
Expand Down Expand Up @@ -56,6 +76,14 @@ public static function isValidColour(string $colour = null)
return true;
}

if (preg_match(self::$validRgb, $colour)) {
return true;
}

if (preg_match(self::$validRgba, $colour)) {
return true;
}

if (preg_match(self::$validWord, $colour)) {
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Pslayers/Validators/ColourValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public function testValidation()
$this->assertTrue(ColourValidator::isValidColour('#000000'));
$this->assertTrue(ColourValidator::isValidColour('#FFF'));
$this->assertTrue(ColourValidator::isValidColour('#b4d455'));
$this->assertTrue(ColourValidator::isValidColour('rgb(255,128,1)'));
$this->assertTrue(ColourValidator::isValidColour('rgba(255,128,0, 1)'));
$this->assertTrue(ColourValidator::isValidColour(null));

$this->assertTrue(ColourValidator::isValidOpacity(1));
Expand Down

0 comments on commit e08c1dd

Please sign in to comment.