Skip to content

Commit 7649180

Browse files
committed
Added HSV to RGB color, fixed HSV to Linear color, other fixes & added imgui type conversions
1 parent 281fa2d commit 7649180

3 files changed

Lines changed: 134 additions & 16 deletions

File tree

Include/Misc/PipeImGui.h

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,42 @@ static_assert(false, "Imgui v" IMGUI_VERSION " found but PipeImGui requires v1.9
1717
#include <imgui.h>
1818

1919

20+
namespace p
21+
{
22+
///////////////////////////////////////////////////////////
23+
// Conversions
24+
25+
inline ImVec2 ToIM(v2 value)
26+
{
27+
return {value.x, value.y};
28+
}
29+
inline v2 FromIM(ImVec2 value)
30+
{
31+
return {value.x, value.y};
32+
}
33+
inline ImVec4 ToIM(const v4& value)
34+
{
35+
return {value.x, value.y, value.z, value.w};
36+
}
37+
inline v4 FromIMVector(const ImVec4& value)
38+
{
39+
return {value.x, value.y, value.z, value.w};
40+
}
41+
inline ImVec4 ToIM(const LinearColor& value)
42+
{
43+
return {value.r, value.g, value.b, value.a};
44+
}
45+
inline LinearColor FromIM(const ImVec4& value)
46+
{
47+
return {value.x, value.y, value.z, value.w};
48+
}
49+
};
50+
51+
2052
namespace ImGui
2153
{
2254
///////////////////////////////////////////////////////////
23-
// Definition
55+
// Internals
2456

2557
namespace details
2658
{
@@ -56,7 +88,7 @@ namespace ImGui
5688

5789

5890
///////////////////////////////////////////////////////////
59-
// Definition
91+
// Definitions
6092

6193
inline void PushID(p::StringView id)
6294
{

Include/PipeColor.h

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,22 @@ namespace p
206206
}
207207
else if constexpr (to == ColorMode::Linear && from == ColorMode::HSV)
208208
{
209-
const float hDiv60 = this->h / 60.0f;
209+
float hue = Mod(this->h, 360.f);
210+
if ( hue < 0.f ) hue += 360.f;
211+
const float saturation = p::Clamp(this->s,0.f,1.f);
212+
const float value = this->v;
213+
214+
const float hDiv60 = hue / 60.0f;
210215
const float hDiv60Floor = Floor(hDiv60);
211216
const float hDiv60Fraction = hDiv60 - hDiv60Floor;
212-
213217
const u32 swizzleIndex = u32(hDiv60Floor) % 6;
214218

219+
const float rgbValues[4] = {
220+
value,
221+
value * (1.0f - saturation),
222+
value * (1.0f - (hDiv60Fraction * saturation)),
223+
value * (1.0f - ((1.0f - hDiv60Fraction) * saturation)),
224+
};
215225
constexpr u32 rgbSwizzle[6][3] = {
216226
{0, 3, 1},
217227
{2, 0, 1},
@@ -220,20 +230,20 @@ namespace p
220230
{3, 1, 0},
221231
{0, 1, 2}
222232
};
223-
const float rgbValues[4] = {
224-
this->v,
225-
this->v * (1.0f - this->h),
226-
this->v * (1.0f - (hDiv60Fraction * this->h)),
227-
this->v * (1.0f - ((1.0f - hDiv60Fraction) * this->h)),
228-
};
229-
return {rgbValues[rgbSwizzle[swizzleIndex][0]],
230-
rgbValues[rgbSwizzle[swizzleIndex][1]], rgbValues[rgbSwizzle[swizzleIndex][2]],
233+
return {
234+
rgbValues[rgbSwizzle[swizzleIndex][0]],
235+
rgbValues[rgbSwizzle[swizzleIndex][1]],
236+
rgbValues[rgbSwizzle[swizzleIndex][2]],
231237
this->a};
232238
}
233239
else if constexpr (to == ColorMode::RGBA && from == ColorMode::HSV)
234240
{
235241
return Convert<ColorMode::Linear>().template Convert<to>();
236242
}
243+
else if constexpr (to == ColorMode::HSV && from == ColorMode::RGBA)
244+
{
245+
return Convert<ColorMode::Linear>().template Convert<to>();
246+
}
237247
else
238248
{
239249
P_CheckMsg(false, "Not supported color conversion");
@@ -409,7 +419,10 @@ namespace p
409419
P_CheckMsg(false, "operator*(scalar) is not allowed on HSV");
410420
return {};
411421
}
412-
return {this->r * scalar, this->g * scalar, this->b * scalar, this->a * scalar};
422+
else
423+
{
424+
return {this->r * scalar, this->g * scalar, this->b * scalar, this->a * scalar};
425+
}
413426
}
414427
constexpr TColor& operator*=(float scalar)
415428
{

Include/PipeMath.h

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,68 @@ namespace p
3636
return a <= b ? a : b;
3737
}
3838

39+
template<typename Type>
40+
static constexpr Type* Max(Type* values, u32 count)
41+
{
42+
if (!values || count <= 0)
43+
{
44+
return nullptr;
45+
}
46+
Type* max = values;
47+
for (u32 i = 1; i < count; ++i)
48+
{
49+
Type* v = values + i;
50+
if (*v > *max)
51+
{
52+
max = v;
53+
}
54+
}
55+
return max;
56+
}
57+
58+
template<typename Type>
59+
static constexpr Type* Min(Type* values, u32 count)
60+
{
61+
if (!values || count <= 0)
62+
{
63+
return nullptr;
64+
}
65+
Type* min = values;
66+
for (u32 i = 1; i < count; ++i)
67+
{
68+
Type* v = values + i;
69+
if (*v < *min)
70+
{
71+
min = v;
72+
}
73+
}
74+
return min;
75+
}
76+
77+
template<typename Type>
78+
static constexpr std::pair<Type*, Type*> MinMax(Type* values, u32 count)
79+
{
80+
if (!values || count <= 0)
81+
{
82+
return {nullptr, nullptr};
83+
}
84+
Type* min = values;
85+
Type* max = values;
86+
for (u32 i = 1; i < count; ++i)
87+
{
88+
Type* v = values + i;
89+
if (*v < *min)
90+
{
91+
min = v;
92+
}
93+
if (*v > *max)
94+
{
95+
max = v;
96+
}
97+
}
98+
return {min, max};
99+
}
100+
39101
template<typename Type>
40102
static constexpr Type Clamp(Type a, Type min, Type max)
41103
{
@@ -372,10 +434,21 @@ namespace p
372434
}
373435

374436
template<FloatingPoint Type>
375-
static constexpr float Mod(Type a, Type b)
437+
static inline constexpr float Mod(Type a, Type b)
376438
{
377-
return a - b * Floor(a / b);
378-
}
439+
if constexpr(IsSame<Type, float>)
440+
{
441+
return fmodf(a, b);
442+
}
443+
else if constexpr(IsSame<Type, double>)
444+
{
445+
return fmod(a, b);
446+
}
447+
else
448+
{
449+
return a - b * Floor(a / b);
450+
}
451+
}
379452

380453
template<SignedIntegral Type>
381454
static constexpr Type Mod(Type a, Type b)

0 commit comments

Comments
 (0)