VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ChatColors.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <array>
5#include <functional>
6#include <string>
7#include <string_view>
8#include <vector>
9
11{
12
13// CS2 in-line chat color escape bytes. Byte values mirror the SwiftlyS2 mapping
14// (references/swiftlys2/src/api/shared/string.cpp). Aliases share a byte.
15inline constexpr std::string_view Default = "\x01";
16inline constexpr std::string_view White = "\x01";
17inline constexpr std::string_view DarkRed = "\x02";
18inline constexpr std::string_view LightPurple = "\x03";
19inline constexpr std::string_view Green = "\x04";
20inline constexpr std::string_view Olive = "\x05";
21inline constexpr std::string_view Lime = "\x06";
22inline constexpr std::string_view Red = "\x07";
23inline constexpr std::string_view Gray = "\x08";
24inline constexpr std::string_view Grey = "\x08";
25inline constexpr std::string_view Yellow = "\x09";
26inline constexpr std::string_view LightYellow = "\x09";
27inline constexpr std::string_view Silver = "\x0A";
28inline constexpr std::string_view BlueGrey = "\x0A";
29inline constexpr std::string_view LightBlue = "\x0B";
30inline constexpr std::string_view Blue = "\x0B";
31inline constexpr std::string_view DarkBlue = "\x0C";
32inline constexpr std::string_view Purple = "\x0E";
33inline constexpr std::string_view Magenta = "\x0E";
34inline constexpr std::string_view LightRed = "\x0F";
35inline constexpr std::string_view Gold = "\x10";
36inline constexpr std::string_view Orange = "\x10";
37
38/** A single entry in @ref Palette: the canonical (lowercase) name and its escape code. */
40{
41 std::string_view Name;
42 std::string_view Code;
43};
44
45/**
46 * Canonical, deduplicated list of every named color recognized by @ref ParseNamed,
47 * one entry per distinct escape byte. Aliases (e.g. `grey`/`gray`, `magenta`/`purple`)
48 * collapse to a single canonical name. Useful for building color-picker UIs that
49 * stay in sync with the framework as new colors are added.
50 */
51inline constexpr std::array<NamedColor, 15> Palette = {{
52 {"default", Default},
53 {"darkred", DarkRed},
54 {"lightpurple", LightPurple},
55 {"green", Green},
56 {"olive", Olive},
57 {"lime", Lime},
58 {"red", Red},
59 {"gray", Gray},
60 {"yellow", Yellow},
61 {"silver", Silver},
62 {"lightblue", LightBlue},
63 {"darkblue", DarkBlue},
64 {"purple", Purple},
65 {"lightred", LightRed},
66 {"gold", Gold},
67}};
68
69/**
70 * Map a human color name (case-insensitive) to its escape sequence.
71 * Returns `Default` for unknown names. Empty input also returns `Default`.
72 */
73std::string_view ParseNamed(std::string_view name);
74
75/** Strip all CS2 color escape bytes (0x01..0x10) from `text` for safe console/log output. */
76std::string Strip(std::string_view text);
77
78/**
79 * @ref Palette as labeled canonical names, so a colour picker grows as the palette does
80 * rather than repeating its contents.
81 *
82 * @p labelFor supplies the localized label for each canonical name; returning "" falls back to
83 * the name itself. Shaped for a `ChoiceRow<std::string>`, but it names no menu type - a caller
84 * that wants the list for anything else gets the same answer.
85 */
86std::vector<Labeled<std::string>> PaletteChoices(std::function<std::string(std::string_view canonicalName)> labelFor);
87
88} // namespace VoltMod::ChatColors
constexpr std::string_view Orange
constexpr std::string_view Blue
constexpr std::string_view DarkRed
std::string_view ParseNamed(std::string_view name)
constexpr std::string_view Magenta
constexpr std::string_view Olive
constexpr std::string_view Gray
std::vector< Labeled< std::string > > PaletteChoices(std::function< std::string(std::string_view canonicalName)> labelFor)
constexpr std::string_view Silver
constexpr std::string_view White
constexpr std::string_view Default
constexpr std::string_view Yellow
constexpr std::string_view LightYellow
constexpr std::string_view Green
constexpr std::string_view LightRed
constexpr std::string_view DarkBlue
constexpr std::string_view Red
constexpr std::string_view Gold
constexpr std::array< NamedColor, 15 > Palette
constexpr std::string_view LightBlue
constexpr std::string_view Grey
std::string Strip(std::string_view text)
constexpr std::string_view Lime
constexpr std::string_view LightPurple
constexpr std::string_view BlueGrey
constexpr std::string_view Purple
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56