#ifndef INCLUDED_IMATHCOLORALGO_H
#define INCLUDED_IMATHCOLORALGO_H
#include "ImathColor.h"
#include "ImathMath.h"
#include "ImathLimits.h"
namespace Imath {
Vec3<double>    hsv2rgb_d(const Vec3<double> &hsv);
Color4<double>  hsv2rgb_d(const Color4<double> &hsv);
Vec3<double>    rgb2hsv_d(const Vec3<double> &rgb);
Color4<double>  rgb2hsv_d(const Color4<double> &rgb);
template<class T>
Vec3<T>
hsv2rgb(const Vec3<T> &hsv)
{
    if ( limits<T>::isIntegral() )
    {
    Vec3<double> v = Vec3<double>(hsv.x / double(limits<T>::max()),
                      hsv.y / double(limits<T>::max()),
                      hsv.z / double(limits<T>::max()));
    Vec3<double> c = hsv2rgb_d(v);
    return Vec3<T>((T) (c.x * limits<T>::max()),
               (T) (c.y * limits<T>::max()),
               (T) (c.z * limits<T>::max()));
    }
    else
    {
    Vec3<double> v = Vec3<double>(hsv.x, hsv.y, hsv.z);
    Vec3<double> c = hsv2rgb_d(v);
    return Vec3<T>((T) c.x, (T) c.y, (T) c.z);
    }
}
template<class T>
Color4<T>
hsv2rgb(const Color4<T> &hsv)
{
    if ( limits<T>::isIntegral() )
    {
    Color4<double> v = Color4<double>(hsv.r / float(limits<T>::max()),
                      hsv.g / float(limits<T>::max()),
                      hsv.b / float(limits<T>::max()),
                      hsv.a / float(limits<T>::max()));
    Color4<double> c = hsv2rgb_d(v);
    return Color4<T>((T) (c.r * limits<T>::max()),
                         (T) (c.g * limits<T>::max()),
                         (T) (c.b * limits<T>::max()),
             (T) (c.a * limits<T>::max()));
    }
    else
    {
    Color4<double> v = Color4<double>(hsv.r, hsv.g, hsv.b, hsv.a);
    Color4<double> c = hsv2rgb_d(v);
    return Color4<T>((T) c.r, (T) c.g, (T) c.b, (T) c.a);
    }
}
template<class T>
Vec3<T>
rgb2hsv(const Vec3<T> &rgb)
{
    if ( limits<T>::isIntegral() )
    {
    Vec3<double> v = Vec3<double>(rgb.x / double(limits<T>::max()),
                      rgb.y / double(limits<T>::max()),
                      rgb.z / double(limits<T>::max()));
    Vec3<double> c = rgb2hsv_d(v);
    return Vec3<T>((T) (c.x * limits<T>::max()),
               (T) (c.y * limits<T>::max()),
               (T) (c.z * limits<T>::max()));
    }
    else
    {
    Vec3<double> v = Vec3<double>(rgb.x, rgb.y, rgb.z);
    Vec3<double> c = rgb2hsv_d(v);
    return Vec3<T>((T) c.x, (T) c.y, (T) c.z);
    }
}
template<class T>
Color4<T>
rgb2hsv(const Color4<T> &rgb)
{
    if ( limits<T>::isIntegral() )
    {
    Color4<double> v = Color4<double>(rgb.r / float(limits<T>::max()),
                      rgb.g / float(limits<T>::max()),
                      rgb.b / float(limits<T>::max()),
                      rgb.a / float(limits<T>::max()));
    Color4<double> c = rgb2hsv_d(v);
    return Color4<T>((T) (c.r * limits<T>::max()),
                         (T) (c.g * limits<T>::max()),
                         (T) (c.b * limits<T>::max()),
             (T) (c.a * limits<T>::max()));
    }
    else
    {
    Color4<double> v = Color4<double>(rgb.r, rgb.g, rgb.b, rgb.a);
    Color4<double> c = rgb2hsv_d(v);
    return Color4<T>((T) c.r, (T) c.g, (T) c.b, (T) c.a);
    }
}
template <class T>
PackedColor
rgb2packed(const Vec3<T> &c)
{
    if ( limits<T>::isIntegral() )
    {
    float x = c.x / float(limits<T>::max());
    float y = c.y / float(limits<T>::max());
    float z = c.z / float(limits<T>::max());
    return rgb2packed( V3f(x,y,z) );
    }
    else
    {
    return (  (PackedColor) (c.x * 255)         |
        (((PackedColor) (c.y * 255)) << 8)      |
        (((PackedColor) (c.z * 255)) << 16)     | 0xFF000000 );
    }
}
template <class T>
PackedColor
rgb2packed(const Color4<T> &c)
{
    if ( limits<T>::isIntegral() )
    {
    float r = c.r / float(limits<T>::max());
    float g = c.g / float(limits<T>::max());
    float b = c.b / float(limits<T>::max());
    float a = c.a / float(limits<T>::max());
    return rgb2packed( C4f(r,g,b,a) );
    }
    else
    {
    return (  (PackedColor) (c.r * 255)         |
        (((PackedColor) (c.g * 255)) << 8)      |
        (((PackedColor) (c.b * 255)) << 16)     |
        (((PackedColor) (c.a * 255)) << 24));
    }
}
template <class T>
void
packed2rgb(PackedColor packed, Vec3<T> &out)
{
    if ( limits<T>::isIntegral() )
    {
    T f = limits<T>::max() / ((PackedColor)0xFF);
    out.x =  (packed &     0xFF) * f;
    out.y = ((packed &   0xFF00) >>  8) * f;
    out.z = ((packed & 0xFF0000) >> 16) * f;
    }
    else
    {
    T f = T(1) / T(255);
    out.x =  (packed &     0xFF) * f;
    out.y = ((packed &   0xFF00) >>  8) * f;
    out.z = ((packed & 0xFF0000) >> 16) * f;
    }
}
template <class T>
void
packed2rgb(PackedColor packed, Color4<T> &out)
{
    if ( limits<T>::isIntegral() )
    {
    T f = limits<T>::max() / ((PackedColor)0xFF);
    out.r =  (packed &       0xFF) * f;
    out.g = ((packed &     0xFF00) >>  8) * f;
    out.b = ((packed &   0xFF0000) >> 16) * f;
    out.a = ((packed & 0xFF000000) >> 24) * f;
    }
    else
    {
    T f = T(1) / T(255);
    out.r =  (packed &       0xFF) * f;
    out.g = ((packed &     0xFF00) >>  8) * f;
    out.b = ((packed &   0xFF0000) >> 16) * f;
    out.a = ((packed & 0xFF000000) >> 24) * f;
    }
}
} 
#endif