Skip to content

Instantly share code, notes, and snippets.

@serkanalgur
Created July 6, 2021 13:47
Show Gist options
  • Save serkanalgur/3139aada1cd74b0f6713533efb6ff09f to your computer and use it in GitHub Desktop.
Save serkanalgur/3139aada1cd74b0f6713533efb6ff09f to your computer and use it in GitHub Desktop.
Random colored user backgrounds with auto color calculation
<?php
/** Random Color Part Generator HEX **/
function RandomColorPart()
{
return mt_rand(0, 255);
}
/** Random Color Part Generator **/
/** Random Color Generator **/
function RandomColor()
{
return [
"red" => RandomColorPart(),
"blue" => RandomColorPart(),
"green" => RandomColorPart()
];
}
/** Random Color Generator **/
/** Random Colored Background **/
function RandomColoredBackground($name = "İ")
{
$bg = RandomColor();
$div ="<style>
:root {
--red: ".$bg['red'].";
--green: ".$bg['blue'].";
--blue: ".$bg['green'].";
--accessible-color: calc(
(
(
(
(var(--red) * 299) +
(var(--green) * 587) +
(var(--blue) * 114)
) / 1000
) - 128
) * -1000
);
}
.outerDiv {
background-color:
rgb(
var(--red),
var(--green),
var(--blue)
);
width:75px;
height:75px;
border-radius:50%;
}
.outerDiv p {
color:
rgb(
var(--accessible-color),
var(--accessible-color),
var(--accessible-color)
);
line-height:75px;
text-align:center;
font-weight:bold;
font-size:50px;
}</style>"."\n";
$div .= "<div class=\"outerDiv\"><p>".$name."</p></div>";
return $div;
}
/** Random Colored Background **/
/** Simple Usage */
echo RandomColoredBackground();
/** Simple Usage */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment