xtd 0.2.0
rgb.hpp
Go to the documentation of this file.
1
4#pragma once
5#include <xtd/byte>
6#include <xtd/math>
7
9namespace xtd {
11 namespace drawing {
13 namespace helpers {
23 struct rgb {
25
37
39
43 float get_hue() const noexcept {
44 if (r == g && g == b) return 0.0;
45
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;
49
50 auto max = static_cast<float>(xtd::math::max(xtd::math::max(rc, gc), bc));
51 auto min = static_cast<float>(xtd::math::min(xtd::math::min(rc, gc), bc));
52
53 auto delta = max - min;
54
55 auto hue = 0.0f;
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;
59 hue *= 60;
60
61 if (hue < 0.0) hue += 360.0;
62 return hue;
63 }
64
67 float get_lightness() const noexcept {
68 return (static_cast<float>(xtd::math::max(xtd::math::max(r, g), b)) + static_cast<float>(xtd::math::min(xtd::math::min(r, g), b))) / 2.0f / 255.0f;
69 }
70
73 float get_saturation() const noexcept {
74 auto max = static_cast<float>(xtd::math::max(xtd::math::max(r, g), b)) / 255.0f;
75 auto min = static_cast<float>(xtd::math::min(xtd::math::min(r, g), b)) / 255.0f;
76
77 if (max == min) return 0.0f;
78
79 return (max + min) <= 1.0f ? (max - min) / (max + min) : (max - min) / (2 - max - min);
80 }
81
82 std::tuple<float, float, float> to_hsl() const noexcept {
83 return {get_hue(), get_saturation(), get_lightness()};
84 }
86
88
101 static rgb alpha_blend(const rgb& fore_componant, const rgb& back_componant, double alpha) noexcept {
102 alpha = xtd::math::clamp(alpha, 0.0, 1.0);
103 return rgb {
104 .r = alpha_blend(fore_componant.r, back_componant.r, alpha),
105 .g = alpha_blend(fore_componant.g, back_componant.g, alpha),
106 .b = alpha_blend(fore_componant.b, back_componant.b, alpha),
107 };
108 }
109
121 static xtd::byte alpha_blend(xtd::byte fore_componant, xtd::byte back_componant, double alpha) noexcept {
122 return static_cast<xtd::byte>(fore_componant * (1 - alpha) + back_componant * alpha);
123 }
124
130 static rgb from_hsl(float hue, float saturation, float lightness) noexcept {
131 // algorithm version (see https://www.programmingalgorithms.com/algorithm/hsl-to-rgb)
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)};
133
134 auto hue_to_rgb = [](float v1, float v2, float vh)->float {
135 if (vh < 0) vh += 1;
136 if (vh > 1) vh -= 1;
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);
140 return v1;
141 };
142
143 hue = hue / 360.0f;
144 auto v2 = (lightness < 0.5f) ? (lightness * (1 + saturation)) : ((lightness + saturation) - (lightness * saturation));
145 auto v1 = 2 * lightness - v2;
146
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)};
148 }
150 };
151 }
152 }
153}
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