getColourByHash

Hashes the passed string and picks an ANSI colour for it by modulo.

Picks any colour, taking care not to pick black or white based on the passed Flag!"brightTerminal" flag. If the Flag!"extendedColours" flag is passed, it will pick from the extended colour range instead of the basic one.

Any number of flags may be passed. If duplicates are passed, the last one will be used.

  1. auto getColourByHash(string word, Flags flags)
    version(Colours)
    pure @safe nothrow
    getColourByHash
    (
    Flags...
    )
    (
    const string word
    ,
    const Flags flags
    )
  2. auto getColourByHash(string word, CoreSettings settings)

Parameters

word string

String to hash and base colour on.

flags Flags

A variadic combination of one or more std.typecons.Flag flags to configure the output.

Return Value

Type: auto

A uint that can be used in an ANSI foreground colour sequence.

Examples

immutable nickColour = "kameloso".getColourByHash(Yes.brightTerminal);
immutable otherColour = "kameloso^".getColourByHash(No.extendedColours);
import std.conv : to;

{
    enum word = "kameloso";
    immutable hash = getColourByHash(word, No.brightTerminal, Yes.extendedColours);
    assert((hash == 227), hash.to!string);
}
{
    enum word = "kameloso";
    immutable hash = getColourByHash(word, No.brightTerminal);
    assert((hash == 227), hash.to!string);
}
{
    enum word = "kameloso";
    immutable hash = getColourByHash(word, Yes.extendedColours);
    assert((hash == 227), hash.to!string);
}
{
    enum word = "kameloso";
    immutable hash = getColourByHash(word);
    assert((hash == 227), hash.to!string);
}
{
    enum word = "kameloso^";
    immutable hash = getColourByHash(word);
    assert((hash == 46), hash.to!string);
}
{
    enum word = "zorael";
    immutable hash = getColourByHash(
        word,
        No.brightTerminal,
        Yes.brightTerminal,
        No.extendedColours,
        Yes.extendedColours);
    assert((hash == 35), hash.to!string);
}
{
    enum word = "NO";
    immutable hash = getColourByHash(
        word,
        Yes.brightTerminal,
        Yes.extendedColours,
        Yes.extendedColours);
    assert((hash == 90), hash.to!string);
}

See Also