3v4l.org

run code in 300+ PHP versions simultaneously
<?php define( COL_MIN_AVG, 64 ); define( COL_MAX_AVG, 192 ); define( COL_STEP, 16 ); // (192 - 64) / 16 = 8 // 8 ^ 3 = 512 colors function usercolor( $username ) { $range = COL_MAX_AVG - COL_MIN_AVG; $factor = $range / 256; $offset = COL_MIN_AVG; $base_hash = substr(md5($username), 0, 6); $b_R = hexdec(substr($base_hash,0,2)); $b_G = hexdec(substr($base_hash,2,2)); $b_B = hexdec(substr($base_hash,4,2)); $f_R = floor((floor($b_R * $factor) + $offset) / COL_STEP) * COL_STEP; $f_G = floor((floor($b_G * $factor) + $offset) / COL_STEP) * COL_STEP; $f_B = floor((floor($b_B * $factor) + $offset) / COL_STEP) * COL_STEP; return printf('#%02x%02x%02x', $f_R, $f_G, $f_B); } for( $i=0; $i<30; $i++ ) { printf('<div style="height: 100px; width: 100px; background-color: %s">&nbsp;</div>'."\n", usercolor(rand())); }

preferences:
34.88 ms | 402 KiB | 5 Q