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 CoreSettings struct (which has a field that signifies a bright terminal background). It also only picks from the basic colour range unless the passed CoreSettings has values that signify that the terminal supports extended colours.

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

Parameters

word string

String to hash and base colour on.

settings CoreSettings

A copy of the program-global CoreSettings.

Return Value

Type: auto

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

Examples

immutable nickColour = "kameloso".getColourByHash(*kameloso.common.settings);
import std.conv : to;

CoreSettings brightSettings;
CoreSettings darkSettings;
brightSettings.brightTerminal = true;

{
    immutable hash = getColourByHash("kameloso", darkSettings);
    assert((hash == 227), hash.to!string);
}
{
    immutable hash = getColourByHash("kameloso^", darkSettings);
    assert((hash == 46), hash.to!string);
}
{
    immutable hash = getColourByHash("zorael", brightSettings);
    assert((hash == 35), hash.to!string);
}
{
    immutable hash = getColourByHash("NO", brightSettings);
    assert((hash == 90), hash.to!string);
}

See Also