colour: specialise the cast function

This commit is contained in:
Danny Robson 2015-04-09 20:45:55 +10:00
parent 0d1fb0147e
commit ef4475949d
4 changed files with 52 additions and 0 deletions

View File

@ -18,6 +18,7 @@ UTIL_FILES = \
bitwise.hpp \ bitwise.hpp \
colour.cpp \ colour.cpp \
colour.hpp \ colour.hpp \
colour.ipp \
coord.hpp \ coord.hpp \
coord/base.hpp \ coord/base.hpp \
coord/init.hpp \ coord/init.hpp \

View File

@ -31,6 +31,11 @@ namespace util {
template <size_t S, typename T> template <size_t S, typename T>
struct colour : public coord::base<S,T,colour,coord::rgba> { struct colour : public coord::base<S,T,colour,coord::rgba> {
using coord::base<S,T,util::colour,coord::rgba>::base; using coord::base<S,T,util::colour,coord::rgba>::base;
using base_t = coord::base<S,T,util::colour,coord::rgba>;
template <typename U>
colour<S,U>
cast (void) const;
static const colour WHITE; static const colour WHITE;
static const colour BLACK; static const colour BLACK;
@ -49,4 +54,6 @@ namespace util {
std::ostream& operator<< (std::ostream&, util::colour<S,T>); std::ostream& operator<< (std::ostream&, util::colour<S,T>);
} }
#include "colour.ipp"
#endif #endif

36
colour.ipp Normal file
View File

@ -0,0 +1,36 @@
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifdef __UTIL_COLOUR_IPP
#error
#endif
#define __UTIL_COLOUR_IPP
template <size_t S, typename T>
template <typename U>
util::colour<S,U>
util::colour<S,T>::cast (void) const
{
colour<S,U> ret;
std::transform (this->begin (),
this->end (),
ret.begin (),
renormalise<T,U>);
return ret;
}

View File

@ -10,4 +10,12 @@ main (int, char**)
CHECK_EQ (util::colour4f::WHITE.g, 1.f); CHECK_EQ (util::colour4f::WHITE.g, 1.f);
CHECK_EQ (util::colour4f::WHITE.b, 1.f); CHECK_EQ (util::colour4f::WHITE.b, 1.f);
CHECK_EQ (util::colour4f::WHITE.a, 1.f); CHECK_EQ (util::colour4f::WHITE.a, 1.f);
// Check casting works between intergral and floating formats
{
util::colour4f f (1);
util::colour<4,uint8_t> u (255);
CHECK_EQ (f.cast<uint8_t> (), u);
CHECK_EQ (u.cast<float> (), f);
}
} }