44 if (
r ==
g &&
g ==
b)
return 0.0;
46 auto rc =
static_cast<float>(
r) / 255.0f;
47 auto gc =
static_cast<float>(
g) / 255.0f;
48 auto bc =
static_cast<float>(
b) / 255.0f;
53 auto delta = max - min;
56 if (rc == max) hue = (gc - bc) / delta;
57 else if (gc == max) hue = 2 + (bc - rc) / delta;
58 else if (bc == max) hue = 4 + (rc - gc) / delta;
61 if (hue < 0.0) hue += 360.0;
77 if (max == min)
return 0.0f;
79 return (max + min) <= 1.0f ? (max - min) / (max + min) : (max - min) / (2 - max - min);
82 std::tuple<float, float, float> to_hsl() const noexcept {
130 static rgb from_hsl(
float hue,
float saturation,
float lightness)
noexcept {
132 if (saturation == 0)
return {
static_cast<xtd::byte>(lightness * 255.0f),
static_cast<xtd::byte>(lightness * 255.0f),
static_cast<xtd::byte>(lightness * 255.0f)};
134 auto hue_to_rgb = [](
float v1,
float v2,
float vh)->
float {
137 if ((6 * vh) < 1)
return (v1 + (v2 - v1) * 6 * vh);
138 if ((2 * vh) < 1)
return v2;
139 if ((3 * vh) < 2)
return (v1 + (v2 - v1) * ((2.0f / 3) - vh) * 6);
144 auto v2 = (lightness < 0.5f) ? (lightness * (1 + saturation)) : ((lightness + saturation) - (lightness * saturation));
145 auto v1 = 2 * lightness - v2;
147 return {
static_cast<xtd::byte>(hue_to_rgb(v1, v2, hue + (1.0f / 3)) * 255.0f),
static_cast<xtd::byte>(hue_to_rgb(v1, v2, hue) * 255.0f),
static_cast<xtd::byte>(hue_to_rgb(v1, v2, hue - (1.0f / 3)) * 255.0f)};
static xtd::byte min(xtd::byte a, xtd::byte b) noexcept
Returns the smaller of two 8-bit unsigned integers.
static xtd::byte clamp(xtd::byte value, xtd::byte min, xtd::byte max) noexcept
Returns value clamped to the inclusive range of min and max.
static xtd::byte max(xtd::byte a, xtd::byte b) noexcept
Returns the larger of two 8-bit unsigned integers.
uint8_t byte
Represents a 8-bit unsigned integer.
Definition byte.hpp:23
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
The alpha struct.
Definition alpha.hpp:22
The rgb struct.
Definition rgb.hpp:23
float get_saturation() const noexcept
Gets the hue-saturation-lightness (HSL) saturation value for this xtd::drawing::helpers::rgb structur...
Definition rgb.hpp:73
static rgb from_hsl(float hue, float saturation, float lightness) noexcept
Creates a xtd::drawing::helpers::rgb strucg from the three HSL component (hue, saturation,...
Definition rgb.hpp:130
xtd::byte b
Gets or sets the blue component value of this xtd::drawing::helpers::rbg struct.
Definition rgb.hpp:35
float get_hue() const noexcept
Gets the hue-saturation-lightness (HSL) hue value, in degrees, for this xtd::drawing::helpers::rgb st...
Definition rgb.hpp:43
static xtd::byte alpha_blend(xtd::byte fore_componant, xtd::byte back_componant, double alpha) noexcept
Returns the weighted average color component between the two given color components.
Definition rgb.hpp:121
float get_lightness() const noexcept
Gets the hue-saturation-lightness (HSL) lightness value for this xtd::drawing::helpers::rgb structure...
Definition rgb.hpp:67
xtd::byte r
Gets or sets the red component value of this xtd::drawing::helpers::rgb struct.
Definition rgb.hpp:29
static rgb alpha_blend(const rgb &fore_componant, const rgb &back_componant, double alpha) noexcept
Returns the weighted average color between the two given colors.
Definition rgb.hpp:101
xtd::byte g
Gets or sets the green component value of this xtd::drawing::helpers::rbg struct.
Definition rgb.hpp:32