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 brightTerminal bool. If extendedColours: true 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, bool brightTerminal, bool extendedColours)
    version(Colours)
    pure @safe nothrow
    getColourByHash
    (
    const string word
    ,
    const bool brightTerminal = CoreSettings.init.brightTerminal
    ,
    const bool extendedColours = CoreSettings.init.extendedColours
    )
  2. auto getColourByHash(string word, CoreSettings settings)

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: true);
immutable otherColour = "kameloso^".getColourByHash(extendedColours: false);
import std.conv : to;

{
    enum word = "kameloso";
    immutable hash = getColourByHash(word, brightTerminal: false, extendedColours: true);
    assert((hash == 227), hash.to!string);
}
{
    enum word = "kameloso";
    immutable hash = getColourByHash(word, brightTerminal: false);
    assert((hash == 227), hash.to!string);
}
{
    enum word = "kameloso";
    immutable hash = getColourByHash(word, extendedColours: true);
    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,
        brightTerminal: true,
        extendedColours: true);
    assert((hash == 35), hash.to!string);
}
{
    enum word = "NO";
    immutable hash = getColourByHash(
        word,
        brightTerminal: true,
        extendedColours: true);
    assert((hash == 90), hash.to!string);
}

See Also