Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
GameDevWeek
Dependencies
Cpp
mirrage
Commits
b27cf8a3
Commit
b27cf8a3
authored
Apr 01, 2019
by
Florian Oetke
Browse files
added function and ud-literal to convert hex color codes to glm::vec4 and ImColor
parent
7e44c363
Pipeline
#3115
failed with stage
in 6 minutes and 16 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/mirrage/gui/include/mirrage/gui/gui.hpp
View file @
b27cf8a3
...
...
@@ -14,12 +14,38 @@
#include
<glm/vec2.hpp>
#include
<glm/vec4.hpp>
#define IM_VEC2_CLASS_EXTRA \
constexpr ImVec2(const glm::vec2& f) \
{ \
x = f.x; \
y = f.y; \
} \
constexpr operator glm::vec2() const { return glm::vec2(x, y); }
#define IM_VEC4_CLASS_EXTRA \
constexpr ImVec4(const glm::vec4& f) \
{ \
x = f.x; \
y = f.y; \
z = f.z; \
w = f.w; \
} \
constexpr operator glm::vec4() const { return glm::vec4(x, y, z, w); }
#include
<imgui.h>
#include
<cstdint>
#include
<memory>
namespace
mirrage
::
gui
::
literals
{
inline
ImColor
operator
""
_imcolor
(
const
char
*
str
,
std
::
size_t
len
)
{
auto
c
=
mirrage
::
util
::
hex_to_color
(
str
,
len
);
return
{
std
::
pow
(
c
.
r
,
2.2
f
),
std
::
pow
(
c
.
g
,
2.2
f
),
std
::
pow
(
c
.
b
,
2.2
f
),
c
.
a
};
}
}
// namespace mirrage::gui::literals
namespace
ImGui
{
inline
constexpr
float
SIZE_AUTO
=
0.0
f
;
...
...
src/mirrage/utils/include/mirrage/utils/units.hpp
View file @
b27cf8a3
...
...
@@ -7,9 +7,11 @@
#pragma once
#include
<mirrage/utils/log.hpp>
#include
<glm/glm.hpp>
#include
<glm/gtc/constants.hpp>
#include
<glm/gtx/rotate_vector.hpp>
#include
<glm/vec2.hpp>
#include
<cmath>
#include
<type_traits>
...
...
@@ -27,6 +29,68 @@ namespace mirrage::util {
using
Rgba
=
glm
::
vec4
;
using
Rgb
=
glm
::
vec3
;
// hex code to color conversion
namespace
detail
{
constexpr
auto
constexpr_strlen
(
const
char
*
str
)
{
auto
len
=
std
::
size_t
(
0
);
while
(
*
str
!=
'\0'
)
str
++
;
return
len
;
}
constexpr
auto
hex_char_to_byte
(
char
c
)
{
auto
b
=
std
::
int32_t
(
c
&
0b1111
);
return
c
>
'9'
?
b
+
0b1001
:
b
;
}
}
// namespace detail
inline
auto
hex_to_color
(
const
char
*
color
,
std
::
size_t
len
)
->
Rgba
{
// skip leading #
if
(
*
color
==
'#'
)
{
color
++
;
len
--
;
}
auto
result
=
glm
::
vec4
(
1
,
1
,
1
,
1
);
switch
(
len
)
{
case
3
:
case
4
:
for
(
auto
i
=
0
;
i
<
int
(
len
);
i
++
)
result
[
i
]
=
detail
::
hex_char_to_byte
(
color
[
i
])
/
255.
f
;
break
;
case
6
:
case
8
:
for
(
auto
i
=
0
;
i
<
int
(
len
)
/
2
;
i
++
)
result
[
i
]
=
(
detail
::
hex_char_to_byte
(
color
[
i
*
2
])
<<
4
|
detail
::
hex_char_to_byte
(
color
[
i
*
2
]))
/
255.
f
;
break
;
default:
MIRRAGE_FAIL
(
"Invalid input to hex_to_vec4: "
<<
color
);
}
return
result
;
}
[[
nodiscard
]]
inline
auto
hex_to_color
(
const
std
::
string
&
str
)
->
Rgba
{
return
hex_to_color
(
str
.
c_str
(),
str
.
length
());
}
[[
nodiscard
]]
inline
auto
hex_to_color
(
const
char
*
str
)
->
Rgba
{
return
hex_to_color
(
str
,
detail
::
constexpr_strlen
(
str
));
}
template
<
class
S
>
struct
Value_type
{
protected:
...
...
@@ -416,6 +480,11 @@ namespace mirrage::util {
constexpr
Force
operator
""
_kn
(
long
double
v
)
{
return
Force
(
static_cast
<
float
>
(
v
*
1000
));
}
constexpr
Force
operator
""
_kn
(
unsigned
long
long
v
)
{
return
Force
(
static_cast
<
float
>
(
v
*
1000
));
}
inline
glm
::
vec4
operator
""
_color
(
const
char
*
str
,
std
::
size_t
len
)
{
return
hex_to_color
(
str
,
len
);
}
constexpr
Time
second
=
1
_s
;
constexpr
Time_squared
second_2
=
1
_s
*
1
_s
;
constexpr
Time
minute
=
1
_min
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment