MOnsDaR
MOnsDaR

Reputation: 8666

Algorithm: Create color from string

I want to create a color from a given string. The string does not have to be related to the resulting color in any form, but the same string should always result in the same color.

This question is not bound to a specific programming language, so the "Color" should be in a language-independent format like RGB.

It would be good if the algorithm creates colors in a wide colorspectrum and not just greyish colors.

Perfectly would be something like this (C++):

#include <string>

int getRedFromString( std::string givenString )
{ /*Your code here...*/ }

int getGreenFromString( std::string givenString )
{ /*Your code here...*/ }

int getBlueFromString( std::string givenString )
{ /*Your code here...*/ }

int main()
{
    std::string colorString = "FooBar";
    int R = getRedFromString  ( colorString );
    int G = getGreenFromString( colorString );
    int B = getBlueFromString ( colorString );
}

Upvotes: 10

Views: 2651

Answers (6)

Larry Watanabe
Larry Watanabe

Reputation: 10184

You can compute the Godel number of the string. Basically it would be (int)A[0] * 256 ^ n + (int) a[1] * 256 ^ (n-1) .... + (int)A[0]

Just same idea as our number system, but using base 256 because there are 256 possible character values.

Next, just reduce by a factor for the range of the spectrum you want to map to:

e.g. suppose you want into range 0 ... 2000

Then just take whatever number you get and divide by (largest number in your range)/2000

The advantage of this approach is that it will give you a broader range of colors than just RGB. However, if you want the simplicity of the 3 primary colors, then you can just divide by 3 instead and take different ranges, or take mod 3.

Upvotes: 1

Drake
Drake

Reputation: 3891

#include <string>
#include <locale>

using namespace std;

int main()
{
    locale loc;  
    string colorString;
    COLORREF color;

    colorString = "FooBar";

    const collate<char>& coll = use_facet<collate<char> >(loc);

    color = coll.hash(colorString.data(), colorString.data()+ colorString.length());
}

Example of the hash

Upvotes: 1

Gabi Purcaru
Gabi Purcaru

Reputation: 31564

I will have a try with an MD5 on the string:

from hashlib import md5

def get_color_tuple(item)
    hash = md5(item).hexdigest()
    hash_values = (hash[:8], hash[8:16], hash[16:24]) # note: we ignore the values from 24 to 32, but it shouldn't be a problem.
    return tuple(int(value, 16)%256 for value in hash_values)

What the algorithm does is basically this: it gets the first three chunks of 4 bytes (i.e. 8 characters) , and returns them in a tuple modulo 256, so that their range will be in [0, 255]

Upvotes: 1

Guffa
Guffa

Reputation: 700770

You could use any hashing algorithm to create a value from the string that is always the same for any given string, and get the color components from that.

The GetHashCode method in .NET for example returns an integer, so it would be easy to create an RGB value from that:

int RGB = colorString.GetHashCode() & FFFFFFh;

or

int code = colorString.GetHashCode();
int B = code & FFh;
code >>= 8;
int G = code & FFh;
code >>= 8;
int R = code & FFh;

Upvotes: 1

Tavison
Tavison

Reputation: 1563

There's a number of ways to do this based on what you are trying to accomplish. The easiest is to turn the string into a stream with str_stream and read the text values as unsigned chars.

Upvotes: 0

SLaks
SLaks

Reputation: 888157

Take a hash of the string, then use the first three bytes of the hash as Red, Blue, and Green values.

Upvotes: 10

Related Questions