Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate random color for background and font color #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 39 additions & 2 deletions src/InitialAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,44 @@ public function background( $background ) {
return $this;
}

/**
/**
* Generate random color
* Stolen from here: https://stackoverflow.com/a/5614583/1103397
*
* @return string Random hex color
*/
public function generateRandomColor() {
$color = [];
for ($i = 0; $i < 3; $i++){
$color[] = str_pad(dechex(mt_rand(0, 255)), 2, '0', STR_PAD_LEFT);
}

return implode('', $color);
}

/**
* Set random background color
*
* @return $this
*/
public function randBackground() {
$this->background($this->generateRandomColor());

return $this;
}

/**
* Set random font color
*
* @return $this
*/
public function randColor() {
$this->color($this->generateRandomColor());

return $this;
}

/**
* Set the font color.
*
* @param string $color
Expand Down Expand Up @@ -450,4 +487,4 @@ private function getFontByScript() {

return $this->getFontFile();
}
}
}