Skip to content

Commit

Permalink
Closes #3 Provide multiple hues - lighter dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Sep 8, 2015
1 parent 9765a6f commit 68c2871
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 48 deletions.
9 changes: 7 additions & 2 deletions README.md
Expand Up @@ -8,13 +8,13 @@ This is a PHP port of David Merfield [randomColor](https://github.com/davidmerfi

See the results on [the demo](http://www.strangeplanet.fr/work/RandomColor.php).

[![Demo](http://llllll.li/randomColor/repo_demo.gif)](http://www.strangeplanet.fr/work/RandomColor.php)
[![Demo](https://raw.githubusercontent.com/mistic100/RandomColor.php/master/demo/screenshot.jpg)](http://www.strangeplanet.fr/work/RandomColor.php)

### Options

You can pass an options object to influence the type of color it produces. The options object accepts the following properties:

**hue** – Controls the hue of the generated color. You can pass a string representing a color name (e.g. 'orange'). Possible color names are *red, orange, yellow, green, blue, purple, pink and monochrome*.
**hue** – Controls the hue of the generated color. You can pass a string representing a color name (e.g. 'orange'). Possible color names are *red, orange, yellow, green, blue, purple, pink and monochrome*. You can also pass an array of multiple hues or a specific hue (0 to 360).

**luminosity** – Controls the luminosity of the generated color. You can pass a string containing *bright, light or dark*.

Expand Down Expand Up @@ -42,6 +42,11 @@ RandomColor::one(array(
'hue' => 'blue'
));

// Returns one yellow or blue color
RandomColors::one(array(
'hue' => array('yellow', 'blue')
));

// Returns a hex code for a 'truly random' color
RandomColor::one(array(
'luminosity' => 'random',
Expand Down
2 changes: 1 addition & 1 deletion composer.json
@@ -1,7 +1,7 @@
{
"name": "mistic100/randomcolor",
"description": "Generate attractive random colors",
"version": "1.0.3",
"version": "1.0.4",
"license": "MIT",
"authors": [{
"name": "Damien \"Mistic\" Sorel",
Expand Down
8 changes: 8 additions & 0 deletions demo/index.php
Expand Up @@ -151,6 +151,14 @@
?>
</div>

<h3>Multiple colors</h3>
<pre>RandomColor::many(27, array('hue'=>array('blue', 'yellow')));</pre>
<div class="output">
<?php
foreach (RandomColor::many(27, array('hue'=>array('blue', 'yellow'))) as $c) echo '<span style="background:' . $c . ';"></span>';
?>
</div>

<h3>Light colors</h3>
<pre>RandomColor::many(27, array('luminosity'=>'light'));</pre>
<div class="output">
Expand Down
Binary file added demo/screenshot.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 66 additions & 45 deletions src/RandomColor.php
Expand Up @@ -88,14 +88,14 @@ static private function _pickHue($options)
{
$range = self::_getHueRange($options);

if ($range === null)
if (empty($range))
{
return 0;
}

$hue = self::_rand($range, $options);

// Instead of storing red as two seperate ranges,
// Instead of storing red as two separate ranges,
// we group them, using negative numbers
if ($hue < 0)
{
Expand All @@ -117,7 +117,7 @@ static private function _pickSaturation($h, $options)
}

$colorInfo = self::_getColorInfo($h);
$range = $colorInfo['saturationRange'];
$range = $colorInfo['s'];

switch (@$options['luminosity'])
{
Expand Down Expand Up @@ -167,30 +167,55 @@ static private function _pickBrightness($h, $s, $options)

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

if (isset($options['hue']))
{
if (isset(self::$dictionary[$options['hue']]))
if (!is_array($options['hue']))
{
return self::$dictionary[$options['hue']]['hueRange'];
$options['hue'] = array($options['hue']);
}
else if (is_numeric($options['hue']))
{
$hue = intval($options['hue']);

if ($hue < 360 && $hue >= 0)
foreach ($options['hue'] as $hue)
{
if ($hue === 'random')
{
$ranges[] = array(0, 360);
}
else if (isset(self::$dictionary[$hue]))
{
$ranges[] = self::$dictionary[$hue]['h'];
}
else if (is_numeric($hue))
{
return array($hue, $hue);
$hue = intval($hue);

if ($hue <= 360 && $hue >= 0)
{
$ranges[] = array($hue, $hue);
}
}
}
}

return array(0, 360);
if (($l = count($ranges)) === 0)
{
return array(0, 360);
}
else if ($l === 1)
{
return $ranges[0];
}
else
{
return $ranges[self::_rand(array(0, $l-1), $options)];
}
}

static private function _getMinimumBrightness($h, $s)
{
$colorInfo = self::_getColorInfo($h);
$bounds = $colorInfo['lowerBounds'];
$bounds = $colorInfo['bounds'];

for ($i = 0, $l = count($bounds); $i < $l - 1; $i++)
{
Expand Down Expand Up @@ -220,7 +245,7 @@ static private function _getColorInfo($h)

foreach (self::$dictionary as $color)
{
if ($color['hueRange'] !== null && $h >= $color['hueRange'][0] && $h <= $color['hueRange'][1])
if ($color['h'] !== null && $h >= $color['h'][0] && $h <= $color['h'][1])
{
return $color;
}
Expand Down Expand Up @@ -317,53 +342,49 @@ static public function hsv2rgb($hsv)
}
}

/*
* h=hueRange
* s=saturationRange : bounds[0][0] ; bounds[-][0]
*/
RandomColor::$dictionary = array(
'monochrome' => array(
'hueRange' => NULL,
'lowerBounds' => array(array(0,0), array(100,0)),
'saturationRange' => array(0,100),
'brightnessRange' => array(0,0)
'bounds' => array(array(0,0), array(100,0)),
'h' => NULL,
's' => array(0,100)
),
'red' => array(
'hueRange' => array(-26,18),
'lowerBounds' => array(array(20,100),array(30,92),array(40,89),array(50,85),array(60,78),array(70,70),array(80,60),array(90,55),array(100,50)),
'saturationRange' => array(20,100),
'brightnessRange' => array(50,100)
'bounds' => array(array(20,100),array(30,92),array(40,89),array(50,85),array(60,78),array(70,70),array(80,60),array(90,55),array(100,50)),
'h' => array(-26,18),
's' => array(20,100)
),
'orange' => array(
'hueRange' => array(19,46),
'lowerBounds' => array(array(20,100),array(30,93),array(40,88),array(50,86),array(60,85),array(70,70),array(100,70)),
'saturationRange' => array(20,100),
'brightnessRange' => array(70,100)
'bounds' => array(array(20,100),array(30,93),array(40,88),array(50,86),array(60,85),array(70,70),array(100,70)),
'h' => array(19,46),
's' => array(20,100)
),
'yellow' => array(
'hueRange' => array(47,62),
'lowerBounds' => array(array(25,100),array(40,94),array(50,89),array(60,86),array(70,84),array(80,82),array(90,80),array(100,75)),
'saturationRange' => array(25,100),
'brightnessRange' => array(75,100)
'bounds' => array(array(25,100),array(40,94),array(50,89),array(60,86),array(70,84),array(80,82),array(90,80),array(100,75)),
'h' => array(47,62),
's' => array(25,100)
),
'green' => array(
'hueRange' => array(63,178),
'lowerBounds' => array(array(30,100),array(40,90),array(50,85),array(60,81),array(70,74),array(80,64),array(90,50),array(100,40)),
'saturationRange' => array(30,100),
'brightnessRange' => array(40,100)
'bounds' => array(array(30,100),array(40,90),array(50,85),array(60,81),array(70,74),array(80,64),array(90,50),array(100,40)),
'h' => array(63,178),
's' => array(30,100)
),
'blue' => array(
'hueRange' => array(179,257),
'lowerBounds' => array(array(20,100),array(30,86),array(40,80),array(50,74),array(60,60),array(70,52),array(80,44),array(90,39),array(100,35)),
'saturationRange' => array(20,100),
'brightnessRange' => array(35,100)
'bounds' => array(array(20,100),array(30,86),array(40,80),array(50,74),array(60,60),array(70,52),array(80,44),array(90,39),array(100,35)),
'h' => array(179,257),
's' => array(20,100)
),
'purple' => array(
'hueRange' => array(258,282),
'lowerBounds' => array(array(20,100),array(30,87),array(40,79),array(50,70),array(60,65),array(70,59),array(80,52),array(90,45),array(100,42)),
'saturationRange' => array(20,100),
'brightnessRange' => array(42,100)
'bounds' => array(array(20,100),array(30,87),array(40,79),array(50,70),array(60,65),array(70,59),array(80,52),array(90,45),array(100,42)),
'h' => array(258,282),
's' => array(20,100)
),
'pink' => array(
'hueRange' => array(283,334),
'lowerBounds' => array(array(20,100),array(30,90),array(40,86),array(60,84),array(80,80),array(90,75),array(100,73)),
'saturationRange' => array(20,100),
'brightnessRange' => array(73,100)
'bounds' => array(array(20,100),array(30,90),array(40,86),array(60,84),array(80,80),array(90,75),array(100,73)),
'h' => array(283,334),
's' => array(20,100)
)
);

0 comments on commit 68c2871

Please sign in to comment.