2011-09-13 15:14:12 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2011-09-13 15:14:12 +10:00
|
|
|
*
|
2017-11-22 17:03:00 +11:00
|
|
|
* Copyright 2010-2017 Danny Robson <danny@nerdcruft.net>
|
2011-09-13 15:14:12 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_COLOUR_HPP
|
|
|
|
#define __UTIL_COLOUR_HPP
|
|
|
|
|
2015-03-06 01:15:52 +11:00
|
|
|
#include "coord.hpp"
|
2016-08-13 21:03:39 +10:00
|
|
|
#include "introspection.hpp"
|
2015-01-13 18:33:02 +11:00
|
|
|
|
2016-08-04 17:42:41 +10:00
|
|
|
#include <ostream>
|
2017-11-22 17:03:00 +11:00
|
|
|
#include <type_traits>
|
2011-09-13 15:14:12 +10:00
|
|
|
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2017-11-22 17:03:00 +11:00
|
|
|
/// An abstract colour POD type componsed of S components of type T.
|
|
|
|
///
|
|
|
|
/// Not to be used directly, instead the use of derived types is required.
|
|
|
|
/// This exists purely to simplify generic colour code.
|
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
|
|
|
typename SelfT
|
|
|
|
>
|
|
|
|
struct colour : coord::base<S,T,SelfT> {
|
|
|
|
using coord::base<S,T,SelfT>::base;
|
2015-04-09 20:45:55 +10:00
|
|
|
|
|
|
|
template <typename U>
|
2017-11-22 17:03:00 +11:00
|
|
|
auto
|
|
|
|
cast (void) const
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
::cruft::revalue_t<SelfT,U> ret;
|
2017-11-22 17:03:00 +11:00
|
|
|
std::transform (std::begin (*this),
|
|
|
|
std::end (*this),
|
|
|
|
std::begin (ret),
|
|
|
|
renormalise<T,U>);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-09-13 15:14:12 +10:00
|
|
|
};
|
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
struct cruft::coord::store<1,T,srgba<1,T>> {
|
2018-03-06 18:13:39 +11:00
|
|
|
union {
|
|
|
|
struct { T r; };
|
|
|
|
T data[1];
|
|
|
|
};
|
2017-11-22 17:03:00 +11:00
|
|
|
};
|
|
|
|
template <typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
struct cruft::coord::store<2,T,srgba<2,T>> {
|
2018-03-06 18:13:39 +11:00
|
|
|
union {
|
|
|
|
struct { T r, g; };
|
|
|
|
T data[2];
|
|
|
|
};
|
2017-11-22 17:03:00 +11:00
|
|
|
};
|
|
|
|
template <typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
struct cruft::coord::store<3,T,srgba<3,T>> {
|
2018-03-06 18:13:39 +11:00
|
|
|
union {
|
|
|
|
struct { T r, g, b; };
|
|
|
|
T data[3];
|
|
|
|
};
|
2017-11-22 17:03:00 +11:00
|
|
|
};
|
2015-06-02 22:59:21 +10:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
struct cruft::coord::store<4,T,srgba<4,T>> {
|
2018-03-06 18:13:39 +11:00
|
|
|
union {
|
|
|
|
struct { T r, g, b, a; };
|
|
|
|
T data[4];
|
|
|
|
};
|
2017-11-22 17:03:00 +11:00
|
|
|
};
|
2015-07-24 01:34:44 +10:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <size_t S, typename T> struct srgba : colour<S,T,srgba<S,T>> {
|
|
|
|
using colour<S,T,srgba<S,T>>::colour;
|
|
|
|
};
|
2015-04-09 21:50:42 +10:00
|
|
|
|
2020-03-12 12:44:20 +11:00
|
|
|
template <typename T>
|
|
|
|
using srgba1 = srgba<1,T>;
|
|
|
|
using srgba1f = srgba1<float>;
|
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
using srgba3f = srgba<3,float>;
|
|
|
|
using srgba4f = srgba<4,float>;
|
2015-01-13 18:33:02 +11:00
|
|
|
|
2018-01-10 17:19:39 +11:00
|
|
|
using srgba3u = srgba<3,uint8_t>;
|
|
|
|
using srgba4u = srgba<4,uint8_t>;
|
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <size_t S, typename T> struct hsva : colour<S,T,hsva<S,T>> {};
|
2016-04-02 13:37:58 +11:00
|
|
|
|
|
|
|
template <size_t S, typename T>
|
2017-11-22 17:03:00 +11:00
|
|
|
struct redim_type<
|
|
|
|
srgba<S,T>
|
|
|
|
> { template <size_t _S> using type = srgba<_S,T>; };
|
2016-08-13 21:03:39 +10:00
|
|
|
|
|
|
|
template <size_t S, typename T>
|
2017-11-22 17:03:00 +11:00
|
|
|
struct revalue_type<srgba<S,T>> {
|
|
|
|
template <typename _T>
|
|
|
|
using type = srgba<S,_T>;
|
2016-08-13 21:03:39 +10:00
|
|
|
};
|
2017-11-22 17:03:00 +11:00
|
|
|
|
|
|
|
|
|
|
|
template <typename> struct is_colour : public std::false_type {};
|
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
|
|
|
template <
|
|
|
|
size_t,
|
|
|
|
typename
|
|
|
|
> typename ColourT
|
|
|
|
> struct is_colour<ColourT<S,T>>
|
|
|
|
:std::conditional_t<
|
|
|
|
std::is_base_of_v<
|
|
|
|
colour<S,T,ColourT<S,T>>,
|
|
|
|
ColourT<S,T>
|
|
|
|
>,
|
|
|
|
std::true_type,
|
|
|
|
std::false_type
|
|
|
|
> {};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
constexpr auto is_colour_v = is_colour<T>::value;
|
2011-09-13 15:14:12 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|