Commit b27cf8a3 authored by Florian Oetke's avatar Florian Oetke

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
......@@ -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.2f), std::pow(c.g, 2.2f), std::pow(c.b, 2.2f), c.a};
}
} // namespace mirrage::gui::literals
namespace ImGui {
inline constexpr float SIZE_AUTO = 0.0f;
......
......@@ -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;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment