Colours
ui::color takes a hex string at the call site. s.bg("#ff8800") and t.ink("#fff") need no helper, and a wrong string stops the build.
The packed format
A colour is one uint32_t laid out 0xaabbggrr, red in the low byte. That has not changed. ui::color is the same four bytes, and it converts to uint32_t on its own, so it goes anywhere a packed word does. See colors for the byte order and why a hex literal reads backwards.
style::bg, style::border, style::gradient and text_style::ink take a ui::color, and so does every colour slot: the ui::theme fields, chart included, look_step, the shadow layers and the kit colour parameters. A uint32_t converts to a ui::color on its own, so a packed word still goes in. gfx_* calls keep taking uint32_t.
Hex strings
| Form | Meaning |
|---|---|
"#rgb" | Each digit is doubled, so "#f80" is "#ff8800". |
"#rgba" | The same, with an alpha digit. |
"#rrggbb" | Opaque. |
"#rrggbbaa" | Alpha last, as in css. |
"white", "black", "transparent" | The only names. White and black are opaque. Transparent is zero. |
Digits can be upper or lower case. With no alpha the colour is opaque. The string reads the way a design tool shows it, rrggbbaa, and the packing to 0xaabbggrr is done for you.
#include "isolenui/ui/ui.h"
static void swatches(ui::ctx* u)
{
ui_box(u, ui::col().pad(16.0f).gap(8.0f).bg("#0b0b0d").round(10.0f).border(1.0f, "#ffffff1f"))
{
ui_box(u, ui::row().height(24.0f).bg("#ff8800")) {}
ui_box(u, ui::row().height(24.0f).bg("#f80")) {}
ui_box(u, ui::row().height(24.0f).bg("#ff880080")) {}
ui_box(u, ui::row().height(24.0f).bg("#F808")) {}
ui_box(u, ui::row().height(24.0f).bg("white")) {}
ui::text(u, "hex", ui::body(u).ink("#fff"));
}
}
static void gradient_banner(ui::ctx* u)
{
ui_box(u, ui::row().grow().height(120.0f).round(12.0f)
.gradient("#8a64e5", "#2a1373", 0.5f, 0.78f))
{
ui::text(u, "isolen", ui::head(u).ink("white"));
}
}
static_assert(ui::color("#ff8800") == 0xff0088ffu);
static_assert(ui::color("#f80") == ui::color("#ff8800"));
static_assert(ui::color("#ff880080") == 0x800088ffu);
static_assert(ui::color("transparent") == 0u);Compile time only
The string constructor is consteval. The compiler reads the string while it builds, so each call site becomes one immediate load. There is no parser in the colour api at run time, and a string that is only known at run time cannot be passed at all. The colour picker kit has its own hex_parse and hex_format for text a user types. See colour picker. They are a separate thing.
rgb and rgba
| Call | Result |
|---|---|
rgb(r, g, b) | Opaque. Each channel is clamped to 0 to 255. |
rgb("#hex") | The same as the string on its own. |
rgba(r, g, b) | Opaque. |
rgba(r, g, b, double a) | The alpha is always a fraction, 0 to 1, as in css. Outside that range it clamps. |
rgba8(r, g, b, int a) | The alpha is a byte, 0 to 255. |
"#ff8800"_rgb | The literal form. ui::literals is an inline namespace, so using namespace ui is enough. |
rgba takes no integer alpha. rgba(0, 0, 0, 150) and rgba(0, 0, 0, 1) are compile errors, the overload is deleted. Write rgba8(0, 0, 0, 150) for the byte, or rgba(0, 0, 0, 1.0) for the fraction. MSVC says C2280, GCC says use of deleted function.#include "isolenui/ui/ui.h"
using namespace ui;
static void helpers(ui::ctx* u)
{
ui_box(u, ui::col().pad(16.0f).bg(rgb(11, 11, 13))
.border(1.0f, rgb(255, 136, 0)))
{
ui_box(u, ui::row().height(24.0f).bg(rgb("#ff8800"))) {}
ui_box(u, ui::row().height(24.0f).bg(rgba(0, 0, 0, 0.5))) {}
ui_box(u, ui::row().height(24.0f).bg(rgba8(0, 0, 0, 150))) {}
ui_box(u, ui::row().height(24.0f).bg("#ff8800"_rgb)) {}
}
}
static_assert(rgba(0, 0, 0, 0.5) == 0x80000000u);
static_assert(rgba8(0, 0, 0, 128) == 0x80000000u);
static_assert(rgba(0, 0, 0, 1) == 0xff000000u);
static_assert(rgba(0, 0, 0, 1.0) == 0xff000000u);
static_assert(rgb(255, 136, 0) == ui::color("#ff8800"));with_alpha
with_alpha(colour, k) scales the alpha the colour already carries by k. It does not set it. On an opaque colour, with_alpha("#ff8800", 0.5f) is half transparent, and on a colour that is already half transparent it gives a quarter. A negative k counts as zero and the result stops at 255. It takes a ui::color, so a hex string bakes at compile time, and k can be a run time value.
#include "isolenui/ui/ui.h"
using namespace ui;
static void fades(ui::ctx* u, float k)
{
ui_box(u, ui::row().height(24.0f).bg(with_alpha("#ff8800", 0.5f))) {}
ui_box(u, ui::row().height(24.0f).bg(with_alpha("#ff8800", k))) {}
ui_box(u, ui::row().height(24.0f).bg(with_alpha(rgba(255, 136, 0, 0.5), 0.5f))) {}
}
static_assert(with_alpha("#ff8800", 0.5f) == 0x800088ffu);
static_assert(with_alpha("#ff880080", 0.5f) == 0x400088ffu);
static_assert(with_alpha("#ff8800", 2.0f) == 0xff0088ffu);Theme fields
The fields of ui::theme are ui::color, so a bare string goes straight in.
#include "isolenui/ui/ui.h"
using namespace ui;
static ui::ctx* boot_dark(const text_font* font, gfx_font handle)
{
ui::theme t = ui::theme_hud();
t.bg = "#0b0b0d";
t.panel = "#141416";
t.accent = "#8a64e5";
t.accent_dim = "#8a64e540";
t.scrim = "#000000b0";
t.chart[0] = "#ff8800";
return ui::ctx_create(font, handle, &t);
}sunk, scrim and the chart entries. "transparent" is zero, so it will not be transparent there. Use a colour with a small alpha, such as "#00000001", when you want almost nothing. See themes.Errors
A bad string is an error at the line that wrote it. Each kind ends in a function that is declared and never defined, so the compiler cannot evaluate the call and stops. GCC words it like this.
| Source | Error |
|---|---|
"#ff88001" | call to non-'constexpr' function 'void ui::color_bad_hex_length()' |
"#gg0000" | call to non-'constexpr' function 'void ui::color_bad_hex_digit()' |
"" | call to non-'constexpr' function 'void ui::color_bad_hex_prefix()' |
"red" | call to non-'constexpr' function 'void ui::color_unknown_name()' |
a const char* variable | could not convert 'p' from 'const char*' to 'ui::color', since only a string literal converts. |
MSVC says C7595: call to immediate function is not a constant expression for a bad string and C2665: no overloaded function could convert all the argument types for a pointer. Other compilers word it their own way.
#include "isolenui/ui/ui.h"
ui::color a = "#ff88001";
ui::color b = "#gg0000";
ui::color c = "red";
ui::color d = "";
ui::style tint(const char* from_a_file)
{
return ui::row().bg(from_a_file);
}
ui::style pick(bool dark)
{
return ui::row().bg(dark ? "#fff" : "#ff8800");
}
ui::style array()
{
char hex[8] = "#ff8800";
return ui::row().bg(hex);
}
ui::style fine(bool dark)
{
return ui::row().bg(dark ? ui::color("#fff") : ui::color("#ff8800"));
}Limits
- A ternary of two literals of different lengths,
b ? "#fff" : "#ff8800", does not compile. The two arrays have different types. Pick the string inside each branch of anif, or writeb ? ui::color("#fff") : ui::color("#ff8800"). - A
chararray that is not a literal, such aschar a[8] = "#ff8800", does not compile. The string has to be a literal at the call. - A ternary that mixes a
ui::colorand auint32_t, or a plain0u, is ambiguous because each converts to the other. Make both sides one type withui::color(0u)or.v. - An integer alpha on
rgba,rgba(0, 0, 0, 150)orrgba(0, 0, 0, 1), does not compile. Usergba8for a byte or a fraction like1.0.
Migrating
Raw packed words still work in every colour slot, and a ui::color converts to uint32_t for gfx_fill and anything else that wants a word. The one thing to change is a byte alpha in rgba, which becomes rgba8. Move a call site to hex when you next touch it.
#include "isolenui/ui/ui.h"
static void both(ui::ctx* u, gfx_list* l, ui::color from_a_theme)
{
ui_box(u, ui::row().bg(0xff0088ffu)) {}
ui_box(u, ui::row().bg("#ff8800")) {}
ui_box(u, ui::row().bg(from_a_theme)) {}
ui_box(u, ui::row().bg(ui::rgb(255, 136, 0))) {}
uint32_t word = ui::rgb("#ff8800");
ui::color c = "#ff8800";
gfx_fill(l, 0.0f, 0.0f, 8.0f, 8.0f, ui::rgb("#ff8800"));
}