diff options
author | Hop311 <hop3114@gmail.com> | 2023-09-15 22:55:00 +0200 |
---|---|---|
committer | Hop311 <hop3114@gmail.com> | 2023-09-15 23:22:03 +0200 |
commit | 8add7c666455a8a4edd9e02ca5a33f78a25e8f2c (patch) | |
tree | 65b1f27aecea054bc708d2ee0d6eb4ae981b3fc3 /src/openvic-simulation/types | |
parent | 46bbbb038e5fa21e25fa33c4fee84e4b14690885 (diff) |
First go at Modifiers
Diffstat (limited to 'src/openvic-simulation/types')
-rw-r--r-- | src/openvic-simulation/types/Colour.hpp | 14 | ||||
-rw-r--r-- | src/openvic-simulation/types/IdentifierRegistry.cpp | 8 | ||||
-rw-r--r-- | src/openvic-simulation/types/IdentifierRegistry.hpp | 4 |
3 files changed, 16 insertions, 10 deletions
diff --git a/src/openvic-simulation/types/Colour.hpp b/src/openvic-simulation/types/Colour.hpp index 01f3852..15c574f 100644 --- a/src/openvic-simulation/types/Colour.hpp +++ b/src/openvic-simulation/types/Colour.hpp @@ -7,30 +7,36 @@ #include <string> namespace OpenVic { - // Represents a 24-bit RGB integer OR a 32-bit ARGB integer + /* Colour represented by an unsigned integer, either 24-bit RGB or 32-bit ARGB. */ using colour_t = uint32_t; + /* When colour_t is used as an identifier, NULL_COLOUR is disallowed * and should be reserved as an error value. * When colour_t is used in a purely graphical context, NULL_COLOUR * should be allowed. */ - static constexpr colour_t NULL_COLOUR = 0, FULL_COLOUR = 0xFF, MAX_COLOUR_RGB = 0xFFFFFF; + static constexpr colour_t NULL_COLOUR = 0, FULL_COLOUR = 0xFF, + MAX_COLOUR_RGB = 0xFFFFFF, MAX_COLOUR_ARGB = 0xFFFFFFFF; + constexpr colour_t float_to_colour_byte(float f, float min = 0.0f, float max = 1.0f) { return static_cast<colour_t>(std::clamp(min + f * (max - min), min, max) * 255.0f); } + constexpr colour_t fraction_to_colour_byte(int n, int d, float min = 0.0f, float max = 1.0f) { return float_to_colour_byte(static_cast<float>(n) / static_cast<float>(d), min, max); } + constexpr colour_t float_to_alpha_value(float a) { return float_to_colour_byte(a) << 24; } + constexpr float colour_byte_to_float(colour_t colour) { return std::clamp(static_cast<float>(colour) / 255.0f, 0.0f, 1.0f); } - inline std::string colour_to_hex_string(colour_t colour) { + inline std::string colour_to_hex_string(colour_t colour, bool alpha = false) { std::ostringstream stream; - stream << std::hex << std::setfill('0') << std::setw(6) << colour; + stream << std::uppercase << std::hex << std::setfill('0') << std::setw(!alpha ? 6 : 8) << colour; return stream.str(); } } diff --git a/src/openvic-simulation/types/IdentifierRegistry.cpp b/src/openvic-simulation/types/IdentifierRegistry.cpp index e33bc29..f284164 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.cpp +++ b/src/openvic-simulation/types/IdentifierRegistry.cpp @@ -21,8 +21,8 @@ std::ostream& OpenVic::operator<<(std::ostream& stream, HasIdentifier const* obj return obj != nullptr ? stream << *obj : stream << "<NULL>"; } -HasColour::HasColour(colour_t const new_colour, bool can_be_null) : colour(new_colour) { - assert((can_be_null || colour != NULL_COLOUR) && colour <= MAX_COLOUR_RGB); +HasColour::HasColour(colour_t const new_colour, bool can_be_null, bool can_have_alpha) : colour(new_colour) { + assert((can_be_null || colour != NULL_COLOUR) && colour <= (!can_have_alpha ? MAX_COLOUR_RGB : MAX_COLOUR_ARGB)); } colour_t HasColour::get_colour() const { return colour; } @@ -32,9 +32,9 @@ std::string HasColour::colour_to_hex_string() const { } HasIdentifierAndColour::HasIdentifierAndColour(const std::string_view new_identifier, - const colour_t new_colour, bool can_be_null) + const colour_t new_colour, bool can_be_null, bool can_have_alpha) : HasIdentifier { new_identifier }, - HasColour { new_colour, can_be_null } {} + HasColour { new_colour, can_be_null, can_have_alpha } {} distribution_t::value_type OpenVic::get_largest_item(distribution_t const& dist) { const distribution_t::const_iterator result = std::max_element(dist.begin(), dist.end(), diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index 20eebb9..3f73a2c 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -37,7 +37,7 @@ namespace OpenVic { const colour_t colour; protected: - HasColour(const colour_t new_colour, bool can_be_null); + HasColour(const colour_t new_colour, bool can_be_null, bool can_have_alpha); public: HasColour(HasColour const&) = delete; @@ -55,7 +55,7 @@ namespace OpenVic { */ class HasIdentifierAndColour : public HasIdentifier, public HasColour { protected: - HasIdentifierAndColour(const std::string_view new_identifier, const colour_t new_colour, bool can_be_null); + HasIdentifierAndColour(const std::string_view new_identifier, const colour_t new_colour, bool can_be_null, bool can_have_alpha); public: HasIdentifierAndColour(HasIdentifierAndColour const&) = delete; |