aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/types/Colour.hpp
blob: b1888f272b12de8410780204d869c4ae8194b525 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#pragma once

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cstdint>
#include <limits>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>

#include "openvic-simulation/utility/Utility.hpp"

namespace OpenVic {
   template<typename ValueT, typename IntT>
   struct colour_traits {
      using value_type = ValueT;
      using integer_type = IntT;
      static_assert(sizeof(value_type) * 4 <= sizeof(integer_type), "value_type must be 4x smaller then colour_integer_type");

      /* 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 integer_type null = 0;
      static constexpr integer_type component = std::numeric_limits<ValueT>::max();
      static constexpr integer_type component_bit_size = sizeof(ValueT) * CHAR_BIT;
      static constexpr integer_type blue_shift = 0;
      static constexpr integer_type green_shift = component_bit_size;
      static constexpr integer_type red_shift = component_bit_size * 2;
      static constexpr integer_type alpha_shift = component_bit_size * 3;
      static constexpr bool has_alpha = true;

      static constexpr integer_type make_rgb_integer(value_type red, value_type green, value_type blue) {
         return (red << red_shift) | (green << green_shift) | (blue << blue_shift);
      }
      static constexpr integer_type make_argb_integer(value_type red, value_type green, value_type blue, value_type alpha) {
         return make_rgb_integer(red, green, blue) | (alpha << alpha_shift);
      }

      static constexpr value_type red_from_integer(integer_type colour) {
         return (colour >> red_shift) & component;
      }
      static constexpr value_type blue_from_integer(integer_type colour) {
         return (colour >> blue_shift) & component;
      }
      static constexpr value_type green_from_integer(integer_type colour) {
         return (colour >> green_shift) & component;
      }
      static constexpr value_type alpha_from_integer(integer_type colour) {
         return (colour >> alpha_shift) & component;
      }

      static constexpr float component_to_float(value_type value) {
         return static_cast<float>(value) / static_cast<float>(component);
      }

      static constexpr value_type component_from_float(float f, float min = 0.0f, float max = 1.0f) {
         constexpr auto floor = [](float f) {
            const std::int64_t i = static_cast<std::int64_t>(f);
            return f < i ? i - 1 : i;
         };

         return floor(std::clamp(min + f * (max - min), min, max) * static_cast<float>(component));
      }

      static constexpr value_type component_from_fraction(int n, int d, float min = 0.0f, float max = 1.0f) {
         return component_from_float(static_cast<float>(n) / static_cast<float>(d), min, max);
      }

      static constexpr float red_to_float(value_type red) {
         return component_to_float(red);
      }
      static constexpr float green_to_float(value_type green) {
         return component_to_float(green);
      }
      static constexpr float blue_to_float(value_type blue) {
         return component_to_float(blue);
      }
      static constexpr float alpha_to_float(value_type alpha) {
         return component_to_float(alpha);
      }

      static constexpr value_type red_from_float(float red) {
         return component_from_float(red);
      }
      static constexpr value_type green_from_float(float green) {
         return component_from_float(green);
      }
      static constexpr value_type blue_from_float(float blue) {
         return component_from_float(blue);
      }
      static constexpr value_type alpha_from_float(float alpha) {
         return component_from_float(alpha);
      }

      static constexpr integer_type max_rgb = make_rgb_integer(component, component, component);
      static constexpr integer_type max_argb = make_argb_integer(component, component, component, component);
   };

   /* Colour represented by an unsigned integer, either 24-bit RGB or 32-bit ARGB. */
   template<typename ValueT, typename ColourIntT, typename ColourTraits = colour_traits<ValueT, ColourIntT>>
   struct basic_colour_t {
      /* PROPERTY generated getter functions will return colours by value, rather than const reference. */
      using ov_return_by_value = void;

      using colour_traits = ColourTraits;
      using value_type = typename colour_traits::value_type;
      using integer_type = typename colour_traits::integer_type;

