Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: php

php:
- 7.0
- 7.1
- 7.2

install:
- travis_retry composer install

script:
- vendor/bin/phpunit
11 changes: 11 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,16 @@
"psr-4": {
"Colors\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Colors\\Tests\\": "tests"
}
},
"require": {
"php": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
}
}
22 changes: 22 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="RandomColor Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
30 changes: 15 additions & 15 deletions src/RandomColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@

class RandomColor
{
static public $dictionary = null;
public static $dictionary = null;

private function __construct() {}

static public function one($options = array())
public static function one($options = array())
{
$h = self::_pickHue($options);
$s = self::_pickSaturation($h, $options);
Expand All @@ -46,7 +46,7 @@ static public function one($options = array())
return self::format(compact('h','s','v'), @$options['format']);
}

static public function many($count, $options = array())
public static function many($count, $options = array())
{
$colors = array();

Expand All @@ -58,7 +58,7 @@ static public function many($count, $options = array())
return $colors;
}

static public function format($hsv, $format='hex')
public static function format($hsv, $format='hex')
{
switch ($format)
{
Expand All @@ -84,7 +84,7 @@ static public function format($hsv, $format='hex')
}
}

static private function _pickHue($options)
private static function _pickHue($options)
{
$range = self::_getHueRange($options);

Expand All @@ -105,7 +105,7 @@ static private function _pickHue($options)
return $hue;
}

static private function _pickSaturation($h, $options)
private static function _pickSaturation($h, $options)
{
if (@$options['hue'] === 'monochrome')
{
Expand Down Expand Up @@ -137,7 +137,7 @@ static private function _pickSaturation($h, $options)
return self::_rand($range, $options);
}

static private function _pickBrightness($h, $s, $options)
private static function _pickBrightness($h, $s, $options)
{
if (@$options['luminosity'] === 'random')
{
Expand Down Expand Up @@ -165,7 +165,7 @@ static private function _pickBrightness($h, $s, $options)
return self::_rand($range, $options);
}

static private function _getHueRange($options)
private static function _getHueRange($options)
{
$ranges = array();

Expand Down Expand Up @@ -212,7 +212,7 @@ static private function _getHueRange($options)
}
}

static private function _getMinimumBrightness($h, $s)
private static function _getMinimumBrightness($h, $s)
{
$colorInfo = self::_getColorInfo($h);
$bounds = $colorInfo['bounds'];
Expand All @@ -235,7 +235,7 @@ static private function _getMinimumBrightness($h, $s)
return 0;
}

static private function _getColorInfo($h)
private static function _getColorInfo($h)
{
// Maps red colors to make picking hue easier
if ($h >= 334 && $h <= 360)
Expand All @@ -252,7 +252,7 @@ static private function _getColorInfo($h)
}
}

static private function _rand($bounds, $options)
private static function _rand($bounds, $options)
{
if (isset($options['prng']))
{
Expand All @@ -264,7 +264,7 @@ static private function _rand($bounds, $options)
}
}

static public function hsv2hex($hsv)
public static function hsv2hex($hsv)
{
$rgb = self::hsv2rgb($hsv);
$hex = '#';
Expand All @@ -277,7 +277,7 @@ static public function hsv2hex($hsv)
return $hex;
}

static public function hsv2hsl($hsv)
public static function hsv2hsl($hsv)
{
extract($hsv);

Expand All @@ -292,7 +292,7 @@ static public function hsv2hsl($hsv)
);
}

static public function hsv2rgb($hsv)
public static function hsv2rgb($hsv)
{
extract($hsv);

Expand Down Expand Up @@ -387,4 +387,4 @@ static public function hsv2rgb($hsv)
'h' => array(283,334),
's' => array(20,100)
)
);
);
80 changes: 80 additions & 0 deletions tests/RandomColorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Colors\Tests;

use Colors\RandomColor;
use PHPUnit\Framework\TestCase;

class RandomColorTest extends TestCase
{
public function testOneShouldReturnRandomColor()
{
$hexColor = RandomColor::one();

$this->assertContains('#', $hexColor);
$this->assertSame(7, strlen($hexColor));
$this->assertRegExp('/#\w{6}/', $hexColor);
}

public function testManyShouldReturnTenGreenColors()
{
$hexColors = RandomColor::many(10, [
'hue' => 'green',
]);

$this->assertCount(10, $hexColors);
foreach ($hexColors as $hexColor) {
$this->assertContains('#', $hexColor);
$this->assertSame(7, strlen($hexColor));
$this->assertRegExp('/#\w{6}/', $hexColor);
}
}

public function testOneShouldReturnRandomLightBlueColor()
{
$hexColor = RandomColor::one([
'luminosity' => 'light',
'hue' => 'blue',
]);

$this->assertContains('#', $hexColor);
$this->assertSame(7, strlen($hexColor));
$this->assertRegExp('/#\w{6}/', $hexColor);
}

public function testOneShouldReturnRandomLightBlueOrYellowColor()
{
$hexColor = RandomColor::one([
'hue' => ['yellow', 'blue'],
]);

$this->assertContains('#', $hexColor);
$this->assertSame(7, strlen($hexColor));
$this->assertRegExp('/#\w{6}/', $hexColor);
}

public function testOneShouldReturnTrulyRandomColor()
{
$hexColor = RandomColor::one([
'luminosity' => 'random',
'hue' => 'random',
]);

$this->assertContains('#', $hexColor);
$this->assertSame(7, strlen($hexColor));
$this->assertRegExp('/#\w{6}/', $hexColor);
}

public function testOneShouldReturnRandomBrightRgbColor()
{
$hexColor = RandomColor::one([
'luminosity' => 'bright',
'format' => 'rgbCss',
]);

$this->assertContains('rgb(', $hexColor);
$this->assertContains(')', $hexColor);
$this->assertContains(',', $hexColor);
$this->assertRegExp('/rgb\( *(\d{1,3} *, *\d{1,3} *, *\d{1,3}) *\)/i', $hexColor);
}
}