diff options
author | Spartan322 <Megacake1234@gmail.com> | 2023-12-19 03:41:57 +0100 |
---|---|---|
committer | Spartan322 <Megacake1234@gmail.com> | 2023-12-24 22:57:56 +0100 |
commit | 3770de7a03879a8ff6b8cf22b402217c19fa2b53 (patch) | |
tree | 0d77d82ab8cea8955e2b86d883d1c2fd10813717 /src/openvic-simulation/dataloader/NodeTools.cpp | |
parent | 14e47d58b85f657ec1fed8abf88219f09bd3efbb (diff) |
Change colour_t to be a strongly typed structure
Make RGB default of `colour_t`
Distinguish RGB and ARGB colors by type and colour_traits
Add `_colour` and `_argb` colour user-defined literals
Add `OpenVic::utility::unreachable`
Diffstat (limited to 'src/openvic-simulation/dataloader/NodeTools.cpp')
-rw-r--r-- | src/openvic-simulation/dataloader/NodeTools.cpp | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp index c4addb7..e68a185 100644 --- a/src/openvic-simulation/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -1,5 +1,7 @@ #include "NodeTools.hpp" +#include "openvic-simulation/types/Colour.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -118,31 +120,33 @@ node_callback_t NodeTools::expect_fixed_point(callback_t<fixed_point_t> callback node_callback_t NodeTools::expect_colour(callback_t<colour_t> callback) { return [callback](ast::NodeCPtr node) -> bool { - colour_t col = NULL_COLOUR; - uint32_t components = 0; + colour_t col = colour_t::null(); + int32_t components = 0; bool ret = expect_list_of_length(3, expect_fixed_point( [&col, &components](fixed_point_t val) -> bool { - components++; - col <<= 8; if (val < 0 || val > 255) { - Logger::error("Invalid colour component: ", val); + Logger::error("Invalid colour component #", components++, ": ", val); return false; } else { if (val <= 1) { val *= 255; + } else if (!val.is_integer()) { + Logger::warning("Fractional part of colour component #", components, " will be truncated: ", val); } - col |= val.to_int32_t(); + col[components++] = val.to_int64_t(); return true; } } ))(node); - ret &= callback(col << 8 * (3 - components)); + ret &= callback(col); return ret; }; } -node_callback_t NodeTools::expect_colour_hex(callback_t<colour_t> callback) { - return expect_uint(callback, 16); +node_callback_t NodeTools::expect_colour_hex(callback_t<colour_argb_t> callback) { + return expect_uint<colour_argb_t::integer_type>([callback](colour_argb_t::integer_type val) -> bool { + return callback(colour_argb_t::from_integer(val)); + }, 16); } callback_t<std::string_view> NodeTools::expect_date_str(callback_t<Date> callback) { |