      static_assert(std::same_as<ValueT, value_type> && std::same_as<ColourIntT, integer_type>);

      static constexpr auto max_value = colour_traits::component;

      struct empty_value {
         constexpr empty_value() {}
         constexpr empty_value(value_type) {}
         constexpr operator value_type() const {
            return max_value;
         }
      };

   private:
      using _alpha_t = std::conditional_t<colour_traits::has_alpha, value_type, empty_value>;

   public:
      value_type red;
      value_type green;
      value_type blue;
      [[no_unique_address]] _alpha_t alpha;

      static constexpr std::integral_constant<std::size_t, std::is_same_v<decltype(alpha), value_type> ? 4 : 3> size = {};

      static constexpr basic_colour_t fill_as(value_type value) {
         if constexpr (colour_traits::has_alpha) {
            return { value, value, value, value };
         } else {
            return { value, value, value };
         }
      }

      static constexpr basic_colour_t null() {
         return fill_as(colour_traits::null);
      }

      static constexpr basic_colour_t from_integer(integer_type integer) {
         if constexpr (colour_traits::has_alpha) {
            return {
               colour_traits::red_from_integer(integer), colour_traits::green_from_integer(integer),
               colour_traits::blue_from_integer(integer), colour_traits::alpha_from_integer(integer)
            };
         } else {
            assert(
               colour_traits::alpha_from_integer(integer) == colour_traits::null ||
               colour_traits::alpha_from_integer(integer) == max_value
            );
            return {
               colour_traits::red_from_integer(integer), colour_traits::green_from_integer(integer),
               colour_traits::blue_from_integer(integer)
            };
         }
      }

      static constexpr basic_colour_t
      from_floats(float r, float g, float b, float a = colour_traits::alpha_to_float(max_value))
      requires(colour_traits::has_alpha)
      {
         return {
            colour_traits::red_from_float(r), colour_traits::green_from_float(g), colour_traits::blue_from_float(b),
            colour_traits::alpha_from_float(a)
         };
      }

      static constexpr basic_colour_t from_floats(float r, float g, float b)
      requires(!colour_traits::has_alpha)
      {
         return { colour_traits::red_from_float(r), colour_traits::green_from_float(g), colour_traits::blue_from_float(b) };
      }

      constexpr integer_type as_rgb() const {
         return colour_traits::make_rgb_integer(red, green, blue);
      }

      constexpr integer_type as_argb() const {
         return colour_traits::make_argb_integer(red, green, blue, alpha);
      }

      constexpr basic_colour_t() : basic_colour_t { null() } {}

      constexpr basic_colour_t(value_type r, value_type g, value_type b, value_type a = max_value)
      requires(colour_traits::has_alpha)
         : red(r), green(g), blue(b), alpha(a) {}

      constexpr basic_colour_t(value_type r, value_type g, value_type b)
      requires(!colour_traits::has_alpha)
         : red(r), green(g), blue(b) {}

      template<typename _ColourTraits>
      requires(
         _ColourTraits::has_alpha && std::same_as<typename _ColourTraits::value_type, value_type> &&
         std::same_as<typename _ColourTraits::integer_type, integer_type>
      )
      explicit constexpr basic_colour_t(basic_colour_t<value_type, integer_type, _ColourTraits> const& colour)
      requires(colour_traits::has_alpha)
         : basic_colour_t { colour.red, colour.green, colour.blue, colour.alpha } {}

      template<typename _ColourTraits>
      requires(
         !_ColourTraits::has_alpha && std::same_as<typename _ColourTraits::value_type, value_type> &&
         std::same_as<typename _ColourTraits::integer_type, integer_type>
      )
      explicit constexpr basic_colour_t(
         basic_colour_t<value_type, integer_type, _ColourTraits> const& colour, value_type a = max_value
      )
      requires(colour_traits::has_alpha)
         : basic_colour_t { colour.red, colour.green, colour.blue, a } {}

