/*****************************************************************************
 * $LastChangedDate: 2011-04-23 21:07:07 -0400 (Sat, 23 Apr 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   RGBA functions.
 *//*
 * LEGAL:   COPYRIGHT (C) 2004 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#ifndef GFX_RGBA_FUNCS_HH
#define GFX_RGBA_FUNCS_HH 1

#include "base/module.hh"
#include "base/stream.hh"
using namespace base;
#include "math/module.hh"
#include "math/funcs.hh"
using namespace math;
#include "gfx/module.hh"
#include "gfx/rgba.hh"

namespace gfx {

/*****************************************************************************
 * operator<<(RGBA)
 *****************************************************************************/
INLINE std::ostream& operator<<( std::ostream& strm, const RGBA rgba )
{
    std::ios::fmtflags savedFlags = strm.flags();
    strm.setf( std::ios::showbase );
    strm << '('
         << std::hex
         << OMANIP_FIELD(2) << uint(rgba[RR]) << ','  // print numbers, not chars
         << OMANIP_FIELD(2) << uint(rgba[GG]) << ','  // (uint8 is uchar)
         << OMANIP_FIELD(2) << uint(rgba[BB]) << ','
         << OMANIP_FIELD(2) << uint(rgba[AA]) << ')';
    strm.flags(savedFlags);
    return strm;
}

/*****************************************************************************
 * Interpolate between two RGBAs.
 * @param   rgba0
 * @param   rgba1
 * @param   factor
 *          0.0 : rgba0
 *          0.5 : interpolate at mid-point.
 *          1.0 : rgba1
 *****************************************************************************/
INLINE RGBA
InterpolateRGBA( const RGBA rgba0, const RGBA rgba1, const fp factor )
{
ASSERT( FP_GE(factor,0.0f) and FP_LE(factor,1.0f) );
    return RGBA( uint8( int(rgba0[RR]) + (int(rgba1[RR]) - int(rgba0[RR])) * factor ),
                 uint8( int(rgba0[GG]) + (int(rgba1[GG]) - int(rgba0[GG])) * factor ),
                 uint8( int(rgba0[BB]) + (int(rgba1[BB]) - int(rgba0[BB])) * factor ),
                 uint8( int(rgba0[AA]) + (int(rgba1[AA]) - int(rgba0[AA])) * factor ) );
}

/*****************************************************************************
 * Faster interpolation between RGB ignoring alpha.
 *****************************************************************************/
INLINE RGBA
InterpolateRGB( const RGBA rgba0, const RGBA rgba1, const fp factor )
{
ASSERT( FP_GE(factor,0.0f) and FP_LE(factor,1.0f) );
    return RGBA( uint8( int(rgba0[RR]) + (int(rgba1[RR]) - int(rgba0[RR])) * factor ),
                 uint8( int(rgba0[GG]) + (int(rgba1[GG]) - int(rgba0[GG])) * factor ),
                 uint8( int(rgba0[BB]) + (int(rgba1[BB]) - int(rgba0[BB])) * factor ),
                 0 );  // ignore alpha
}

} // namespace gfx

#endif // GFX_RGBA_FUNCS_HH
