Considering an array of colors like this:
$cores = array('#000', '#333', '#666', '#999'); // do tamanho que você precisar
I would create a second array, associative, where you save colors for each user. It starts empty:
$coresPorUsuario = array();
And the idea is that it will contain the colors of the users as they arise. For example, array('VE' => '#000', 'ED' => '#333')
.
To control everything, use a function. It receives the user’s initials and returns the corresponding color. If the user already exists in $coresPorUsuario
, take the color that is there. If it does not exist, take the next available color, associate it to the user, and return that color. You will also need a variable to control which is the next available color.
$proxima = 0;
function corUsuario($usuario) {
global $cores;
global $coresPorUsuario;
global $proxima;
// Usuário ainda não existe na array
if(empty($coresPorUsuario[$usuario])) {
// Guarda a cor do usuário e avança para a próxima cor disponível
$coresPorUsuario[$usuario] = $cores[$proxima++];
// Se passou da quantidade de cores disponíveis, começa novamente da primeira
$proxima = $proxima == count($cores) ? 0 : $proxima;
}
// Retorna a cor do usuário
return $coresPorUsuario[$usuario];
}
WARNING: The above example uses global variables as a short path as an example. You may want to implement this function as a class method. In this case, use instance properties instead of global variables.
Testing:
echo corUsuario('AA') . "\n"; // #000
echo corUsuario('AB') . "\n"; // #333
echo corUsuario('AC') . "\n"; // #666
echo corUsuario('AA') . "\n"; // #000
echo corUsuario('AD') . "\n"; // #999
echo corUsuario('AE') . "\n"; // #000
Demonstration
So it will be at most 24 abbreviations? What if a twenty-fifth appears?
– bfavaretto
Agree, only 24? O_O Good, but if the site is small I see no problem :D
– Olimon F.
I used 24 as an example, if by chance we could solve this question I would increase the color table over time.
– Vinicius Eduardo
OK, the solution I posted supports as many colors as you have in the array, the more the better, but if the number of users exceeds the number of colors the script for.
– Olimon F.
Playing Interaction Design: What’s the Goal from the user’s perspective? It is really differentiate two people with the same abbreviation (Vinicius Eduardo and Valeria Escobar, for example) or just differentiate the abbreviations in the comments showoff on a screen? If it is the second one, the number of colors required tends to be much lower if they are not fixed. Also, perhaps the contrast between colors is more important than the choice of color itself.
– Luiz Vieira
If it is the first case (differentiate people), perhaps you can consider differentiation by occurrence of duplicity. Like, it wouldn’t matter if there was a red AP and a red VE, but if there were more than one VE there they would be differentiated by very different colors (despite having a rather large color combination in the RGB, the concern to make them have sufficient contrast should decrease a bit that number).
– Luiz Vieira
I like the way you think, I’ll adapt and use!
– Vinicius Eduardo