      template<typename _ColourTraits>
      requires(
         std::same_as<typename _ColourTraits::value_type, value_type> &&
         std::same_as<typename _ColourTraits::integer_type, integer_type>
      )
      explicit constexpr basic_colour_t(basic_colour_t<value_type, integer_type, _ColourTraits> const& colour)
      requires(!colour_traits::has_alpha)
         : basic_colour_t { colour.red, colour.green, colour.blue } {}

      constexpr explicit operator integer_type() const {
         if constexpr (colour_traits::has_alpha) {
            return as_argb();
         } else {
            return as_rgb();
         }
      }

      constexpr basic_colour_t with_red(value_type r) const {
         if constexpr (colour_traits::has_alpha) {
            return { r, green, blue, alpha };
         } else {
            return { r, green, blue };
         }
      }

      constexpr basic_colour_t with_green(value_type g) const {
         if constexpr (colour_traits::has_alpha) {
            return { red, g, blue, alpha };
         } else {
            return { red, g, blue };
         }
      }

      constexpr basic_colour_t with_blue(value_type b) const {
         if constexpr (colour_traits::has_alpha) {
            return { red, green, b, alpha };
         } else {
            return { red, green, b };
         }
      }

      constexpr basic_colour_t with_alpha(value_type a) const
      requires(colour_traits::has_alpha)
      {
         return { red, green, blue, a };
      }

      constexpr float redf() const {
         return colour_traits::red_to_float(red);
      }

      constexpr float greenf() const {
         return colour_traits::green_to_float(green);
      }

      constexpr float bluef() const {
         return colour_traits::blue_to_float(blue);
      }

      constexpr float alphaf() const {
         return colour_traits::alpha_to_float(alpha);
      }

      inline std::string to_hex_string(bool alpha = false) const {
         using namespace std::string_view_literals;
         static constexpr std::string_view digits = "0123456789ABCDEF"sv;
         static constexpr std::size_t bits_per_digit = 4;
         static constexpr std::size_t digit_mask = (1 << bits_per_digit) - 1;
         static constexpr std::size_t argb_length = colour_traits::component_bit_size / bits_per_digit * 4;
         static constexpr std::size_t rgb_length = colour_traits::component_bit_size / bits_per_digit * 3;

         const std::size_t length = alpha ? argb_length : rgb_length;
         std::array<char, argb_length> address;
         const integer_type value = alpha ? as_argb() : as_rgb();

         for (std::size_t index = 0, j = (length - 1) * bits_per_digit; index < length; ++index, j -= bits_per_digit) {
            address[index] = digits[(value >> j) & digit_mask];
         }
         return { address.data(), length };
      }

      explicit operator std::string() const {
         return to_hex_string(colour_traits::has_alpha);
      }

      friend std::ostream& operator<<(std::ostream& stream, basic_colour_t const& colour) {
         return stream << colour.operator std::string();
      }

      constexpr bool is_null() const {
         return *this == null();
      }

      constexpr auto operator<=>(basic_colour_t const& rhs) const = default;
      constexpr auto operator<=>(integer_type rhs) const {
         return operator integer_type() <=> rhs;
      }

      constexpr value_type& operator[](std::size_t index) {
         return _array_access_helper<value_type>(*this, index);
      }

      constexpr const value_type& operator[](std::size_t index) const {
         return _array_access_helper<const value_type>(*this, index);
      }

   private:
      template<typename V, typename T>
      static constexpr V& _array_access_helper(T& t, std::size_t index) {
         switch (index) {
         case 0: return t.red;
         case 1: return t.green;
         case 2: return t.blue;
         }
         if constexpr (size() == 4) {
            if (index == 3) {
               return t.alpha;
            }
            assert(index < 4);
         } else {
            assert(index < 3);
         }
         /* Segfault if index is out of bounds and NDEBUG is defined! */
         OpenVic::utility::unreachable();
      }

