From 9bd19ed3728e57d2a03e02c30af4e14bbccf66b5 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 14 May 2024 21:30:28 -0400 Subject: [PATCH] color interpolation --- CMakeLists.txt | 2 +- include/blt/math/vectors.h | 42 +++++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 68ad50d..e96e06e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 0.17.7) +set(BLT_VERSION 0.17.8) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/math/vectors.h b/include/blt/math/vectors.h index 0219503..793e5d8 100644 --- a/include/blt/math/vectors.h +++ b/include/blt/math/vectors.h @@ -28,7 +28,8 @@ namespace blt } template - struct vec { + struct vec + { static_assert(std::is_arithmetic_v && "blt::vec must be created using an arithmetic type!"); private: std::array elements; @@ -48,8 +49,10 @@ namespace blt constexpr vec(U t, std::initializer_list args): elements() { auto b = args.begin(); - for (auto& v : elements) { - if (b == args.end()) { + for (auto& v : elements) + { + if (b == args.end()) + { v = t; continue; } @@ -430,6 +433,12 @@ namespace blt using color4 = vec4; using color3 = vec3; + inline color4 interpolate(const color4& in, const color4& desired, float factor) + { + auto diff = desired - in; + return in + (diff * factor); + } + inline color4 make_color(float r, float g, float b) { return color4{r, g, b, 1.0f}; @@ -438,9 +447,11 @@ namespace blt template inline blt::vec make_vec2(const blt::vec& t, size_t fill = 0) { - if constexpr (size >= 2) { + if constexpr (size >= 2) + { return blt::vec(t.x(), t.y()); - } else { + } else + { return blt::vec(t.x(), fill); } } @@ -448,9 +459,11 @@ namespace blt template inline blt::vec make_vec3(const blt::vec& t, size_t fill = 0) { - if constexpr (size >= 3) { + if constexpr (size >= 3) + { return blt::vec(t.x(), t.y(), t.z()); - } else { + } else + { blt::vec ret; for (size_t i = 0; i < size; i++) ret[i] = t[i]; @@ -463,9 +476,11 @@ namespace blt template inline blt::vec make_vec4(const blt::vec& t, size_t fill = 0) { - if constexpr (size >= 4) { + if constexpr (size >= 4) + { return blt::vec(t.x(), t.y(), t.z(), t.w()); - } else { + } else + { blt::vec ret; for (size_t i = 0; i < size; i++) ret[i] = t[i]; @@ -482,7 +497,8 @@ namespace blt v1 = v.normalize(); vec3 arbitraryVector{1, 0, 0}; - if (std::abs(vec3::dot(v, arbitraryVector)) > 0.9) { + if (std::abs(vec3::dot(v, arbitraryVector)) > 0.9) + { arbitraryVector = vec3{0, 1, 0}; } @@ -501,10 +517,12 @@ namespace blt basis[0] = basis[0].normalize(); // iterate over the rest of the vectors - for (int i = 1; i < n; ++i) { + for (int i = 1; i < n; ++i) + { // subtract the projections of the vector onto the previous basis vectors vec3 new_vector = vectors[i]; - for (int j = 0; j < i; ++j) { + for (int j = 0; j < i; ++j) + { float projection = vec3::dot(vectors[i], basis[j]); new_vector[0] -= projection * basis[j].x(); new_vector[1] -= projection * basis[j].y();