opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <format>
6#include <optional>
7#include <string>
8#include <string_view>
9
10namespace osc
11{
13
14 // returns the linearized version of a sRGB component value
16
17 // returns the sRGB version of a linearized component value
19
20 // returns the linear version of a (presumed to be) sRGB color
22
23 // returns a color that is the (presumed to be) linear color with the sRGB gamma curve applied
25
26 // returns the color as a hexadecimal string in the format "#rrggbbaa", as
27 // commonly-used in web applications, configuration files, etc.
28 //
29 // - HDR values are clamped to LDR (they can't fit in this format)
30 // - examples:
31 // - red --> "#ff0000ff"
32 // - green --> "#00ff00ff"
33 // - blue --> "#0000ffff"
34 // - black --> "#000000ff"
35 // - clear --> "#00000000"
36 // - etc.
37 std::string to_html_string_rgba(const Color&);
38 std::optional<Color> try_parse_html_color_string(std::string_view);
39
40 // returns a color that is the result of converting `color` to HSLA multiplying
41 // its luminance (L) by `factor`, and converting it back to RGBA
42 Color multiply_luminance(const Color& color, float factor);
43}
44
45template<>
46struct std::formatter<osc::Color> final {
47 template<class ParseCtx>
48 constexpr auto parse(ParseCtx& ctx) { return inner_.parse(ctx); }
49
50 template<class FmtCtx>
51 auto format(const osc::Color color, FmtCtx& ctx) const
52 {
53 auto it = std::ranges::copy(std::string_view{"Color("}, ctx.out()).out;
54 std::string_view delimiter;
55 for (const auto& component : color) {
56 it = std::ranges::copy(delimiter, it).out;
57 ctx.advance_to(it);
58 it = inner_.format(component, ctx);
59 delimiter = ", ";
60 }
61 *it++ = ')';
62 return it;
63 }
64private:
65 std::formatter<osc::Color::value_type> inner_;
66};
Definition custom_decoration_generator.h:5
Color multiply_luminance(const Color &color, float factor)
float to_linear_colorspace(float srgb_component_value)
constexpr U to(T &&value)
Definition conversion.h:56
std::optional< Color > try_parse_html_color_string(std::string_view)
float to_srgb_colorspace(float linear_component_value)
std::string to_html_string_rgba(const Color &)
constexpr auto parse(ParseCtx &ctx)
Definition color.h:48
auto format(const osc::Color color, FmtCtx &ctx) const
Definition color.h:51