Files
panopainter/engine/util.cpp

36 lines
1.2 KiB
C++

#include "pch.h"
#include "util.h"
bool point_in_rect(const glm::vec2& p, const glm::vec4& r)
{
return p.x > r.x && p.x < r.x+r.z && p.y > r.y && p.y < r.y+r.w;
}
glm::vec4 rand_color()
{
float r = (rand() % 256) / 256.f;
float g = (rand() % 256) / 256.f;
float b = (rand() % 256) / 256.f;
return { r, g, b, 1.f };
}
glm::vec3 convert_hsv2rgb(const glm::vec3 c)
{
glm::vec4 K = glm::vec4(1.0f, 2.0f / 3.0f, 1.0f / 3.0f, 3.0f);
glm::vec3 p = glm::abs(glm::fract(glm::vec3(c.xxx + K.xyz)) * 6.0f - K.www);
auto tmp = glm::clamp(glm::vec3(p - K.xxx), 0.0f, 1.0f);
return c.z * glm::mix(glm::vec3(K.xxx), tmp, c.y);
}
glm::vec3 convert_rgb2hsv(const glm::vec3 c)
{
glm::vec4 K = glm::vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
//glm::vec4 p = mix(glm::vec4(c.bg, K.wz), glm::vec4(c.gb, K.xy), glm::step(c.b, c.g));
//glm::vec4 q = mix(glm::vec4(p.xyw, c.r), glm::vec4(c.r, p.yzx), glm::step(p.x, c.r));
glm::vec4 p = c.g < c.b ? glm::vec4(c.bg, K.wz) : glm::vec4(c.gb, K.xy);
glm::vec4 q = c.r < p.x ? glm::vec4(p.xyw, c.r) : glm::vec4(c.r, p.yzx);
float d = q.x - glm::min(q.w, q.y);
float e = 1.0e-10f;
return glm::vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}