diff options
author | George L. Albany <Megacake1234@gmail.com> | 2023-12-24 23:00:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-24 23:00:36 +0100 |
commit | bf4e7368600bb425b6612231fbb84de34ec99a27 (patch) | |
tree | d3b29714d3286b9edbe380f877bbce541344a635 /src/openvic-simulation/dataloader | |
parent | f036506b88af02508242f279ca29b743ef713976 (diff) | |
parent | 3770de7a03879a8ff6b8cf22b402217c19fa2b53 (diff) |
Merge pull request #100 from OpenVicProject/change/colour_t_to_struct
Diffstat (limited to 'src/openvic-simulation/dataloader')
-rw-r--r-- | src/openvic-simulation/dataloader/NodeTools.cpp | 22 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/NodeTools.hpp | 4 |
2 files changed, 16 insertions, 10 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) { diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index 02ff239..c3eaf65 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -106,8 +106,10 @@ namespace OpenVic { callback_t<std::string_view> expect_fixed_point_str(callback_t<fixed_point_t> callback); node_callback_t expect_fixed_point(callback_t<fixed_point_t> callback); + /* Expect a list of 3 base 10 values, each either in the range [0, 1] or (1, 255], representing RGB components. */ node_callback_t expect_colour(callback_t<colour_t> callback); - node_callback_t expect_colour_hex(callback_t<colour_t> callback); + /* Expect a hexadecimal value representing a colour in ARGB format. */ + node_callback_t expect_colour_hex(callback_t<colour_argb_t> callback); callback_t<std::string_view> expect_date_str(callback_t<Date> callback); node_callback_t expect_date(callback_t<Date> callback); |