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 brightTerminal bool. If the extendedColours bool 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(brightTerminal: false, extendedColours: true);
import std.conv : to;

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

See Also