getColourByHashImpl

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

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.

version(Colours)
private pure @safe nothrow
getColourByHashImpl
(
const string word
,
const bool brightTerminal
,
const bool extendedColours
)

Parameters

word string

String to hash and base colour on.

brightTerminal bool

Whether the terminal has a bright background or not.

extendedColours bool

Whether to use extended colours or not.

Return Value

Type: auto

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

Examples

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

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

See Also