4

in a project I'm making I'm using numerous color codes. the point is not for them to be Beautiful, just differents.(I also want to be able to constantly have the same colors code for the same fields on refresh (no Random color generators)) I was thinking about taking the name of the fields and turn them to Hex color. Is there a pre-defined function for this?

Exemple :

$string = "Blablabla";
$colorCode = toColorCode($string);

function toColorCode($initial){
    /*MAGIC MADNESS*/
    return array("R"=>XXX,"G"=>XXX,"B"=>XXX);
}

FORGOT TO MENTION : it's important that the values are Numbers only.

5
  • On a side note is this a good idea from a UI design point of view? How will you make sure your text has a decent contrast with the background? I doubt anyone will appreciate yellow text on white background Commented Dec 19, 2011 at 16:22
  • Two questions: 1. What exactly are you expecting as an argument to toColorCode()? A word, 'white', or a hex code, 'FFFFFF'? 2. Is there a reason that you need to return an array of values instead of the actual hex code? Commented Dec 19, 2011 at 16:22
  • so, you want to turn strings like "FF5600" into 0xFF5600? Commented Dec 19, 2011 at 16:25
  • @ManosDilaverakis if he puts this just a legend color it won't be that bad. Commented Dec 19, 2011 at 16:33
  • @ManosDilaverakis that's where the importance of an array come. i can select Red Blue for 1 categorie and set Green to a fix value. i could also force a minimum value on one of the field etc... Commented Dec 19, 2011 at 17:01

1 Answer 1

3

As far as I can understand, you want to generate a fairly unique color code for a string.

The easies way is to call a checksum function on the string, for example MD5:

function toColorCode($initial){
  $checksum = md5($initial);
  return array(
    "R" => hexdec(substr($checksum, 0, 2)),
    "G" => hexdec(substr($checksum, 2, 2)),
    "B" => hexdec(substr($checksum, 4, 2))
  );
}
Sign up to request clarification or add additional context in comments.

3 Comments

would be good if MD5 returns only numerical characters, sry i forgot to mention.
Wrap the substr: hexdec(substr($checksum, 0, 2))
Thanks justin, didn't tought about it. that fixed my problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.