rgbFromHex

Convenience wrapper that takes a hex string and populates a Voldemort struct with its RR, GG and BB components.

This is to be used when mapping a #RRGGBB colour to their decimal red/green/blue equivalents.

@safe
rgbFromHex
(
const string hexString
,
const bool acceptLowercase = false
)

Parameters

hexString string

Hexadecimal number (colour) in string form.

acceptLowercase bool

Whether or not to accept the rrggbb string in lowercase letters.

Return Value

Type: auto

A Voldemort struct with r, g and b members,

Examples

import std.conv : text;
{
    auto rgb = rgbFromHex("000102");

    assert((rgb.r == 0), rgb.r.text);
    assert((rgb.g == 1), rgb.g.text);
    assert((rgb.b == 2), rgb.b.text);
}
{
    auto rgb = rgbFromHex("#FFFFFF");

    assert((rgb.r == 255), rgb.r.text);
    assert((rgb.g == 255), rgb.g.text);
    assert((rgb.b == 255), rgb.b.text);
}
{
    auto rgb = rgbFromHex("#3C507D");

    assert((rgb.r == 60), rgb.r.text);
    assert((rgb.g == 80), rgb.b.text);
    assert((rgb.b == 125), rgb.b.text);
}
{
    auto rgb = rgbFromHex("9a4B7c", acceptLowercase: true);

    assert((rgb.r == 154), rgb.r.text);
    assert((rgb.g == 75), rgb.g.text);
    assert((rgb.b == 124), rgb.b.text);
}