Skip to content

Commit ae5af5e

Browse files
committed
provide custom pseudo-random generator with prng option
1 parent 0a4b44a commit ae5af5e

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/RandomColor.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22
/**
3-
* RandomColor 1.0.0
3+
* RandomColor 1.0.1
44
*
55
* PHP port of David Merfield JavaScript randomColor
66
* https://github.com/davidmerfield/randomColor
77
*
88
*
99
* The MIT License (MIT)
10-
*
10+
*
1111
* Copyright (c) 2014 Damien "Mistic" Sorel
1212
*
1313
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -16,10 +16,10 @@
1616
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1717
* copies of the Software, and to permit persons to whom the Software is
1818
* furnished to do so, subject to the following conditions:
19-
*
19+
*
2020
* The above copyright notice and this permission notice shall be included in all
2121
* copies or substantial portions of the Software.
22-
*
22+
*
2323
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2424
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2525
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -66,7 +66,7 @@ static public function format($hsv, $format='hex')
6666
{
6767
case 'hsv':
6868
return $hsv;
69-
69+
7070
case 'hsl':
7171
return self::hsv2hsl($hsv);
7272

@@ -95,7 +95,7 @@ static private function _pickHue($options)
9595
return 0;
9696
}
9797

98-
$hue = mt_rand($range[0], $range[1]);
98+
$hue = self::_rand($range, $options);
9999

100100
// Instead of storing red as two seperate ranges,
101101
// we group them, using negative numbers
@@ -111,7 +111,7 @@ static private function _pickSaturation($h, $options)
111111
{
112112
if (@$options['luminosity'] === 'random')
113113
{
114-
return mt_rand(0, 100);
114+
return self::_rand(array(0, 100), $options);
115115
}
116116
if (@$options['hue'] === 'monochrome')
117117
{
@@ -136,7 +136,7 @@ static private function _pickSaturation($h, $options)
136136
break;
137137
}
138138

139-
return mt_rand($range[0], $range[1]);
139+
return self::_rand($range, $options);
140140
}
141141

142142
static private function _pickBrightness($h, $s, $options)
@@ -164,7 +164,7 @@ static private function _pickBrightness($h, $s, $options)
164164
}
165165
}
166166

167-
return mt_rand($range[0], $range[1]);
167+
return self::_rand($range, $options);
168168
}
169169

170170
static private function _getHueRange($options)
@@ -229,6 +229,18 @@ static private function _getColorInfo($h)
229229
}
230230
}
231231

232+
static private function _rand($bounds, $options)
233+
{
234+
if (isset($options['prng']))
235+
{
236+
return $options['prng']($bounds[0], $bounds[1]);
237+
}
238+
else
239+
{
240+
return mt_rand($bounds[0], $bounds[1]);
241+
}
242+
}
243+
232244
static public function hsv2hex($hsv)
233245
{
234246
$rgb = self::hsv2rgb($hsv);
@@ -241,15 +253,15 @@ static public function hsv2hex($hsv)
241253

242254
return $hex;
243255
}
244-
256+
245257
static public function hsv2hsl($hsv)
246258
{
247259
extract($hsv);
248-
260+
249261
$s/= 100;
250262
$v/= 100;
251263
$k = (2-$s)*$v;
252-
264+
253265
return array(
254266
'h' => $h,
255267
's' => round($s*$v / ($k < 1 ? $k : 2-$k), 4) * 100,
@@ -298,7 +310,7 @@ static public function hsv2rgb($hsv)
298310
list($r,$g,$b) = array($v,$m,$n);
299311
break;
300312
}
301-
313+
302314
return array(
303315
'r' => floor($r*255),
304316
'g' => floor($g*255),

0 commit comments

Comments
 (0)