35 newColor.
r = (newColor.
r < 0) ? 0 : (newColor.
r > 1) ? 1 : newColor.
r;
36 newColor.
g = (newColor.
g < 0) ? 0 : (newColor.
g > 1) ? 1 : newColor.
g;
37 newColor.
b = (newColor.
b < 0) ? 0 : (newColor.
b > 1) ? 1 : newColor.
b;
38 newColor.
a = (newColor.
a < 0) ? 0 : (newColor.
a > 1) ? 1 : newColor.
a;
39 auto pack = [](
float value) ->
byte
41 int iv =
static_cast<int>(std::lround(value * 255.0f));
42 iv = std::clamp(iv, 0, 255);
43 return static_cast<byte>(iv);
46 .r = pack(newColor.
r),
47 .g = pack(newColor.
g),
48 .b = pack(newColor.
b),
57 if (newOffset < 0.0f) newOffset = 0.0f;
58 if (newOffset >= 1.0f) newOffset = 1.0f;
59 offset =
static_cast<uint16_t
>(newOffset * 65535.0f);
64 float GetOffset()
const {
return (offset / 65535.0f); }
96 std::array<ColorStop, kMaxStops>
stops{};
106 return float(
value) / 65534.0f;
201 double turn = std::modf(
double(angleDegrees) / 360.0, &whole);
202 if (turn < 0.0) turn += 1.0;
203 return static_cast<uint16_t
>(turn * 65534.0);
267 for (uint8_t i = 0; i <
StopCount(); ++i)
268 if (
stops[i] != other.stops[i])
@ None
Definition gui_element_flags.h:18
StopInterpolation
How a GradientF blends from a stop to the next one.
Definition gui_style_gradient.h:277
GradientType
Kind of Gradient.
Definition gui_style_gradient.h:73
ColorSpace
The space gradient stops are interpolated in.
Definition gui_style_gradient.h:81
@ Linear
a straight blend
Definition gui_style_gradient.h:278
@ Constant
the stop's color all the way to the next stop: a step
Definition gui_style_gradient.h:280
@ Smooth
eases out of the stop and into the next (smoothstep)
Definition gui_style_gradient.h:279
@ Linear
Definition gui_style_gradient.h:75
@ None
Definition gui_style_gradient.h:74
@ Radial
Definition gui_style_gradient.h:76
@ HSV
Definition gui_style_gradient.h:85
@ HSL
Definition gui_style_gradient.h:84
@ Oklab
perceptual
Definition gui_style_gradient.h:87
@ sRGB
the default, as CSS
Definition gui_style_gradient.h:82
@ LinearRGB
Definition gui_style_gradient.h:83
@ Lab
CIE Lab, perceptual.
Definition gui_style_gradient.h:86
Definition gui_logging.h:24
Color stops for gradients.
Definition gui_style_gradient.h:22
bool operator==(const ColorStop &other) const noexcept=default
float GetOffset() const
Position along the gradient, 0..1.
Definition gui_style_gradient.h:64
ColorStop & SetColor(color4b newColor)
Definition gui_style_gradient.h:30
color4f GetColor4f() const
The color as 0..1 floats.
Definition gui_style_gradient.h:66
color4b GetColor4b() const
Definition gui_style_gradient.h:67
ColorStop & SetOffset(float newOffset)
Position along the gradient, 0..1 (clamped); stored as 16 bits.
Definition gui_style_gradient.h:55
ColorStop & SetColor(color4f newColor)
Clamps each channel to 0..1 and stores it as 8 bits.
Definition gui_style_gradient.h:33
A gradient of float colors, for data such as a material editor's gradient map: up to kMaxStops stops,...
Definition gui_style_gradient.h:303
int StopCount() const
The stops in use: at most kMaxStops.
Definition gui_style_gradient.h:312
static GradientF From(const Gradient &gradient)
The same stops as gradient, blending linearly in its color space.
bool AddStop(const color4f &color, float offset, StopInterpolation interpolation=StopInterpolation::Linear)
Adds a stop at offset (clamped to 0..1); false when kMaxStops are in use.
void Assign(const Gradient &gradient)
As From, into this one's storage.
std::vector< GradientStop > stops
in any order
Definition gui_style_gradient.h:306
color4f Sample(float t) const
The color at t (0..1, clamped), unclamped: HDR stays.
void WriteTo(std::span< color4b > out) const
As above, clamped to 0..1 and stored as 8 bits, for display.
Gradient ToGradient() const
The first Gradient::kMaxStops stops as a linear Gradient: colors clamped to 0..1 and stored as 8 bits...
ColorSpace colorSpace
space the stops are blended in
Definition gui_style_gradient.h:307
static constexpr int kMaxStops
stops past this are left out
Definition gui_style_gradient.h:304
void WriteTo(std::span< color4f > out) const
Samples the gradient evenly across out, unclamped.
bool operator==(const GradientF &) const =default
A stop of a GradientF.
Definition gui_style_gradient.h:285
color4f color
alpha not premultiplied; red, green and blue may pass 1 (HDR)
Definition gui_style_gradient.h:287
float offset
along the gradient, 0..1
Definition gui_style_gradient.h:286
bool operator==(const GradientStop &) const =default
StopInterpolation interpolation
from this stop to the next
Definition gui_style_gradient.h:288
Up to kMaxStops color stops plus an angle and interpolation color space.
Definition gui_style_gradient.h:93
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:253
static Gradient Linear(const color4f &c0, const color4f &c1)
Two stops at 0 and 1.
Definition gui_style_gradient.h:130
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:207
std::array< ColorStop, kMaxStops > stops
the first stopCount are in use
Definition gui_style_gradient.h:96
static constexpr int kMaxStops
capacity of stops
Definition gui_style_gradient.h:94
bool IsEmpty() const
No stops, or type None: nothing to draw.
Definition gui_style_gradient.h:256
ColorSpace colorSpace
space the stops are interpolated in
Definition gui_style_gradient.h:100
bool operator==(const Gradient &other) const noexcept
Definition gui_style_gradient.h:261
float GetValue() const
value as a fraction of a full turn (see EncodeAngle).
Definition gui_style_gradient.h:103
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:231
uint8_t stopCount
stops in use
Definition gui_style_gradient.h:98
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:198
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:240
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:184
uint16_t value
the angle, encoded by EncodeAngle
Definition gui_style_gradient.h:97
void ClearStops()
Removes all stops.
Definition gui_style_gradient.h:249
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:161
static Gradient Solid(const color4f &color)
One color: two equal stops at 0 and 1.
Definition gui_style_gradient.h:110
GradientType type
Definition gui_style_gradient.h:99
Gradient & WithColorSpace(ColorSpace cs)
Sets the space the stops are interpolated in; returns *this.
Definition gui_style_gradient.h:226
static Gradient Solid(const color4b &color)
One color: two equal stops at 0 and 1.
Definition gui_style_gradient.h:120
static Gradient Radial(float angleDegrees=1.0f)
An empty radial gradient; add stops with AddStop.
Definition gui_style_gradient.h:217
static Gradient Linear(const color4b &c0, const color4b &c1)
Two stops at 0 and 1.
Definition gui_style_gradient.h:140
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:228
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:150
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:172
Definition gui_base_types.h:383
Definition gui_base_types.h:394
float b
Definition gui_base_types.h:395
float r
Definition gui_base_types.h:395
float a
Definition gui_base_types.h:395
float g
Definition gui_base_types.h:395