Skip to content

Commit b63a4e0

Browse files
committed
Random Bg color. Fixes #32
1 parent 5146d72 commit b63a4e0

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

src/InitialAvatar.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class InitialAvatar
4040
protected $fontName = 'OpenSans, sans-serif';
4141
protected $generated_initials = 'JD';
4242
protected $preferBold = false;
43+
protected $randomBgColor = false;
4344

4445
/**
4546
* Language eg.en zh-CN.
@@ -202,9 +203,31 @@ public function preferRegular()
202203
public function background($background)
203204
{
204205
$this->bgColor = (string) $background;
206+
$this->randomBgColor = false;
205207

206208
return $this;
207209
}
210+
211+
/**
212+
* Set background color to be randomly generated for each avatar.
213+
*
214+
* @param bool $random
215+
* @param int $saturation Saturation value (0-100)
216+
* @param int $luminance Luminance value (0-100)
217+
*
218+
* @return $this
219+
*/
220+
public function randomBackground($random = true, int $saturation = 85, int $luminance = 60)
221+
{
222+
$this->randomBgColor = (bool) $random;
223+
224+
if ($random) {
225+
// Generate an initial random color
226+
$this->generateRandomColor($saturation, $luminance);
227+
}
228+
229+
return $this;
230+
}
208231

209232
/**
210233
* Set the font color.
@@ -437,13 +460,29 @@ public function getInitials()
437460

438461
/**
439462
* Will return the background color parameter.
463+
* If randomBgColor is enabled, generates a new random color each time.
440464
*
441465
* @return string
442466
*/
443467
public function getBackgroundColor()
444468
{
469+
if ($this->randomBgColor) {
470+
// Generate a new random color each time this method is called
471+
$this->generateRandomColor();
472+
}
473+
445474
return $this->bgColor;
446475
}
476+
477+
/**
478+
* Will return whether random background color is enabled.
479+
*
480+
* @return bool
481+
*/
482+
public function getRandomBackgroundColor()
483+
{
484+
return $this->randomBgColor;
485+
}
447486

448487
/**
449488
* Will return the set driver.
@@ -921,4 +960,26 @@ protected function getContrastColor($hexColor)
921960
return '#FFFFFF';
922961
}
923962
}
963+
964+
/**
965+
* Generate a random background color.
966+
*
967+
* @param int $saturation Saturation value (0-100)
968+
* @param int $luminance Luminance value (0-100)
969+
*
970+
* @return string The generated hex color
971+
*/
972+
protected function generateRandomColor(int $saturation = 85, int $luminance = 60)
973+
{
974+
// Generate a random hue (0-1)
975+
$hue = mt_rand(0, 359) / 360;
976+
$saturation /= 100;
977+
$luminance /= 100;
978+
979+
$hexColor = $this->convertHSLtoRGB($hue, $saturation, $luminance);
980+
$this->bgColor = $hexColor;
981+
$this->fontColor = $this->getContrastColor($hexColor);
982+
983+
return $hexColor;
984+
}
924985
}

tests/RandomBackgroundTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use LasseRafn\InitialAvatarGenerator\InitialAvatar;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class RandomBackgroundTest extends TestCase
7+
{
8+
/** @test */
9+
public function it_generates_random_background_colors()
10+
{
11+
$avatar = new InitialAvatar();
12+
$avatar->randomBackground(true);
13+
14+
// Verify random background is enabled
15+
$this->assertTrue($avatar->getRandomBackgroundColor());
16+
17+
// Get first color
18+
$firstColor = $avatar->getBackgroundColor();
19+
20+
// Get second color - should be different
21+
$secondColor = $avatar->getBackgroundColor();
22+
23+
// Colors should be different due to random generation
24+
$this->assertNotEquals($firstColor, $secondColor);
25+
26+
// Disable random background
27+
$avatar->randomBackground(false);
28+
$this->assertFalse($avatar->getRandomBackgroundColor());
29+
30+
// Set fixed background
31+
$fixedColor = '#123456';
32+
$avatar->background($fixedColor);
33+
34+
// Get color multiple times - should be the same
35+
$this->assertEquals($fixedColor, $avatar->getBackgroundColor());
36+
$this->assertEquals($fixedColor, $avatar->getBackgroundColor());
37+
}
38+
39+
/** @test */
40+
public function it_generates_different_avatars_with_random_background()
41+
{
42+
$avatar = new InitialAvatar();
43+
$avatar->randomBackground(true);
44+
45+
// Generate two avatars
46+
$firstAvatar = $avatar->generate('Test User');
47+
$firstBgColor = $avatar->getBackgroundColor();
48+
49+
$secondAvatar = $avatar->generate('Test User');
50+
$secondBgColor = $avatar->getBackgroundColor();
51+
52+
// Background colors should be different
53+
$this->assertNotEquals($firstBgColor, $secondBgColor);
54+
55+
// SVG test
56+
$avatar = new InitialAvatar();
57+
$avatar->randomBackground(true);
58+
59+
// Generate two SVG avatars
60+
$firstSvg = $avatar->generateSvg('Test User');
61+
$firstSvgBgColor = $avatar->getBackgroundColor();
62+
63+
$secondSvg = $avatar->generateSvg('Test User');
64+
$secondSvgBgColor = $avatar->getBackgroundColor();
65+
66+
// Background colors should be different
67+
$this->assertNotEquals($firstSvgBgColor, $secondSvgBgColor);
68+
}
69+
}

0 commit comments

Comments
 (0)