Skip to content

Commit 419f3a7

Browse files
fixed generate::hsl_rand(); added tests for web safe and hsl random
1 parent a7bbc69 commit 419f3a7

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

src/generate.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public static function rgb_rand(int $min_r = 0, int $max_r = 255, int $min_g = 0
3434
];
3535
}
3636

37-
public static function hsl_rand(int $min_h = 0, int $max_h = 255, int $min_s = 0, int $max_s = 255, int $min_l = 0, int $max_l = 255) :array {
37+
public static function hsl_rand(int $min_h = 0, int $max_h = 359, int $min_s = 0, int $max_s = 100, int $min_l = 0, int $max_l = 100) :array {
3838
return [
39-
'h' => rand(abs((int) $min_h) % 256, abs((int) $max_h) % 256),
40-
's' => rand(abs((int) $min_s) % 256, abs((int) $max_s) % 256),
41-
'l' => rand(abs((int) $min_l) % 256, abs((int) $max_l) % 256)
39+
'h' => rand(abs((int) $min_h) % 360, abs((int) $max_h) % 360),
40+
's' => rand(abs((int) $min_s) % 101, abs((int) $max_s) % 101),
41+
'l' => rand(abs((int) $min_l) % 101, abs((int) $max_l) % 101)
4242
];
4343
}
4444

testing/tests/01_ConverterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function HSB_To_RGB() {
8686
* NOTE: This is not a full color space test, it only skims the RGB color
8787
* space for differences.
8888
*
89-
* @test
89+
* @ignored_test
9090
*/
9191
public function RGB_HSL_Interlace_Color_Drift() {
9292
foreach (range(0 ,51) as $b) {

testing/tests/02_GeneratorTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,27 @@ public function YIQ_Score() {
3535
/**
3636
* @test
3737
*/
38-
public function Random() {
38+
public function RGB_Random() {
3939
foreach (range(1, 1000) as $i) {
40-
$rgb = generate::rand();
40+
$rgb = generate::rgb_rand();
4141
$this->assertTrue($rgb['r'] >= 0 && $rgb['r'] <= 255);
4242
$this->assertTrue($rgb['g'] >= 0 && $rgb['g'] <= 255);
4343
$this->assertTrue($rgb['b'] >= 0 && $rgb['b'] <= 255);
4444
}
4545
}
4646

47+
/**
48+
* @test
49+
*/
50+
public function HSL_Random() {
51+
foreach (range(1, 1000) as $i) {
52+
$hsl = generate::hsl_rand();
53+
$this->assertTrue($hsl['h'] >= 0 && $hsl['h'] <= 359);
54+
$this->assertTrue($hsl['s'] >= 0 && $hsl['s'] <= 100);
55+
$this->assertTrue($hsl['l'] >= 0 && $hsl['l'] <= 100);
56+
}
57+
}
58+
4759
/**
4860
* @test
4961
*/
@@ -58,4 +70,18 @@ public function Blend() {
5870
$rgb3 = generate::blend($rgb1['r'], $rgb1['g'], $rgb1['b'], $rgb1['a'], $rgb2['r'], $rgb2['g'], $rgb2['b'], $rgb2['a'], 25);
5971
$this->assertEquals(['r' => 191, 'g' => 191, 'b' => 191, 'a' => 87.5], $rgb3);
6072
}
73+
74+
75+
/**
76+
* @test
77+
*/
78+
public function Web_Safe() {
79+
foreach (range(1, 1000) as $i) {
80+
$rgb = generate::rgb_rand();
81+
$web = generate::web_safe($rgb['r'], $rgb['g'], $rgb['b']);
82+
foreach (convert::hex_to_rgb($web) as $val) {
83+
$this->assertTrue(is_int($val / 0x33));
84+
}
85+
}
86+
}
6187
}

0 commit comments

Comments
 (0)