33 newColor.
r = (newColor.
r < 0) ? 0 : (newColor.
r > 1) ? 1 : newColor.
r;
34 newColor.
g = (newColor.
g < 0) ? 0 : (newColor.
g > 1) ? 1 : newColor.
g;
35 newColor.
b = (newColor.
b < 0) ? 0 : (newColor.
b > 1) ? 1 : newColor.
b;
36 newColor.
a = (newColor.
a < 0) ? 0 : (newColor.
a > 1) ? 1 : newColor.
a;
37 auto pack = [](
float value) ->
byte
39 int iv =
static_cast<int>(std::lround(value * 255.0f));
40 iv = std::clamp(iv, 0, 255);
41 return static_cast<byte>(iv);
44 .r = pack(newColor.
r),
45 .g = pack(newColor.
g),
46 .b = pack(newColor.
b),
55 if (newOffset < 0.0f) newOffset = 0.0f;
56 if (newOffset >= 1.0f) newOffset = 1.0f;
57 offset =
static_cast<uint16_t
>(newOffset * 65535.0f);
62 float GetOffset()
const {
return (offset / 65535.0f); }
94 std::array<ColorStop, kMaxStops>
stops{};
104 return float(
value) / 65534.0f;
199 double turn = std::modf(
double(angleDegrees) / 360.0, &whole);
200 if (turn < 0.0) turn += 1.0;
201 return static_cast<uint16_t
>(turn * 65534.0);
265 for (uint8_t i = 0; i <
StopCount(); ++i)
266 if (
stops[i] != other.stops[i])
@ None
Definition gui_element_flags.h:19
GradientType
Kind of Gradient.
Definition gui_style_gradient.h:71
ColorSpace
The space gradient stops are interpolated in.
Definition gui_style_gradient.h:79
@ Linear
Definition gui_style_gradient.h:73
@ None
Definition gui_style_gradient.h:72
@ Radial
Definition gui_style_gradient.h:74
@ HSV
Definition gui_style_gradient.h:83
@ HSL
Definition gui_style_gradient.h:82
@ Oklab
perceptual
Definition gui_style_gradient.h:85
@ sRGB
the default, as CSS
Definition gui_style_gradient.h:80
@ LinearRGB
Definition gui_style_gradient.h:81
@ Lab
CIE Lab, perceptual.
Definition gui_style_gradient.h:84
Definition gui_logging.h:24
Color stops for gradients.
Definition gui_style_gradient.h:20
bool operator==(const ColorStop &other) const noexcept=default
float GetOffset() const
Position along the gradient, 0..1.
Definition gui_style_gradient.h:62
ColorStop & SetColor(color4b newColor)
Definition gui_style_gradient.h:28
color4f GetColor4f() const
The color as 0..1 floats.
Definition gui_style_gradient.h:64
color4b GetColor4b() const
Definition gui_style_gradient.h:65
ColorStop & SetOffset(float newOffset)
Position along the gradient, 0..1 (clamped); stored as 16 bits.
Definition gui_style_gradient.h:53
ColorStop & SetColor(color4f newColor)
Clamps each channel to 0..1 and stores it as 8 bits.
Definition gui_style_gradient.h:31
Up to kMaxStops color stops plus an angle and interpolation color space.
Definition gui_style_gradient.h:91
uint8_t StopCount() const
The stops in use: stopCount, but never past the stop array (a gradient built in code can set stopCoun...
Definition gui_style_gradient.h:251
static Gradient Linear(const color4f &c0, const color4f &c1)
Two stops at 0 and 1.
Definition gui_style_gradient.h:128
static Gradient Linear(float angleDegrees=0.0f)
An empty linear gradient at an angle in degrees; add stops with AddStop.
Definition gui_style_gradient.h:205
std::array< ColorStop, kMaxStops > stops
the first stopCount are in use
Definition gui_style_gradient.h:94
static constexpr int kMaxStops
capacity of stops
Definition gui_style_gradient.h:92
bool IsEmpty() const
No stops, or type None: nothing to draw.
Definition gui_style_gradient.h:254
ColorSpace colorSpace
space the stops are interpolated in
Definition gui_style_gradient.h:98
bool operator==(const Gradient &other) const noexcept
Definition gui_style_gradient.h:259
float GetValue() const
value as a fraction of a full turn (see EncodeAngle).
Definition gui_style_gradient.h:101
bool AddStop(const color4f &color, float offset)
Adds a stop at offset (0..1); false when all kMaxStops are in use.
Definition gui_style_gradient.h:229
uint8_t stopCount
stops in use
Definition gui_style_gradient.h:96
static uint16_t EncodeAngle(float angleDegrees)
Angle as a fraction of a full turn in [0, 1), wrapping negative angles (-90deg == 270deg).
Definition gui_style_gradient.h:196
bool AddStop(const color4b &color, float offset)
Adds a stop at offset (0..1); false when all kMaxStops are in use.
Definition gui_style_gradient.h:238
static Gradient Linear(const color4b &c0, const color4b &c1, const color4b &c2, const color4b &c3)
Four stops at 0, 0.25, 0.5 and 1.
Definition gui_style_gradient.h:182
uint16_t value
the angle, encoded by EncodeAngle
Definition gui_style_gradient.h:95
void ClearStops()
Removes all stops.
Definition gui_style_gradient.h:247
static Gradient Linear(const color4b &c0, const color4b &c1, const color4b &c2)
Three stops at 0, 0.5 and 1.
Definition gui_style_gradient.h:159
static Gradient Solid(const color4f &color)
One color: two equal stops at 0 and 1.
Definition gui_style_gradient.h:108
GradientType type
Definition gui_style_gradient.h:97
Gradient & WithColorSpace(ColorSpace cs)
Sets the space the stops are interpolated in; returns *this.
Definition gui_style_gradient.h:224
static Gradient Solid(const color4b &color)
One color: two equal stops at 0 and 1.
Definition gui_style_gradient.h:118
static Gradient Radial(float angleDegrees=1.0f)
An empty radial gradient; add stops with AddStop.
Definition gui_style_gradient.h:215
static Gradient Linear(const color4b &c0, const color4b &c1)
Two stops at 0 and 1.
Definition gui_style_gradient.h:138
void WriteTo(std::span< color4b > out) const
Samples the gradient evenly across out, sRGB encoded.
ColorSpace GetColorSpace() const
Space the stops are interpolated in.
Definition gui_style_gradient.h:226
static Gradient Linear(const color4f &c0, const color4f &c1, const color4f &c2)
Three stops at 0, 0.5 and 1.
Definition gui_style_gradient.h:148
static Gradient Linear(const color4f &c0, const color4f &c1, const color4f &c2, const color4f &c3)
Four stops at 0, 0.25, 0.5 and 1.
Definition gui_style_gradient.h:170
Definition gui_base_types.h:384
Definition gui_base_types.h:395
float b
Definition gui_base_types.h:396
float r
Definition gui_base_types.h:396
float a
Definition gui_base_types.h:396
float g
Definition gui_base_types.h:396