   public:
      template<std::size_t Index>
      auto&& get() & {
         return get_helper<Index>(*this);
      }

      template<std::size_t Index>
      auto&& get() && {
         return get_helper<Index>(*this);
      }

      template<std::size_t Index>
      auto&& get() const& {
         return get_helper<Index>(*this);
      }

      template<std::size_t Index>
      auto&& get() const&& {
         return get_helper<Index>(*this);
      }

   private:
      template<std::size_t Index, typename T>
      static auto&& get_helper(T&& t) {
         static_assert(Index < size(), "Index out of bounds for OpenVic::basic_colour_t");
         if constexpr (Index == 0) {
            return std::forward<T>(t).red;
         }
         if constexpr (Index == 1) {
            return std::forward<T>(t).green;
         }
         if constexpr (Index == 2) {
            return std::forward<T>(t).blue;
         }
         if constexpr (Index == 3) {
            return std::forward<T>(t).alpha;
         }
      }
   };

   template<typename T>
   concept IsColour = OpenVic::utility::is_specialization_of_v<T, basic_colour_t>;

   template<typename ValueT, typename IntT>
   struct rgb_colour_traits : colour_traits<ValueT, IntT> {
      static constexpr bool has_alpha = false;
   };

   using colour_argb_t = basic_colour_t<std::uint8_t, std::uint32_t>;
   using colour_rgb_t = basic_colour_t<std::uint8_t, std::uint32_t, rgb_colour_traits<std::uint8_t, std::uint32_t>>;

   using colour_t = colour_rgb_t;

   namespace colour_literals {
      constexpr colour_t operator""_rgb(unsigned long long value) {
         return colour_t::from_integer(value);
      }

      constexpr colour_argb_t operator""_argb(unsigned long long value) {
         return colour_argb_t::from_integer(value);
      }
   }
}

namespace std {
   template<typename ValueT, typename ColourIntT, typename ColourTraits>
   struct tuple_size<::OpenVic::basic_colour_t<ValueT, ColourIntT, ColourTraits>>
      : integral_constant<size_t, ::OpenVic::basic_colour_t<ValueT, ColourIntT, ColourTraits>::size()> {};

   template<size_t Index, typename ValueT, typename ColourIntT, typename ColourTraits>
   struct tuple_element<Index, ::OpenVic::basic_colour_t<ValueT, ColourIntT, ColourTraits>> {
      using type = decltype(::OpenVic::basic_colour_t<ValueT, ColourIntT, ColourTraits>::red);
   };
   template<typename ValueT, typename ColourIntT, typename ColourTraits>
   struct tuple_element<1, ::OpenVic::basic_colour_t<ValueT, ColourIntT, ColourTraits>> {
      using type = decltype(::OpenVic::basic_colour_t<ValueT, ColourIntT, ColourTraits>::green);
   };
   template<typename ValueT, typename ColourIntT, typename ColourTraits>
   struct tuple_element<2, ::OpenVic::basic_colour_t<ValueT, ColourIntT, ColourTraits>> {
      using type = decltype(::OpenVic::basic_colour_t<ValueT, ColourIntT, ColourTraits>::blue);
   };
   template<typename ValueT, typename ColourIntT, typename ColourTraits>
   struct tuple_element<3, ::OpenVic::basic_colour_t<ValueT, ColourIntT, ColourTraits>> {
      using type = decltype(::OpenVic::basic_colour_t<ValueT, ColourIntT, ColourTraits>::alpha);
   };

   template<typename ValueT, typename ColourIntT, typename ColourTraits>
   struct hash<OpenVic::basic_colour_t<ValueT, ColourIntT, ColourTraits>> {
      size_t operator()(OpenVic::basic_colour_t<ValueT, ColourIntT, ColourTraits> const& s) const noexcept {
         return hash<decltype(s.as_argb())> {}(s.as_argb());
      }
   };
}